vue之使用vuex進行狀態(tài)管理詳解
vuex進行狀態(tài)管理
首先學(xué)習vuex必須先知道vue原理
Vue是一個典型的MVVM框架,模型(Model)只是普通的JavaScript對象,修改它則視圖(View)會自動更新。這種設(shè)計讓狀態(tài)管理變得非常簡單而直觀
Vue實現(xiàn)這種數(shù)據(jù)雙向綁定的效果,需要三大模塊:
Observer:能夠?qū)?shù)據(jù)對象的所有屬性進行監(jiān)聽,如有變動可拿到最新值并通知訂閱者Compile:對每個元素節(jié)點的指令進行掃描和解析,根據(jù)指令模板替換數(shù)據(jù),以及綁定相應(yīng)的更新函數(shù)Watcher:作為連接Observer和Compile的橋梁,能夠訂閱并收到每個屬性變動的通知,執(zhí)行指令綁定的相應(yīng)回調(diào)函數(shù),從而更新視圖
大概分為這么幾類
vuex是什么呢
vuex是實現(xiàn)狀態(tài)管理的工具 可以用來管理大型項目的工具
下面是一個簡單的vuex的結(jié)構(gòu)
index是主文件

//index//
import Vue from 'vue'
import Vuex from 'vuex'
import * as getters from './getters'
import * as actions from './actions'
import * as mutations from './mutations'
Vue.use(Vuex)
const state = {
username: '123'
}
const store = new Vuex.Store({
state,
getters,
actions,
mutations
})
export default store
進行一個簡單地賬戶管理
//actions文件
// 給action注冊事件處理函數(shù)。當這個函數(shù)被觸發(fā)時候,將狀態(tài)提交到mutations中處理
export function modifyAName({commit}, name) { // commit 提交;name即為點擊后傳遞過來的參
return commit ('modifyAName', name)
}
// ES6精簡寫法
//export const modifyAName = ({commit},name) => commit('modifyAName', name)
//motations文件
// 提交 mutations是更改Vuex狀態(tài)的唯一合法方法
export const modifyAName = (state, name) => { // A組件點擊更改餐館名稱為 A餐館
state.resturantName = name // 把方法傳遞過來的參數(shù),賦值給state中的resturantName
}
//getters文件
// 獲取最終的狀態(tài)信息
export const resturantName = state => state.resturantName
//組件A/methods函數(shù)
sub(){
let log = document.querySelector("#log").value;
let reg = document.querySelector("#reg").value;
userapi("/users/userpsw",{name:log,psw:reg}, (mes) => {
if(mes === 1){
console.log(log)
this.$store.commit("modifyAName",log)
// 賬戶名稱
console.log(this.$store.getters.resturantName)
document.querySelector(".text").innerHTML = "登錄成功"
this.$router.push({ path: '/my' })
}else{
document.querySelector(".text").innerHTML = "登錄失敗"
}
})
},
組件B/computed
computed:{
username(){//調(diào)用username即可 {{username}}
return this.$store.getters.resturantName
}
},



vuex狀態(tài)管理基本使用
通過npm安裝vuex
npm install vuex --save
使用vuex
import Vuex from 'vuex'
Vue.use(Vuex)
//賦值給一個變量,然后引入
let store = new Vuex.Store({
...
})
//為了更好的維護,且體現(xiàn)模塊化,可以寫在別的js文件中,然后導(dǎo)入
import store from './store' //創(chuàng)建一個store文件夾,默認為文件夾下的index.js文件
new Vue({
...
store,
...
})使用vuex基本結(jié)構(gòu)
new Vuex.Store({
//狀態(tài)
state: {
count: 100
},
//將狀態(tài)裝飾,或刷選,以想要的結(jié)果輸出
getters: {
hasUnit () {
return this.state.count.filter(count => count + 1)
}
},
//可以理解為變更狀態(tài)的方法,第一個參數(shù)為state,可以再添加自定義參數(shù),注意:只能進行同步
mutations: {
increment (state) {
state.count++
}
},
//可控制的提交mutation事件,彌補mutation無法異步
actions: {
increment (state) {
setTimeout(() =>
state.commit('increment'), 30)
}
}
})為了更好的維護,往往將store文件寫成以下結(jié)構(gòu) ,名字自定義,但是需要知道每個文件的作用

