詳解vue中父子組件傳遞參數(shù)props的實(shí)現(xiàn)方式
通過 Prop 向子組件傳遞數(shù)據(jù)
001:父組件=====》子組件通信
<template>
<div>
<h1>這里是父元素</h1>
//******
<childComponent :detailMes="detailMes"/>
</div>
</template>
<script>
import childComponent from './childComponent'
export default {
name:"parentComponent",
data() {
return {
detailMes:'111'
}
},
components: {
childComponent,
},
}
</script>子組件
<template>
<div>
數(shù)據(jù)需要從父元素傳遞過來哦:{{detailMes}}
</div>
</template>
<script>
export default {
name:'childComponent',
data() {
return {
}
},
props: {
detailMes:{
type : String,
}
},
methods: {
name() {
}
}
}002:子組件=====》父組件通信
(要求父組件先給子組件一個(gè)函數(shù))
列表數(shù)據(jù)在父組件,循環(huán)的ul>li在子組件,現(xiàn)在在組件刪除數(shù)據(jù),需要通知父組件
<template>
<div>
<h1>這里是父</h1>
//父組件先給子組件一個(gè)函數(shù)
<childComponent :list="list" :del="del"/>
</div>
</template>
<script>
import childComponent from './childComponent'
export default {
data() {
return {
list:[
{id:"001",stuName:'學(xué)生a'},
{id:"002",stuName:'學(xué)生b'},
]
}
},
components: {
childComponent,
},
methods: {
del(id) {
const idx=this.list.findIndex(v=>v.id==id);
if(idx>-1){
this.list.splice(idx,1)
}
// this.list=this.list.filter(item=>item.id!==id)
}
},
}
</script><template>
<div>
子組件
<ul>
<li v-for="item of list" :key="item.id">
<span>{{item.stuName}}</span>
<button @click="dele(item.id)" class="red">x</button>
</li>
</ul>
</div>
</template>
<script>
export default {
name:'childComponent',
data() {
return {
}
},
props: {
// 父元素傳遞過來的方法
list:{},
// 父組件傳遞過來的方法
del:{}
},
methods: {
dele(ids) {
// 通知父元素,快刪除數(shù)據(jù)了
// 去調(diào)用父組件的方法
this.del(ids);
}
}
}003 傳遞 多層傳遞下去
<template>
<div>
<h1>這里是父</h1>
<childComponent :msg="msg"/>
</div>
</template>
<script>
import childComponent from './childComponent'
export default {
data() {
return {
msg:'這條數(shù)據(jù)需要通過層層傳遞下去'
}
},
components: {
childComponent,
},
}
</script><template>
<div>
子組件
<grandsonComponent :msg="msg"></grandsonComponent>
</div>
</template>
<script>
import grandsonComponent from '@/components/grandsonComponent.vue';
export default {
components: {
grandsonComponent,
},
props: {
msg:{}
},
}
</script><template>
<div>
這是統(tǒng)計(jì)的{{msg}}的數(shù)據(jù)
</div>
</template>
<script>
export default {
name:'grandsonComponent',
props: {
msg: {}
},
}
</script>到此這篇關(guān)于詳解vue中父子組件傳遞參數(shù)props實(shí)現(xiàn)方式的文章就介紹到這了,更多相關(guān)vue父子組件傳遞props內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺談Vue數(shù)據(jù)響應(yīng)思路之?dāng)?shù)組
這篇文章主要介紹了淺談Vue數(shù)據(jù)響應(yīng)思路之?dāng)?shù)組,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-11-11
vue + vuex todolist的實(shí)現(xiàn)示例代碼
這篇文章主要介紹了vue + vuex todolist的實(shí)現(xiàn)示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03
詳解element-ui設(shè)置下拉選擇切換必填和非必填
這篇文章主要介紹了詳解element-ui設(shè)置下拉選擇切換必填和非必填,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
vue中keep-alive組件使用和一些基礎(chǔ)配置方法
本文主要介紹了Vue中keep-alive組件的使用方法和一些基礎(chǔ)配置,keep-alive是Vue中的一個(gè)抽象組件,可以緩存組件實(shí)例,提高性能,本文給大家介紹vue中keep-alive組件使用和一些基礎(chǔ)配置方法,感興趣的朋友一起看看吧2024-10-10
關(guān)于vue-router的使用及實(shí)現(xiàn)原理
這篇文章主要介紹了關(guān)于vue-router的使用及實(shí)現(xiàn)原理,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06

