• 关于vuex


    官网的话:
      Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化。

      在我看来vuex就是把需要共享的变量全部存储在一个对象里面,然后将这个对象放在顶层组件中供其他组件使用。换种说法,将vue想作是一个js文件、组件是函数,那么vuex就是一个全局变量,只是这个“全局变量”包含了一些特定的规则而已。
      在vue的组件开发中,经常会遇到需要将当前组件的状态传递给其他组件。父子间通信时,我们会采用props + emit 这种方式,但当通信双方不是父子组件甚至压根不存在相关联系,或者一个状态需要共享给多个组件时,就会非常麻烦,数据也会难维护,这对开发不友好。vuex这个时候就很实用,不过在使用vuex之后也带来了更多的概念和框架。

     1 const store = new Vuex.Store({
     2     state: {
     3         name: 'wangjk',
     4         age: 22
     5     },
     6     getters: {
     7         personInfo(state) {
     8             return `My name is ${state.name}, I am ${state.age}`;
     9         }
    10     }
    11     mutations: {
    12         SET_AGE(state, age) {
    13             commit(age, age);
    14         }
    15     },
    16     actions: {
    17         nameAsyn({commit}) {
    18             setTimeout(() => {
    19                 commit('SET_AGE', 18);
    20             }, 1000);
    21         }
    22     },
    23     modules: {
    24         a: modulesA
    25     }
    26 }

    上面的就是一份最基本也是最完整的vuex代码;vuex包含有五个基本的对象:
    (1)state : 存储状态,也就是变量;
    (2)getters : 派生状态,也就是set 、get 中的get , 有两个可选的参数 : state 、getters分别可以获取state中的变量和其他的getters。外部调用方式:store.getters.personInfo()。就和vue的computed差不多;
    (3)mutations : 提交状态修改。也就是set、get中的set,这是vuex中唯一修改state的方式。但不支持异步操作。第一个参数默认是state。外部调用方式:store.commit('SET_AGE',18)。和vue中的methods类似。
    (4)actions : 和mutations类似。不过actions支持异步操作。第一个参数默认是和store具有相同参数属性的对象。外部调用方式:store.dispatch('nameAsyn')
    (5)mudules : store的子模块,内容就相当于是store的一个实例。调用方式和前面介绍的相似,只是要加上当前子模块名,如:store.a.getters.xxx()

    vue-cli中使用vuex的方式

    一般来讲,我们都会采用vue-cli来进行实际的开发,在vue-cli中,开发和调用方式稍微不同。

     1 ├── index.html
     2 ├── main.js
     3 ├── components
     4 └── store
     5 ├── index.js # 我们组装模块并导出 store 的地方
     6 ├── state.js # 跟级别的 state
     7 ├── getters.js # 跟级别的 getter
     8 ├── const.js # 根级别的mutations名称(官方推荐mutions方法名使用大写)
     9 ├── mutations.js # 根级别的 mutation
    10 ├── actions.js # 根级别的 action
    11 └── modules
    12 ├── m1.js # 模块1
    13 └── m2.js # 模块2

    下面为示例:

    state.js

    1 const state = {
    2     name : 'yuandd',
    3     age : 23    
    4 } 
    5 
    6 export default state;

    getter.js(我们一般使用getters来获取state的状态,而不是直接使用state)

     1 export const name = (state) => {
     2     return state.name;
     3 }
     4 
     5 export const age = (state) =>{
     6     return state.age;
     7 }
     8 
     9 export const other = (state) =>{
    10     return 'My name is ${state.name}, I am ${state.age}'
    11 }

    const.js(我们会将所有mutations的函数名放在这个文件里)

    1 const SET_NAME = 'SET_NAME';
    2 const SET_AGE = 'SET_AGE';
    3
    4 export {
    5 SET_NAME,
    6 SET_AGE
    7 }

    mutations.js

     1 import const SET_NAME = 'SET_NAME';
     2 
     3 export default {
     4     [types.SET_NAME](state,name){
     5         state.name = name;
     6     },
     7     [types.SET_AGE](state,age){
     8         state.age = age;
     9     }
    10 }

    actions.js

     1 import {
     2     SET_NAME,
     3     SET_AGE,
     4 }  from "./const"
     5 
     6 export default {
     7     nameAsyn({commit}, {age, name}) {
     8         commit(types.SET_NAME, name);
     9         commit(types.SET_AGE, age);
    10     }
    11 };

    index.js

    1 import state from "./state";
    2 import actions from "./actions";
    3 import mutations from "./mutations";
    4 
    5 export default {
    6     state,
    7     actions,
    8     mutations
    9 }

    最后将store实例挂载到main.js里面的vue上去就好了;

    1 import Router from "./router";
    2 import store from './store';
    3 
    4 new Vue({
    5   Router,
    6   store,
    7   render: h => h(App)
    8 }).$mount('#app')

    在vue组件中使用时,我们通常会使用mapGetters、mapActions、mapMutations,然后就可以按照vue调用methods和computed的方式去调用这些变量或函数,示例如下:

     1 import {mapGetters, mapMutations, mapActions} from 'vuex';
     2 
     3 /* 只写组件中的script部分 */
     4 export default {
     5     computed: {
     6         ...mapGetters([
     7             'name',
     8             'age'
     9         ])
    10     },
    11     methods: {
    12         ...mapMutations({
    13             setName: 'SET_NAME',
    14             setAge: 'SET_AGE'
    15         }),
    16         ...mapActions([
    17             nameAsyn
    18         ])
    19     }
    20 };

       以上就是我对vuex的理解。

  • 相关阅读:
    条件编译中的基本语法
    UITableView中headerView视察滚动的简单实现
    CocoaPods使用简单回顾
    CocoaPods第三方类库管理工具的简单使用
    Xcode中release和debug模式
    转:关于LazyTableImage
    汉字与UTF-8编码之间的转换
    结构体与字符串之间的转换
    MFC中小笔记(二)
    升级 WIN8.1 VC6.0和 Visual Assist 的使用问题
  • 原文地址:https://www.cnblogs.com/ydd3664/p/9984168.html
Copyright © 2020-2023  润新知