淺談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>
效果圖:
實際上,為了數據能在父子組件間傳值;還可以通過調用父組件的函數或調用子組件的函數的方式實現傳值。 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 組件間傳值內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
1. python如何換行輸出2. Python使用urlretrieve實現直接遠程下載圖片的示例代碼3. Python:UserWarning:此模式具有匹配組。要實際獲得組,請使用str.extract4. Android Studio中一套代碼多渠道打包的實現方法5. Java 接口和抽象類的區別詳解6. python如何計算圓的面積7. Java使用Tesseract-Ocr識別數字8. Android打包篇:Android Studio將代碼打包成jar包教程9. 詳解java google Thumbnails 圖片處理10. 解決Android Studio 格式化 Format代碼快捷鍵問題
