詳解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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue mockjs編寫假數(shù)據(jù)并請(qǐng)求獲取實(shí)現(xiàn)流程
這篇文章主要介紹了Vue mockjs編寫假數(shù)據(jù)并請(qǐng)求獲取實(shí)現(xiàn)流程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-12-12
Vue實(shí)現(xiàn)拖拽穿梭框功能四種方式實(shí)例詳解
這篇文章主要介紹了Vue實(shí)現(xiàn)拖拽穿梭框功能四種方式,使用原生js實(shí)現(xiàn)拖拽,VUe使用js實(shí)現(xiàn)拖拽穿梭框,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09
vue項(xiàng)目打包發(fā)布到Nginx后無法訪問后端接口的解決辦法
這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目打包發(fā)布到Nginx后無法訪問后端接口的解決辦法,記錄一下項(xiàng)目需要注意的地方,方便以后快速使用,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-04-04
詳解mpvue中使用vant時(shí)需要注意的onChange事件的坑
這篇文章主要介紹了詳解mpvue中使用vant時(shí)需要注意的onChange事件的坑,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-05-05
vue element el-form 多級(jí)嵌套驗(yàn)證的實(shí)現(xiàn)示例
本文主要介紹了vue element el-form 多級(jí)嵌套驗(yàn)證的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
在vue中把含有html標(biāo)簽轉(zhuǎn)為html渲染頁面的實(shí)例
今天小編就為大家分享一篇在vue中把含有html標(biāo)簽轉(zhuǎn)為html渲染頁面的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-10-10

