欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue父子組件通信的高級用法示例

 更新時間:2019年08月29日 08:28:35   作者:我的小熊不見了  
這篇文章主要給大家介紹了關(guān)于vue父子組件通信的高級用法,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用vue具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧

前言

vue項(xiàng)目的一大亮點(diǎn)就是組件化。使用組件可以極大地提高項(xiàng)目中代碼的復(fù)用率,減少代碼量。但是使用組件最大的難點(diǎn)就是父子組件之間的通信。

子通信父

父組件

<template>
<div class="parent">
我是父組件
<!--父組件監(jiān)聽子組件觸發(fā)的say方法,調(diào)用自己的parentSay方法-->
<!--通過:msg將父組件的數(shù)據(jù)傳遞給子組件-->
<children :msg="msg" @say="parentSay"></children>
</div>
</template>

<script>
import Children from './children.vue'
export default {
data () {
return {
msg: 'hello, children'
}
},
methods: {
// 參數(shù)就是子組件傳遞出來的數(shù)據(jù)
parentSay(msg){
console.log(msg) // hello, parent
}
},

// 引入子組件
components:{
children: Children
}
}
</script>

子組件

<template>
<div class="hello">
<div class="children" @click="say">
我是子組件
<div>
父組件對我說:{{msg}}
</div>
</div>

</div>
</template>

<script>

export default {
//父組件通過props屬性傳遞進(jìn)來的數(shù)據(jù)
props: {
msg: {
type: String,
default: () => {
return ''
}
}
},
data () {
return {
childrenSay: 'hello, parent'
}
},

methods: {
// 子組件通過emit方法觸發(fā)父組件中定義好的函數(shù),從而將子組件中的數(shù)據(jù)傳遞給父組件
say(){
this.$emit('say' , this.childrenSay);
}
}
}
</script>

子組件使用$emit方法調(diào)用父組件的方法,達(dá)到子通信父的目的。

父通信子

父組件

<!--Html-->
<template>
<!--父組件觸發(fā)click方法-->
<div class="parent" @click="say">
我是父組件
<!--通過ref標(biāo)記子組件-->
<children ref="child"></children>
</div>
</template>

<script>
import Children from './children.vue'
export default {
data () {
return {
msg: "hello,my son"
}
},
methods: {
// 通過$refs調(diào)用子組件的parentSay方法
say(){
this.$refs.child.parentSay(this.msg);
}
},

// 引入子組件
components:{
children: Children
}
}
</script>

子組件

<template>
<div class="hello">
<div class="children" >
我是子組件
<div>
父組件對我說:{{msg}}
</div>
</div>

</div>
</template>

<script>

export default {
data () {
return {
msg: ''
}
},

methods: {
// 父組件調(diào)用的JavaScript方法parentSay
parentSay(msg){
this.msg = msg;
}
}
}
</script>

父組件通過 $refs 調(diào)用子組件的方法。

以上就是父子組件通信的方式,父子組件傳遞數(shù)據(jù)直接用props,或者使用 $emit 和 $refs 依靠事件來傳遞數(shù)據(jù)。

父子組件通信提升篇

上文中,子通信父是在 子中觸發(fā)點(diǎn)擊事件 然后調(diào)用父組件的方法,父通信子是在 父中觸發(fā)點(diǎn)擊事件 調(diào)用子組件的方法。但是實(shí)際情況中可能存在 子通信父時子組件不允許有點(diǎn)擊事件 而事件在父中或者 父通信子時點(diǎn)擊事件在子組件 中。

子通信父時擊事件在父組件

這種情況其實(shí)很常見,例如提交一個表單時表單的內(nèi)容為子組件,而保存按鈕在父組件上。此時點(diǎn)擊保存按鈕想要獲取子組件表單的值。這種情況下已經(jīng)不單單是子通信父和父通信子了,需要將兩者結(jié)合在一起使用才能完成整個通信過程。

實(shí)現(xiàn)的思路是在父組件中點(diǎn)擊事件時,先通過父子通信調(diào)用子組件的方法,然后在子組件中的該方法里使用子父通信調(diào)用父組件的另一個方法并將信息傳遞回來。以下是代碼演示:

父組件

<template>
<div class="parent" @click="getData">
我是父組件
<!--父組件監(jiān)聽子組件觸發(fā)的transData方法,調(diào)用自己的transData方法-->
<!--通過ref標(biāo)記子組件-->
<children ref="child" @transData="transData"></children>
</div>
</template>

<script>
import Children from './children.vue'
export default {
data () {
return {
msg: 'hello, children'
}
},
methods: {
getData(){
// 調(diào)用子組件的getData方法
this.$refs.child.getData();
},
// 參數(shù)就是子組件傳遞出來的數(shù)據(jù)
transData(msg){
console.log(msg) // hello, parent
}
},

// 引入子組件
components:{
children: Children
}
}
</script>

子組件

<template>
<div class="hello">
<div class="children" >
我是子組件
<div>
子組件的數(shù)據(jù):{{childrenSay}}
</div>
</div>

</div>
</template>

<script>

export default {
data () {
return {
childrenSay: 'hello, parent'
}
},
methods: {
// 子組件通過emit方法觸發(fā)父組件中定義好的函數(shù),從而將子組件中的數(shù)據(jù)傳遞給父組件
getData() {
this.$emit('transData' , this.childrenSay);
}
}
}
</script>

另一種情況思路也和這個一樣,基礎(chǔ)就在與父通信子和子通信父的靈活運(yùn)用。

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。

相關(guān)文章

最新評論