美文网首页
vue 状态管理vuex

vue 状态管理vuex

作者: 杰出噜 | 来源:发表于2018-12-06 19:29 被阅读0次

    在viwe同级别建立store 文件
    因为srore把 一些功能可以分模块管理,在store中分布定义统一注入;

    const moduleA = {
      state: { ... },
      mutations: { ... },
      actions: { ... },
      getters: { ... }
    }
    
    const moduleB = {
      state: { ... },
      mutations: { ... },
      actions: { ... }
    }
    
    const store = new Vuex.Store({
      modules: {
        a: moduleA,
        b: moduleB
      }
    })
    store.state.a.xxx // -> moduleA 的状态
    store.state.b.xxx // -> moduleB 的状态
    

    state 状态值管理变量管理

    //store 中注册state;
    const app={
    state :{
    a:'xxx',
    b:'xxxx'
    }
    }
    //在页面中调用
    this.$store.state.a
    // 在单独构建的版本中辅助函数为 Vuex.mapState
    import { mapState } from 'vuex'
    
    export default {
      // ...
      computed: mapState({
        // 箭头函数可使代码更简练
        count: state => state.count,
    
        // 传字符串参数 'count' 等同于 `state => state.count`
        countAlias: 'count',
    
        // 为了能够使用 `this` 获取局部状态,必须使用常规函数
        countPlusLocalState (state) {
          return state.count + this.localCount
        }
      })
    }
    //如果computer如果有多个键值,可以以对象的形势表示
    computed:{...mapState(...)}
    

    //如果对state的状态进行计算并且在多地方应用时间,(return this.$store.state.todos.filter(todo => todo.done).length)应当在srore 中注册getters;

    //注册方法getters
    const app={
     state: {
        todos: [
          { id: 1, text: '...', done: true },
          { id: 2, text: '...', done: false }
        ]
      },
      getters: {
        doneTodos: state => {
          return state.todos.filter(todo => todo.done)
        }
      }
    }
    //页面中调用
    this.$store.getters.doneTodos;
    
    

    Getter 也可以接受其他 getter 作为第二个参数:

    getters: {
      // ...
      doneTodosCount: (state, getters) => {
        return getters.doneTodos.length
      }
    }
    store.getters.doneTodosCount // -> 1
    //也可以传递任何一个页面的参数
    getters: {
      // ...
      getTodoById: (state) => (id) => {
        return state.todos.find(todo => todo.id === id)
      }
    }
    store.getters.getTodoById(2) // -> { id: 2, text: '...', done: false }
    

    更改 Vuex 的 store 中的状态的唯一方法是提交 mutation。Vuex 中的 mutation 非常类似于事件:每个 mutation 都有一个字符串的 事件类型 (type) 和 一个 回调函数 (handler)。这个回调函数就是我们实际进行状态更改的地方,并且它会接受 state 作为第一个参数:

    const store = new Vuex.Store({
      state: {
        count: 1
      },
      mutations: {
        increment (state) {
          // 变更状态
          state.count++
        }
      }
    })
    //传递的当前模块中state;
     mutations: {
        increment (state,pra) {
          // 变更状态
          state.count=pra+10;
        }
      }
    //页面组件中调用
    this.$store.commit('increment', 10)
    

    对组件中的方法调用必须是使用commit 来调用mutations中的方法,实现数据的可追踪,
    在大多数情况下,载荷应该是一个对象,这样可以包含多个字段并且记录的 mutation 会更易读:

    this.$store.commit('increment',{count:1})
     mutations: {
        increment (state,prams) {
          // 变更状态
          state.count=prams.count+10;
        }
      }
    

    mutations type名称可以独立出来,增加代码维护可读性

    // mutation-types.js
    export const SOME_MUTATION = 'SOME_MUTATION'
    // store.js
    import Vuex from 'vuex'
    import { SOME_MUTATION } from './mutation-types'
    
    const store = new Vuex.Store({
      state: { ... },
      mutations: {
        // 我们可以使用 ES2015 风格的计算属性命名功能来使用一个常量作为函数名
        [SOME_MUTATION] (state) {
          // mutate state
        }
      }
    })
    

    在组件中提交 Mutation
    你可以在组件中使用 this.$store.commit('xxx') 提交 mutation,或者使用 mapMutations 辅助函数将组件中的 methods 映射为 store.commit 调用(需要在根节点注入 store)。

    import { mapMutations } from 'vuex'
    
    export default {
      // ...
      methods: {
        ...mapMutations([
          'increment', // 将 `this.increment()` 映射为 `this.$store.commit('increment')`
    
          // `mapMutations` 也支持载荷:
          'incrementBy' // 将 `this.incrementBy(amount)` 映射为 `this.$store.commit('incrementBy', amount)`
        ]),
        ...mapMutations({
          add: 'increment' // 将 `this.add()` 映射为 `this.$store.commit('increment')`
        })
      }
    }
    

    mutations 中方法必须是同步的如果想实现调用异步操作可以调用Action;
    Action 定义;

    const store = new Vuex.Store({
      state: {
        count: 0
      },
      mutations: {
        increment (state) {
          state.count++
        }
      },
      actions: {
        increment (context) {
          context.commit('increment')
        }
      }
    })
    //context 是store的上下文
    

    Action 函数接受一个与 store 实例具有相同方法和属性的 context 对象,因此你可以调用 context.commit 提交一个 mutation,或者通过 context.statecontext.getters 来获取 state 和 getters。当我们在之后介绍到 Modules 时,你就知道 context 对象为什么不是 store 实例本身了。

    实践中,我们会经常用到 ES2015 的 参数解构 来简化代码(特别是我们需要调用 commit 很多次的时候):

    actions: {
      increment ({ commit }) {
        commit('increment')
      }
    }
    //在页面组件中调用
    this.$store.dispatch('increment')
    //也可以传递参数
    // 以载荷形式分发
    store.dispatch('incrementAsync', {
      amount: 10
    })
    actions: {
      incrementAsync({ commit },params) {
        commit('increment,params.amount')
      }
    }
    

    在组件中分发 Action
    你在组件中使用 this.$store.dispatch('xxx') 分发 action,或者使用 mapActions 辅助函数将组件的 methods 映射为 store.dispatch 调用(需要先在根节点注入 store):

    import { mapActions } from 'vuex'
    
    export default {
      // ...
      methods: {
        ...mapActions([
          'increment', // 将 `this.increment()` 映射为 `this.$store.dispatch('increment')`
    
          // `mapActions` 也支持载荷:
          'incrementBy' // 将 `this.incrementBy(amount)` 映射为 `this.$store.dispatch('incrementBy', amount)`
        ]),
        ...mapActions({
          add: 'increment' // 将 `this.add()` 映射为 `this.$store.dispatch('increment')`
        })
      }
    }
    

    [参考文件](https://vuex.vuejs.org/zh/guide/state.html

    相关文章

      网友评论

          本文标题:vue 状态管理vuex

          本文链接:https://www.haomeiwen.com/subject/vxcacqtx.html