詳解vuex的簡單使用
1 目錄的配置
根據(jù)官方推薦在src目錄里面創(chuàng)建store目錄
2 創(chuàng)建store里面的文件
根據(jù)官方推薦創(chuàng)建 actions.js, getters.js,index.js, mutations.js, mutations-types.js, state.js
2.1 state.js
state.js: 是vuex的單一狀態(tài)數(shù),用一個對象就包含了全部的應(yīng)用層級狀態(tài)。至此它便作為一個『唯一數(shù)據(jù)源(SSOT)』而存在。這也意味著,每個應(yīng)用將僅僅包含一個 store 實例。單一狀態(tài)樹讓我們能夠直接地定位任一特定的狀態(tài)片段,在調(diào)試的過程中也能輕易地取得整個當(dāng)前應(yīng)用狀態(tài)的快照。(用來管理所有vuex狀態(tài)數(shù)據(jù))
/*
* 是vuex的單一狀態(tài)數(shù),用一個對象就包含了全部的應(yīng)用層級狀態(tài)
* 單一狀態(tài)樹讓我們能夠直接地定位任一特定的狀態(tài)片段,在調(diào)試的過程中也能輕易地取得整個當(dāng)前應(yīng)用狀態(tài)的快照。(用來管理所有vuex狀態(tài)數(shù)據(jù))
*
*/
const state = {
// 城市狀態(tài),用來管理城市
city: {},
cityList: [],
fullScreen: true,
palyer: false
};
export default state;
2.2 mutations-types.js 存取mutations相關(guān)的字符常量 (一些常量)
/* * 存取mutations相關(guān)的字符常量 * */ // 定義常量并導(dǎo)出 export const SET_CITY = 'SET_CITY'; export const SET_PLAY = 'SET_PLAY'; export const SET_FULL_SCREEN = 'SET_FULL_SCREEN'; export const SET_CITY_LIST = 'SET_CITY_LIST';
2.3 mutations.js (定義修改的操作)
更改 Vuex 的 store 中的狀態(tài)的唯一方法是提交 mutation。Vuex 中的 mutations 非常類似于事件:每個 mutation 都有一個字符串的 事件類型 (type) 和 一個 回調(diào)函數(shù) (handler)。這個回調(diào)函數(shù)就是我們實際進行狀態(tài)更改的地方,并且它會接受 state 作為第一個參數(shù)
/*
* 更改 Vuex 的 store 中的狀態(tài)的唯一方法是提交 mutation
* Vuex 中的 mutations 非常類似于事件:每個 mutation 都有一個字符串的 事件類型 (type) 和 一個 回調(diào)函數(shù) (handler)。這個回調(diào)函數(shù)就是我們實際進行狀態(tài)更改的地方,并且它會接受 state 作為第一個參數(shù)
*/
// 導(dǎo)入mutation-type.js里面所有的常量
import * as types from './mutation-types';
// 定義一個mutation可以供設(shè)置和修改值
const mutations = {
/*
* 1 表達式作為屬性表達式放在方括號之內(nèi)
* 2 形參state (獲取當(dāng)前狀態(tài)樹的state)
* 3 形參city,是提交mutation時傳的參數(shù)
* 4 使用mutation便于書寫方便
* 5 這個操作相當(dāng)于往state.js里面寫入city
*/
[types.SET_CITY](state, city) {
state.city = city;
},
[types.SET_CITY_LIST](state, list) {
state.cityList = list;
},
[types.SET_FULL_SCREEN](state, flag) {
state.fullScreen = flag;
},
[types.SET_PLAY](state, palyState) {
state.palyer = palyState;
}
};
// 導(dǎo)出mutation
export default mutations;
2.4 getters.js 有時候我們需要從 store 中的 state 中派生出一些狀態(tài)。
mapGetters 輔助函數(shù)僅僅是將 store 中的 getters 映射到局部計算屬性
/* * 有時候我們需要從 store 中的 state 中派生出一些狀態(tài) * 這里的常量主要是對state里面做一些映射 * mapGetters 輔助函數(shù)僅僅是將 store 中的 getters 映射到局部計算屬性 */ // 對state里面的屬性做一些映射 export const city = state => state.city; // 箭頭函數(shù)的簡寫 export const cityList = state => state.cityList; export const fullScreen = state => state.fullScreen; export const palyer = state => state.palyer;
2.5 actions.js
Action 類似于 mutation,不同在于:
- Action 提交的是 mutation,而不是直接變更狀態(tài)。
- Action 可以包含任意異步操作。
- 在一個動作中多次改變mutation可以封裝一個action
/*
* actions類似mutation
* 區(qū)別:
* 1:action提交的是mutation
* 2:action可以包含任意異步操作
*/
/*
* 使用:
* 1:在一個動作中多次改變mutation可以封裝一個action
*/
import * as types from './mutation-types';
export const selectList = function ({commit, state}, {list, index}) {
commit(types.SET_CITY_LIST, list);
commit(types.SET_PLAY, false);
commit(types.SET_FULL_SCREEN, true);
};
2.6 index.js入口
/*
* 入口
*/
import Vue from 'vue';
import Vuex from 'vuex';
// import * as obj from 'xxxx'; 會將xxxx中所有export導(dǎo)出的內(nèi)容組合成一個對象返回。
import * as actions from './actions';
// 拿到getters里面的映射
import * as getters from './getter';
import state from './state';
import mutations from './mutations';
import createdLogger from 'vuex/dist/logger';
Vue.use(Vuex);
const debug = process.env.NODE_ENV != 'production';
export default new Vuex.Store({
actions,
getters,
state,
mutations,
strict: debug,
plugins: debug ? [createdLogger()] : []
});
3 使用
3.1 在mian.js注冊store
在main.js里面new的Vue的實例里面注冊store
3.2 寫入值,要在組件中引入mapMutations的語法糖
引入語法糖
import {mapMutations, mapActions} from 'vuex';
在methods里面mapMutations 輔助函數(shù)將組件中的 methods 映射為 store.commit 調(diào)用
...mapMutations({
// 這里和mutation里面的常量做一個映射
setCity: 'SET_CITY'
})
在需要的地方寫入值
this.setCity(city);
3.3獲取值
獲得vuex中的值,要在組件中引入mapGetters(mapGetters 輔助函數(shù)僅僅是將 store 中的 getters 映射到局部計算屬性)
引入mapGetters
import {mapGetters} from 'vuex';
在computed計算屬性里面使用對象展開運算符將 getters 混入 computed 對象中
computed: {
// 這里面的city映射的是state.js里面的city
// 可以映射多個值
...mapGetters([
'city',
'cityList',
'palyer',
'fullScreen'
])
}
拿到值
created() {
console.log(this.city);
console.log(this.cityList[1]);
console.log(this.palyer);
console.log(this.fullScreen);
}
3.4 action存入值
...mapActions(['selectList'])
在需要存入的地方使用
this.selectList({
list: this.citys,
index: 1
});
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue項目使用jszip和file-saver批量打包壓縮圖片或附件方式
這篇文章主要介紹了vue項目使用jszip和file-saver批量打包壓縮圖片或附件方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03
vue無法加載文件C:\xx\AppData\Roaming\npm\vue.ps1系統(tǒng)禁止運行腳本
這篇文章主要介紹了vue?:?無法加載文件?C:\xx\AppData\Roaming\npm\vue.ps1...系統(tǒng)上禁止運行腳本問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07
vue在mounted拿不到props中傳遞的數(shù)據(jù)問題
這篇文章主要介紹了vue在mounted拿不到props中傳遞的數(shù)據(jù)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03
Vue.js+Layer表格數(shù)據(jù)綁定與實現(xiàn)更新的實例
下面小編就為大家分享一篇Vue.js+Layer表格數(shù)據(jù)綁定與實現(xiàn)更新的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03
打包組件報錯:Error:Cannot?find?module?'vue/compiler-sfc&ap
最近遇到這樣的問題,vue組件庫搭建過程中使用webpack打包組件時報錯,本文給大家分享打包組件報錯:Error:?Cannot?find?module?‘vue/compiler-sfc‘的解決方法,感興趣的朋友一起看看吧2023-12-12

