Vue父子組件之間的通信實例詳解
在vue組件通信中其中最常見通信方式就是父子組件之中的通信,而父子組件的設定方式在不同情況下又各有不同。最常見的就是父組件為控制組件子組件為視圖組件。父組件傳遞數(shù)據(jù)給子組件使用,遇到業(yè)務邏輯操作時子組件觸發(fā)父組件的自定義事件。無論哪種組織方式父子組件的通信方式都是大同小異。
一、父組件到子組件通訊
父組件到子組件的通訊主要為:子組件接受使用父組件的數(shù)據(jù),這里的數(shù)據(jù)包括屬性和方法(String,Number,Boolean,Object, Array ,Function)。vue提倡單項數(shù)據(jù)流,因此在通常情況下都是父組件傳遞數(shù)據(jù)給子組件使用,子組件觸發(fā)父組件的事件,并傳遞給父組件所需要的參數(shù)。
1、通過props傳遞數(shù)據(jù)
父子通訊中最常見的數(shù)據(jù)傳遞方式就是通過props傳遞數(shù)據(jù),就好像方法的傳參一樣,父組件調用子組件并傳入數(shù)據(jù),子組件接受到父組件傳遞的數(shù)據(jù)進行驗證使用。
<!--父組件-->
<template>
<div>
<h2>父組件</h2>
<br>
<Child-one :parentMessage="parentMessage"></Child-one>
</div>
</template>
<script>
import ChildOne from './ChildOne';
export default{
components: {
ChildOne,
},
data() {
return {
parentMessage: '我是來自父組件的消息',
};
},
};
</script>
<style scoped>
</style>
<!--子組件-->
<template>
<div>
<h3>我是子組件一</h3>
<span>{{parentMessage}}</span>
</div>
</template>
<script>
export default{
props: ['parentMessage'],
};
</script>
<style scoped>
</style>
props可以接受function,所以function也可以以這種方式傳遞到子組件使用。
2、通過$on傳遞父組件方法
通過$on傳遞父組件方法是組件通信中常用的方法傳遞方式。它可以與通過props傳遞方法達到相同的效果。相比于props傳遞function,它更加的直觀和顯示的表現(xiàn)出了調用關系。
<!--父組件-->
<template>
<div>
<h2>父組件</h2>
<br>
<Child-one @childEvent="parentMethod"></Child-one>
</div>
</template>
<script>
import ChildOne from './ChildOne';
export default{
components: {
ChildOne,
},
data() {
return {
parentMessage: '我是來自父組件的消息',
};
},
methods: {
parentMethod() {
alert(this.parentMessage);
},
},
};
</script>
<style scoped>
</style>
<!--子組件-->
<template>
<div>
<h3>我是子組件一</h3>
</div>
</template>
<script>
export default{
mounted() {
this.$emit('childEvent');
},
};
</script>
<style scoped>
</style>
3、獲取父組件然后使用父組件中的數(shù)據(jù)(不推薦)
準確來說這種方式并不屬于數(shù)據(jù)的傳遞而是一種主動的查找。父組件并沒有主動的傳遞數(shù)據(jù)給子組件,而是子組件通過與父組件的關聯(lián)關系,獲取了父組件的數(shù)據(jù)。
該方法雖然能實現(xiàn)獲取父組件中的數(shù)據(jù)但是不推薦這種方式,因為vue提倡單向數(shù)據(jù)流,只有父組件交給子組件的數(shù)據(jù)子組件才有使用的權限,不允許子組件私自獲取父組件的數(shù)據(jù)進行使用。在父與子的關系中子應當是處于一種被動關系。
this.$parent
此處的this為子組件實例
二、子組件到父組件通訊
子組件到父組件的通訊主要為父組件如何接受子組件之中的數(shù)據(jù)。這里的數(shù)據(jù)包括屬性和方法(String,Number,Boolean,Object, Array ,Function)。
1、通過$emit傳遞父組件數(shù)據(jù)
與父組件到子組件通訊中的$on配套使用,可以向父組件中觸發(fā)的方法傳遞參數(shù)供父組件使用。
<!--父組件-->
<template>
<div>
<h2>父組件</h2>
<br>
<Child-one @childEvent="parentMethod"></Child-one>
</div>
</template>
<script>
import ChildOne from './ChildOne';
export default{
components: {
ChildOne,
},
data() {
return {
parentMessage: '我是來自父組件的消息',
};
},
methods: {
parentMethod({ name, age }) {
console.log(this.parentMessage, name, age);
},
},
};
</script>
<style scoped>
</style>
<!--子組件-->
<template>
<div>
<h3>我是子組件一</h3>
</div>
</template>
<script>
export default{
mounted() {
this.$emit('childEvent', { name: 'zhangsan', age: 10 });
},
};
</script>
<style scoped>
</style>
2、refs獲取
可以通過在子組件添加ref屬性,然后可以通過ref屬性名稱獲取到子組件的實例。準確來說這種方式和this.$parent一樣并不屬于數(shù)據(jù)的傳遞而是一種主動的查找。
盡量避免使用這種方式。因為在父子組件通信的過程中。父組件是處于高位是擁有控制權,而子組件在多數(shù)情況下應該為純視圖組件,只負責視圖的展示和自身視圖的邏輯操作。對外交互的權利應該由父組件來控制。所以應當由父組件傳遞視圖數(shù)據(jù)給子組件,子組件負責展示。而子組件的對外交互通過$emit觸發(fā)父組件中相應的方法,再由父組件處理相應邏輯。
<template>
<div>
<h2>父組件</h2>
<br>
<Child-one ref="child"></Child-one>
</div>
</template>
<script>
import ChildOne from './ChildOne';
export default{
components: {
ChildOne,
},
mounted(){
console.log(this.$refs['child']);
},
};
</script>
<style scoped>
</style>
this.$refs['child']
總結
以上所述是小編給大家介紹的Vue父子組件之間的通信,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關文章
Vue + Webpack + Vue-loader學習教程之相關配置篇
這篇文章主要介紹了關于Vue + Webpack + Vue-loader的相關配置篇,文中通過示例代碼介紹的非常詳細,相信對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。2017-03-03
element-plus的el-table自定義表頭篩選查詢功能實現(xiàn)
這篇文章主要介紹了element-plus的el-table自定義表頭篩選查詢功能實現(xiàn),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-07-07
Vue項目中v-bind動態(tài)綁定src路徑不成功問題及解決
這篇文章主要介紹了Vue項目中v-bind動態(tài)綁定src路徑不成功問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04
Vue?element實現(xiàn)權限管理業(yè)務流程詳解
目前本人再使用vue-element-admin項目時都是通過直接刪除一些用不上的路由來進行側邊欄的清除,但是其實有一個更加好的辦法來對項目的側邊欄顯示的內用進行管理,就是權限管理,其實也不知道這個方法好不好,原理上來說時跟直接刪除該路由的方式時一樣的2022-08-08

