淺談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. vue實現web在線聊天功能2. 完美解決vue 中多個echarts圖表自適應的問題3. JavaScript實現頁面動態驗證碼的實現示例4. 解決Android Studio 格式化 Format代碼快捷鍵問題5. JavaEE SpringMyBatis是什么? 它和Hibernate的區別及如何配置MyBatis6. Java使用Tesseract-Ocr識別數字7. Python使用urlretrieve實現直接遠程下載圖片的示例代碼8. 在Chrome DevTools中調試JavaScript的實現9. Springboot 全局日期格式化處理的實現10. SpringBoot+TestNG單元測試的實現
