淺談Vue的組件間傳值(包括Vuex)
在不使用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>
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>
效果圖:
實(shí)際上,為了數(shù)據(jù)能在父子組件間傳值;還可以通過調(diào)用父組件的函數(shù)或調(diào)用子組件的函數(shù)的方式實(shí)現(xiàn)傳值。 Vue中子組件調(diào)用父組件的函數(shù)
https://www.jb51.net/article/134732.htm
Vue父組件調(diào)用子組件的函數(shù)
https://www.jb51.net/article/219793.htm
Vuex是Vue框架中不可或缺的一部分;
Vuex在需要多組件通信的時(shí)候顯得格外重要;比如數(shù)據(jù)在父組件形成,但數(shù)據(jù)需要在子組件的子組件中使用時(shí),就可以使用Vuex管理;或者說需要兄弟組件傳值時(shí),可以使用Vuex。
在Vue的store.js中有五個(gè)屬性:分別是state,mutations,actions,getters,modules
結(jié)構(gòu)為:
let a={ state: { name:'moduleA' }, //mutations專門用于改變state屬性中的數(shù)據(jù) mutations: { setFun(state,item){state.name=item;} }}export default new Vuex.Store({ //state專門存放數(shù)據(jù) state: { num:100, useAcomponent:{name:'A',},useBcomponent:'content', }, //mutations專門用于改變state屬性中的數(shù)據(jù) mutations: { setStateFun(state,item){state.useBcomponent='Bcomponent';} }, actions: { httpGetData(store,item){setTimeout(()=>{console.log(item);store.commit('setStateFun',item);},3000)} }, getters:{ //調(diào)用getters中的函數(shù)時(shí)沒有入?yún)etterFun1(state){return state.num++} //調(diào)用getters中的函數(shù)時(shí)有入?yún)? gettterFun2(state){return function(val){return state.num+=val;}} }, modules: { ModuleA:a }});}
state中的數(shù)據(jù)可以在不同組件中訪問獲取。
獲取state的數(shù)據(jù):
this.$store.state.state對象中的數(shù)據(jù);例如let val=this.$store.state.num;
更改state數(shù)據(jù),就是調(diào)用Vuex的mutations對象中的函數(shù):
this.$store.commit('函數(shù)名','數(shù)據(jù)');例如this.$store.commit('setStateFun','testSetItem');
actions對象,用于在Vuex中發(fā)請求
this.$store.dispatch('函數(shù)名','數(shù)據(jù)');例如this.$store.dispatch('httpGetData','testItem');
getters對象,類似Vue的計(jì)算屬性
this.$store.getters.函數(shù)名;例如//沒入?yún)r(shí)this.$store.getters.getterFun1;//有入?yún)r(shí)this.$store.getters.getterFun2(123);
modules對象,類似將需要使用的store模塊化分開,每個(gè)modules對象對應(yīng)一個(gè)模塊
//獲取modules對象中的state數(shù)據(jù)this.$store.state.modules對象名.state值;例如this.$store.state.ModuleA.name//使用modules對象中的mutations的函數(shù)this.$store.commit('函數(shù)名','入?yún)?shù)據(jù)');例如this.$store.commit('setFun','itemabc');//這里需要注意,如果modules模塊中與外部(不是modules對象模塊)的mutations對象中有相同名字的函數(shù)時(shí),則相同名字的函調(diào)用時(shí)都會(huì)執(zhí)行
到此這篇關(guān)于淺談Vue的組件間傳值(包括Vuex)的文章就介紹到這了,更多相關(guān)Vue 組件間傳值內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. JavaScript Reduce使用詳解2. 簡述JAVA同步、異步、阻塞和非阻塞之間的區(qū)別3. 深入了解JAVA 軟引用4. 在JSP中使用formatNumber控制要顯示的小數(shù)位數(shù)方法5. SpringBoot集成mqtt的多模塊項(xiàng)目配置詳解6. Python TestSuite生成測試報(bào)告過程解析7. SpringBoot集成Redisson實(shí)現(xiàn)延遲隊(duì)列的場景分析8. 使用Python3 poplib模塊刪除服務(wù)器多天前的郵件實(shí)現(xiàn)代碼9. 解決AJAX返回狀態(tài)200沒有調(diào)用success的問題10. 詳解JAVA 強(qiáng)引用
