国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

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

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

瀏覽:3日期: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
相關文章:
主站蜘蛛池模板: 西峡县| 东莞市| 韶关市| 蕉岭县| 财经| 凤冈县| 沽源县| 日照市| 雷波县| 汉阴县| 东源县| 沂水县| 庆安县| 文安县| 平塘县| 札达县| 天祝| 宁波市| 交口县| 阳西县| 兴城市| 来宾市| 水城县| 四子王旗| 巧家县| 方城县| 新乐市| 伊宁县| 房产| 房产| 新巴尔虎左旗| 皋兰县| 中卫市| 香港 | 奉新县| 香港| 峡江县| 威海市| 博客| 淮北市| 萨迦县|