index.js:主要作為導(dǎo)出store,作為匯總的文件state.js:作為定義的變量集合,作為一個對象導(dǎo)出getters.js:作為主要是獲取state中變量值的方法,return出state相應(yīng)的值,或者length等等的一些state變量的信息mutations-type.js: 作為一些方法等命名的變量,防止書寫錯誤
![]()
mutaintions.js:存放修改state變量的同步方法,這里可以的方法名就是用了mutations-type.js中的變量命名,[object.value]這種方式是ES6中的語法

action.js:存放修改state變量的異步方法
在組件中引用,不使用mapXXXXX輔助函數(shù)情況下
state和getter:直接在computed計算屬性中使用
computed: {
count () {
return this.$store.state.count
},
hasUnit () {
return this.$store.getters.hasUnit
}
}mutations和actions則被作為事件調(diào)用,可以放在methods,watch等等,有事件調(diào)用的時候都可以
methods: {
add () {
this.$store.commit('increment') // mutations使用commit調(diào)用
},
asyncAdd () {
this.$store.dispatch('increment') // actions使用dispatch調(diào)用
}
}通過mapXXXX輔助函數(shù)來引用
先確保組件中已導(dǎo)入vuex的相應(yīng)輔助函數(shù):用到哪個導(dǎo)入哪個
import { mapState } from 'vuex'
import { mapGetters } from 'vuex'
import { mapMutations } from 'vuex'
import { mapActions } from 'vuex'
//導(dǎo)入多個:ES6模塊
import { mapState, mapGetters } from 'vuex'然后根據(jù)你的需求使用對應(yīng)的輔助函數(shù),前提是已經(jīng)導(dǎo)入
computed: {
...mapState([
'count'
]),
...mapGetters([
'addUnit'
])
}
//使用this.count, this.addUnitmethods: {
...mapMutations([
'increment'
]),
...mapActions({
addNum: 'increment'
})
add () {
this.increment()
}
}
//可以被當做事件調(diào)用,為了避免命名沖突,可以使用重命名,需要使用{}而不是[]模塊:Module
當項目很大的時候,很多版塊在一起容易混淆,希望每個版塊的信息歸每個版塊下面,易于維護
//局部模塊引用的第一個參數(shù)state都是自己模塊里的
const moduleA = {
state: { ... },
mutations: { ... },
actions: { ... },
getters: { ... }
}
const moduleB = {
state: { ... },
mutations: { ... },
actions: { ... }
}
const store = new Vuex.Store({
modules: {
a: moduleA,
b: moduleB
}
})以moduleA為例 ,可以新建一個JS文件,然后通過import導(dǎo)入到總的vuex管理中
const state = {
count: 1
};
const getters = {
get(state) {
return state.count;
}
};
const mutations = {
add(state) {
state.count++;
},
reduce(state) {
state.count--;
}
};
const actions = {
add: ({ commit }) => {
commit("add");
},
reduce: ({ commit }) => {
commit("reduce");
}
};
export default {
namespaced: true, // 使該模塊帶有命名空間,使它在命名上會按路徑進行命名 如moduleA/state
state,
getters,
mutations,
actions
};在使用了模塊后,我們可以通過以下方式獲取,這里只用了輔助函數(shù)展示
computed: {
...mapState({
count: state => state.moduleA.count
}),
...mapGetters({
get: "moduleB/get"
})
}methods: {
...mapMutations(
'moduleA', {addA:'add', reduceB:'reduce'} // 重命名就用對象包裹
),
...mapActions(
'moduleA', ['add', 'reduce']
)
}如果想引用根目錄下的狀態(tài),需要傳入第三個參數(shù)
const moduleA = {
// ...
getters: {
addUnit (state, getters, rootState) { //rootState為根下的狀態(tài)
return state.count + rootState.count
}
}
}以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue.js組件tree實現(xiàn)省市多級聯(lián)動
這篇文章主要為大家詳細介紹了Vue.js組件tree實現(xiàn)省市多級聯(lián)動的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12
antd vue表格可編輯單元格以及求和實現(xiàn)方式
這篇文章主要介紹了antd vue表格可編輯單元格以及求和實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04
axios的interceptors多次執(zhí)行問題解決
這篇文章主要為大家介紹了axios中interceptors多次執(zhí)行問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06
使用vue + less 實現(xiàn)簡單換膚功能的示例
下面小編就為大家分享一篇使用vue + less 實現(xiàn)簡單換膚功能的示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02

