久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

淺談Vue的組件間傳值(包括Vuex)

瀏覽:4日期:2023-12-12 11:51:46
目錄父傳子:子傳父:

在不使用Vuex的情況下,組件間傳值的方式是通過父傳子的方式或者兄弟組件傳值。

父傳子:

fatherComponent:

<template> <div><HELLOWORLD :needData='content'></HELLOWORLD> </div></template><script>import HELLOWORLD from ’../components/HelloWorld.vue’export default { components:{HELLOWORLD }, data(){return{ content:'content'} }}</script><style lang='less' scoped></style>

SonComponent(子組件名稱為HELLOWORLD):

<template> <div><h1>HELLOWORLD</h1> </div></template><script>export default { props:['needData'], data(){return{ H:this.needData,} }, mounted(){console.log(this.H); }}</script><style lang='less' scoped></style>

淺談Vue的組件間傳值(包括Vuex)

子傳父:

FatherComponent:

<template> <div><HELLOWORLD @sendData='getData'></HELLOWORLD> </div></template><script>import HELLOWORLD from ’../components/HelloWorld.vue’export default { components:{HELLOWORLD }, data(){return{ } }, methods:{getData(sonData){ console.log('data=>',sonData);}, }}</script><style lang='less' scoped></style>

SonComponent:

<template> <div><h1>HELLOWORLD</h1> </div></template><script>export default { data(){return{ content:'content'} }, mounted(){this.$emit('sendData',this.content); }}</script><style lang='less' scoped></style>

效果圖:

淺談Vue的組件間傳值(包括Vuex)

實際上,為了數據能在父子組件間傳值;還可以通過調用父組件的函數或調用子組件的函數的方式實現傳值。 Vue中子組件調用父組件的函數

https://www.jb51.net/article/134732.htm

Vue父組件調用子組件的函數

https://www.jb51.net/article/219793.htm

Vuex是Vue框架中不可或缺的一部分;

Vuex在需要多組件通信的時候顯得格外重要;比如數據在父組件形成,但數據需要在子組件的子組件中使用時,就可以使用Vuex管理;或者說需要兄弟組件傳值時,可以使用Vuex。

在Vue的store.js中有五個屬性:分別是state,mutations,actions,getters,modules

結構為:

let a={ state: { name:'moduleA' }, //mutations專門用于改變state屬性中的數據 mutations: { setFun(state,item){state.name=item;} }}export default new Vuex.Store({ //state專門存放數據 state: { num:100, useAcomponent:{name:'A',},useBcomponent:'content', }, //mutations專門用于改變state屬性中的數據 mutations: { setStateFun(state,item){state.useBcomponent='Bcomponent';} }, actions: { httpGetData(store,item){setTimeout(()=>{console.log(item);store.commit('setStateFun',item);},3000)} }, getters:{ //調用getters中的函數時沒有入參getterFun1(state){return state.num++} //調用getters中的函數時有入參 gettterFun2(state){return function(val){return state.num+=val;}} }, modules: { ModuleA:a }});}

state中的數據可以在不同組件中訪問獲取。

獲取state的數據:

this.$store.state.state對象中的數據;例如let val=this.$store.state.num;

更改state數據,就是調用Vuex的mutations對象中的函數:

this.$store.commit('函數名','數據');例如this.$store.commit('setStateFun','testSetItem');

actions對象,用于在Vuex中發請求

this.$store.dispatch('函數名','數據');例如this.$store.dispatch('httpGetData','testItem');

getters對象,類似Vue的計算屬性

this.$store.getters.函數名;例如//沒入參時this.$store.getters.getterFun1;//有入參時this.$store.getters.getterFun2(123);

modules對象,類似將需要使用的store模塊化分開,每個modules對象對應一個模塊

//獲取modules對象中的state數據this.$store.state.modules對象名.state值;例如this.$store.state.ModuleA.name//使用modules對象中的mutations的函數this.$store.commit('函數名','入參數據');例如this.$store.commit('setFun','itemabc');//這里需要注意,如果modules模塊中與外部(不是modules對象模塊)的mutations對象中有相同名字的函數時,則相同名字的函調用時都會執行

到此這篇關于淺談Vue的組件間傳值(包括Vuex)的文章就介紹到這了,更多相關Vue 組件間傳值內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Vue
相關文章:
主站蜘蛛池模板: 松原市| 大关县| 新安县| 济源市| 海原县| 上栗县| 淅川县| 郯城县| 枣庄市| 晋江市| 江源县| 重庆市| 宜兰县| 靖远县| 富平县| 宁安市| 聂拉木县| 资中县| 霍林郭勒市| 南陵县| 镇安县| 福安市| 永登县| 托克逊县| 武鸣县| 色达县| 和龙市| 安阳县| 泰宁县| 乌拉特前旗| 雅江县| 琼结县| 宽甸| 利川市| 炎陵县| 嘉兴市| 清水河县| 大安市| 甘德县| 鄂州市| 炎陵县|