一罚随、什么是vuex
vuex是適用于在Vue項(xiàng)目開發(fā)時(shí)使用的狀態(tài)管理工具壤靶。
二线梗、vuex的作用
試想一下椰于,如果在一個(gè)項(xiàng)目開發(fā)中頻繁的使用組件傳參的方式來同步data中的值,一旦項(xiàng)目變得很龐大仪搔,管理和維護(hù)這些值將是相當(dāng)棘手的工作瘾婿。為此,Vue為這些被多個(gè)組件頻繁使用的值提供了一個(gè)統(tǒng)一管理的工具——VueX烤咧。在具有VueX的Vue項(xiàng)目中偏陪,我們只需要把這些值定義在VueX中,即可在整個(gè)Vue項(xiàng)目的組件中使用煮嫌。
三笛谦、vuex的安裝
1、安裝指令:
npm install vuex 或者 cnpm install vuex
2昌阿、創(chuàng)建vuex目錄
在項(xiàng)目的src文件夾內(nèi)新建一個(gè)store文件夾饥脑,在該文件夾內(nèi)再創(chuàng)建index.js
此時(shí)你的項(xiàng)目的src文件夾應(yīng)當(dāng)是這樣的
3、開始使用vuex
設(shè)置store中的index.js
import Vue from 'vue'
import Vuex from 'vuex'
import createPersist from 'vuex-localstorage'
Vue.use(Vuex)
//創(chuàng)建vuex構(gòu)造器
export default new Vuex.Store({
state:{
},
mutations:{
},
plugins:[//長久儲(chǔ)存
createPersist(
{namespace:'namespace-for-state'}
)
],
modules:{
}
})
設(shè)置main.js
import store from './store/index'//引入vuex
new Vue({
el: '#app',
store:store,
components: { App },
template: '<App/>'
})
四懦冰、vuex的的使用
在組件中使用Vuex
<template>
<div id='app'>
name:
<h1>{{ $store.state.name }}</h1>
</div>
</template>
或者要在組件方法中使用
methods:{
add(){
console.log(this.$store.state.name)
}
},
VueX中的核心內(nèi)容
在VueX對(duì)象中灶轰,其實(shí)不止有state,還有用來操作state中數(shù)據(jù)的方法集,以及當(dāng)我們需要對(duì)state中的數(shù)據(jù)需要加工的方法集等等成員儿奶。
state 是存放狀態(tài) 和 Vue 實(shí)例中的 data遵循相同的規(guī)則
使用如下:
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state:{ //聲明數(shù)據(jù)
str:'',
obj:{},
arr:[],
num:0,
bool:true/false
},
})
getters 是加工state成員給外界 ,可以認(rèn)為是 store 的計(jì)算屬性框往。getter 的返回值會(huì)根據(jù)它的依賴被緩存起來,且只有當(dāng)它的依賴值發(fā)生了改變才會(huì)被重新計(jì)算闯捎。
使用方法如下:
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state:{ //聲明數(shù)據(jù)
shopCar:[]
},
getters:{ //可以認(rèn)為是 store 的計(jì)算屬性
shopCarPrices(state){
var sum=0
for(var i=0;i<state.shopCar.length;i++){
sum+=state.shopCar[i]
}
return sum
}
}
})
組件中調(diào)用
this.$store.getters.shopCarPrices
mutations是對(duì) state成員操作 椰弊, Vuex 的 store 中的狀態(tài)的唯一方法是提交 mutation。
使用方法如下:
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state:{ //聲明數(shù)據(jù)
name:'helloVueX'
},
mutations:{
amend(state){
state.name = '張三'
}
}
})
而在組件中瓤鼻,我們需要這樣去調(diào)用這個(gè)mutation——例如在App.vue的某個(gè)method中:
this.$store.commit('amend')
使用Mutation傳值
this.$store.commit('amend',15)//傳一個(gè)值時(shí)
this.$store.commit('amend',{age:15,sex:'男'})//傳多個(gè)值時(shí)
接收掛載的參數(shù):
amend(state,val){
state.name = '張三'
console.log(val) // 15或{age:15,sex:'男'}
}
actions 是用來專門進(jìn)行異步操作秉版,最終提交mutation方法。
由于setTimeout是異步操作茬祷,所以需要使用actions
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state:{
key:''
},
mutations: {
updateKey(state,val){
state.key=val
console.log(state.key);
}
},
actions:{
updateKey(state,val){
setTimeout(()=>{
state.commit('updateKey',val)
},10)
}
}
})
在組件中調(diào)用:
this.$store.dispatch('updateKey',10)
modules 模塊化狀態(tài)管理,每個(gè)模塊擁有自己的 state清焕、mutation、action祭犯、getter秸妥、甚至是嵌套子模塊——從上至下進(jìn)行同樣方式的分割。當(dāng)項(xiàng)目龐大沃粗,狀態(tài)非常多時(shí)粥惧,可以采用模塊化管理模式。
在創(chuàng)建的store文件夾中在創(chuàng)建一個(gè)user.js文件最盅。
在store中的index.js文件中引入剛創(chuàng)建好的user.js文件
import user from './user'
在index文件中寫入
modules:{
user
},
創(chuàng)建好的user.js文件中寫入模塊突雪,可以寫如我們需要的核心對(duì)象起惕,語法都是一樣的
比如:
export default {
state:{
username :""
},
mutations:{
add(state,val){
state.username = val
},
},
}
組件中傳值
this.$store.commit('add',res)
this.$store.commit('方法名',值)
組件中調(diào)用
this.$store.state.user.username
user代表的是在這個(gè)user模塊里
username 代表的是在user這個(gè)模塊的state中有一個(gè)叫username 的變量
plugins是在vue中使用一些插件 列如可以使用vuex-localstorage
vuex-localstorage 要先進(jìn)行下載 可以使用
npm install vuex-localstorage或cnpm install vuex-localstorage
在index.js
中引入 vuex-localstorage
import createPersist from 'vuex-localstorage'
使用方法如下:
plugins:[
createPersist({namespace:"namespace-for-state"})
],