1. vue中各個組件之間傳值
1.父子組件
父組件-->子組件,通過子組件的自定義屬性:props
子組件-->父組件,通過自定義事件:this.$emit('事件名',參數1,參數2,...);
2.非父子組件或父子組件
通過數據總數Bus,this.$root.$emit('事件名',參數1,參數2,...)
3.非父子組件或父子組件
更好的方式是在vue中使用vuex
方法1: 用組件之間通訊。這樣寫很麻煩,並且寫着寫着,估計自己都不知道這是啥了,很容易寫暈。
方法2: 我們定義全局變量。模塊a的數據賦值給全局變量x。然后模塊b獲取x。這樣我們就很容易獲取到數據

2. Vuex
官方解釋:Vuex 是一個專為 Vue.js 應用程序開發的狀態管理模式。可以想象為一個“前端數據庫”(數據倉庫),
讓其在各個頁面上實現數據的共享包括狀態,並且可操作
Vuex分成五個部分:
1.State:單一狀態樹
2.Getters:狀態獲取
3.Mutations:觸發同步事件
4.Actions:提交mutation,可以包含異步操作
5.Module:將vuex進行分模塊

3. vuex使用步驟
3.1 安裝
npm install vuex -S
安裝完成后 package.json中會有
"vuex": "^3.1.1"
3.2 創建store模塊,分別維護state/actions/mutations/getters

3.3 在store/index.js文件中新建vuex的store實例,並注冊上面引入的各大模塊
import Vue from 'vue' import Vuex from 'vuex' import state from './state' import getters from './getters' import actions from './actions' import mutations from './mutations' Vue.use(Vuex) const store = new Vuex.Store({ state, getters, actions, mutations }) export default store
3.4 在main.js中導入並使用store實例
import store from './store' new Vue({ el: '#app', data() { return { /* 總線*/ Bus:new Vue({ }) } }, router, store, components: { App }, template: '<App/>' })
4. vuex的核心概念:store、state、getters、mutations、actions
4.0 store
每一個Vuex應用的核心就是store(倉庫),store基本上就是一個容器,它包含着你的應用中大部分的狀態 (state)。
const store = new Vuex.Store({
state, // 共同維護的一個狀態,state里面可以是很多個全局狀態
getters, // 獲取數據並渲染
actions, // 數據的異步操作
mutations // 處理數據的唯一途徑,state的改變或賦值只能在這里
})
4.1 state.js(保存數據的容器)
export default { resturantName: '飛歌餐館' }
4.2 getters.js(getXxx)
export default { getResturantName: (state) => { return state.resturantName; } }
注1:getters將state中定義的值暴露在this.$store.getters對象中,我們可以通過如下代碼訪問
this.$store.getters.resturantName
注2:state狀態存儲是響應式的,從store實例中讀取狀態最簡單的方法就是在計算屬性中返回某個狀態,如下:
computed: {
resturantName: function() {
return this.$store.getters.resturantName;
}
}
4.3 mutations(setXxx)
export default { // type(事件類型): 其值為setResturantName // payload:官方給它還取了一個高大上的名字:載荷,其實就是一個保存要傳遞參數的容器 setResturantName: (state, payload) => { state.resturantName = payload.resturantName; } }
4.4 actions.js
export default { setResturantNameAsync: (context, payload) => { console.log('xxx') setTimeout(() => { console.log('yyy') context.commit('setResturantName', payload); //Action提交的是mutation }, 3000); console.log('zzz') }, doAjax: (context, payload) => { //在vuex里面是不能使用vue實例的 let _this =payload._this; let url = _this.axios.urls.SYSTEM_USER_DOLOGIN; _this.axios.post(url, {}).then((response) => { console.log('doAjax...........'); console.log(response); }).catch(function(error) { console.log(error); }); } }
Action類似於 mutation,不同在於:
1.Action提交的是mutation,而不是直接變更狀態
2.Action可以包含任意異步操作
3.Action的回調函數接收一個 context 上下文參數,注意,這個參數可不一般,它與 store 實例有着相同的方法和屬性
但是他們並不是同一個實例,context 包含:
1. state、2. rootState、3. getters、4. mutations、5. actions 五個屬性
所以在這里可以使用 context.commit 來提交一個 mutation,或者通過 context.state 和 context.getters 來獲取 state 和 getters。
實例 VuexPage1.vue
<template>
<div>
<h3 style="margin: 60px;">第一個vuex頁面:{{title}}</h3>
<button @click="changeTitle">餐館易主</button>
<button @click="changeTitleAsync">兩個月后餐館易主</button>
<button @click="doAjax">測試vuex中使用ajax</button>
</div>
</template>
<script>
export default{
data() {
return {
};
},
methods:{
changeTitle(){
this.$store.commit('setResturantName',{
resturantName:'小豬砍刀羊肉館'
});
},
changeTitleAsync(){
/* 異步*/
this.$store.dispatch('setResturantNameAsync',{
resturantName:'小豬砍刀蛇肉館'
});
},
doAjax(){
this.$store.dispatch('doAjax',{
_this:this
});
}
},
/* 計算屬性*/
computed:{
title(){
// return this.$store.state.resturantName;
return this.$store.getters.getResturantName
}
}
}
</script>
<style>
</style>
VuexPage2.vue
<template>
<div>
<h3 style="margin: 60px;">第二個vuex頁面:{{title}}</h3>
</div>
</template>
<script>
export default{
data() {
return {
title:''
};
},
/* 鈎子函數*/
created(){
this.title = this.$store.state.resturantName;
}
}
</script>
<style>
</style>
將組件掛上
import VuexPage1 from '@/views/sys/VuexPage1' import VuexPage2 from '@/views/sys/VuexPage2' { path: '/sys/VuexPage1', name: 'VuexPage1', component: VuexPage1 }, { path: '/sys/VuexPage2', name: 'VuexPage2', component: VuexPage2 }
效果:

