国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

詳解vuex的簡單使用

2019-11-19 14:11:17
字體:
供稿:網(wǎng)友

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)出mutationexport 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,不同在于:

  1. Action 提交的是 mutation,而不是直接變更狀態(tài)。
  2. Action 可以包含任意異步操作。
  3. 在一個動作中多次改變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í)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 延庆县| 弥勒县| 乐至县| 泸溪县| 公安县| 鸡泽县| 集安市| 祁门县| 麻江县| 安图县| 白水县| 南投市| 阳西县| 和静县| 金溪县| 神木县| 永仁县| 同江市| 秦皇岛市| 河北省| 平定县| 海宁市| 衡水市| 灵石县| 大埔县| 新乐市| 来宾市| 墨脱县| 弋阳县| 德兴市| 徐闻县| 叙永县| 厦门市| 汨罗市| 邵东县| 赞皇县| 四川省| 农安县| 澳门| 南溪县| 赞皇县|