vuex 基本入门和使用(二)

发布时间:2019-08-19 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了vuex 基本入门和使用(二)脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

vuex 基本入门和使用(二)

vuex 版本为^2.3.1,按照我自己的理解来整理vuex。

关于 state

每个vuex 应用只有一个 Store 实例,所以使用起来不会太复杂,对于定位错误状态和操作会很方便。

简单用法:在vuex 的计算属性中返回vuex 的状态

最基本的使用方式,通过在vue文件里面初始化 vuex 的 store 来进行操作 vuex 的数据:如下例子:

// 在组件里面使用 vuex
// 初始化 vuex 实例 
const store = new Vuex.Store({
  state: {
    count: 0
  },
  mutations: {
      increment: state => state.count++,
    decrement: state => state.count--
  }
})

// 创建一个 Counter 组件
const Counter = {
  template: `<div>{{ count }}</div>`,
  computed: {
    count () {
        // 直接返回 state
      return store.state.count
    }
  }
}

// 初始化 vue 实例
const app = new Vue({
  el: '#app',
  components: { Counter },
  template: `
    <div class="app">
     <button @click="increment">+</button>
     <button @click="decrement">-</button> 
     <counter></counter>
    </div>
  `
  ,
   methods: {
    increment () {
      store.commit('increment')
    },
    decrement () {
        store.commit('decrement')
    }
  }
})
  • 每当 store.state.count 变化的时候, 都会重新求取计算属性,并且触发更新相关联的 DOM
  • 然而,这种模式导致组件依赖全局状态单例。在模块化的构建系统中,在每个需要使用 state 的组件中需要频繁地导入,并且在测试组件时需要模拟状态。
我以为,当项目发展到多个模块,多个组件和子组件混合的时候,在这种场合,单纯的值传递方式会很麻烦,因为组件或者模块之间的变量是独立的,对于一些全局使用的属性类似 token,cookie 之类的东西,或者是一些多个模块之间共享的属性。

所以 vuex 提供一个新的方式来将 vuex 的 store 存放到根组件下,通过 store 选项,将store从根组件“注入”到每一个子组件中(需调用 Vue.use(Vuex)):

// 初始化 vuex的 store(可以将这个放到其他文件里面去)
const store = new Vuex.Store({
  state: {
    count: 0
  },
  mutations: {
      increment: state => state.count++,
    decrement: state => state.count--
  }
})

// 在初始化 vue的时候注册 store(store 即是 vuex 的 store)
const app = new Vue({
  el: '#app',
  // 把 store 对象提供给 “store” 选项,这可以把 store 的实例注入所有的子组件
  store,
  components: { Counter }, // 子组件
  template: `
    <div class="app">
      <counter></counter>
    </div>
  `
})

通过在根实例中注册 store 选项,该 store 实例会注入到根组件下的所有子组件中,且子组件能通过 this.$store 访问到。让我们更新下 Counter 的实现:

// 这是子组件 Counter
const Counter = {
  template: `<div>{{ count }}</div>`,
  computed: {
    count () {
        //  通过this.$store能够访问到 store 并且获取到 state
      return this.$store.state.count
    }
  }
}

通过这种方式可以在实际应用中,将 vuex 的初始化分离出去其他模块化文件,然后在 vue初始化的引用相关 vuex 的文件即可,然后通过this.$store全局调用 vuex 的 store 来实现数据存储。

我整理了一下完整的例子,这是 jsrun的例子:
http://jsrun.net/qWqKp

高级用法:mapState 辅助函数

当一个组件需要获取多个状态时候,将这些状态都声明为计算属性会有些重复和冗余。为了解决这个问题,我们可以使用 mapState 辅助函数帮助我们生成计算属性,让你少按几次键。(其实就是自动转换了一些语法输出)

import { mapState } From 'vuex'需要先引入才可以使用

mapState使用前后对比:

// 不使用MapState时:
computed: {
    count () {
      return this.$store.state.count
    }
  }
// 使用mapState时:
computed: mapState({
       count: state => state.count,
})

如果在大批量的类似这种的计算属性的话,使用 mapState 会更加便捷,而且不只是普通字符串,函数也可以转换,确实是比较方便的。

这里会有一个疑问,mapState到底做了什么事情,怎么将代码转为 vue 能识别的代码?所以需要参考 vuex 代码中关于mapState的部分:(我找了当前 vuex 版本3.01的源代码:https://github.com/vuejs/vuex/blob/e821f1bf5b818992d7f34c03029ce2ded1f52a75/dist/vuex.esm.js

这是normalizeMap的代码:

function normalizeMap (map) {
  // 判断是否数组,并且最终返回也是一个数组
  return Array.isArray(map)
    // 是数组就直接 map 循环
    ? map.map(key => ({ key, val: key }))
    // 是对象就将 key拿出来,然后再进行 map 循环
    : Object.keys(map).map(key => ({ key, val: map[key] }))
}

这是mapState的代码:

VAR mapState = normalizenamespace(function (namespace, states) {
  var res = {}; // 这是一个对象类型
  // 将 state 通过normalizeMap格式化变成一个数组,数组元素是一个对象
  normalizeMap(states).foreach(function (ref) {
    var key = ref.key;// 将数组元素对象解出来,先保存起来被后面使用
    var val = ref.val;
    // 组成一个新数组,以 key 为 key,值是一个函数mappedstate
    res[key] = function mappedState () {
      var state = this.$store.state; // 将本身 vuex 的 store 的 state 和 getters 保存
      var getters = this.$store.getters;
      // 先不看 namespace 部分
      // ......
      // 这个函数里面会判断真正的值 ref.val 是函数还是普通值
      return typeof val === 'function'
        ? val.call(this, state, getters) // 函数会被直接执行,并且传入 vuex 的state 和 getters
        : state[val] // 值会直接放到  vuex 的state 里面
    };
  });
  return res
});
  • states参数在这里只有2种,一种是对象{},一种是数组[]。因为normalizeMap只做了这2个判断。
  • states 会先通过normalizeMap进行序列化,转换为一个数组,数组元素是{ key, val: key } 这种结构的。
  • 这里需要注意一下:如果传入的是对象里面只有函数,如下例的countPlusLocalState,那么被 object.keys获取的 key 是函数的名字。
// 例如传入的state 是一个数组,如下:
[
   {
     count: state => state.count,   
   }
]
// 那么被normalizeMap转换后:
// 即转换为{ key, val: key })
[
    { 
     key, // key 是对象{count: state => state.count}
     val: key // val是对象{count: state => state.count}
    },
    //.....
]

//------------------------

// 例如传入的state 是一个对象,如下:
{
  count: state => state.count,
}
// 那么被normalizeMap转换后:
// 即被Object.keys(map).map(key => ({ key, val: map[key] }))处理后
[
    { 
     key, // key 是count,因为被Object.keys提取出来
     val: map[key] //  val 是state => state.count,这里以 key 为键找对象的值
    },
    //.....
]
  • normalizeMap(states)格式化之后会使用 forEach 遍历这个数组:

    • 如果 val 是一个函数,则直接调用这个 val 函数,把当前 store 上的 state 和 getters 作为参数,返回值作为 mappedState 的返回值。
    • 否则直接把 this.$store.state[val] 作为 mappedState 的返回值。
// 被 foreach 遍历,继续用上面的例子的state来举例,因为不是函数,所以被直接返回:
res["count"] = this.$store.state['state => state.count']
// 虽然这里是以数组的写法,但是在 js 里面数组的写法也可以用在对象上。

//如果是函数的话,会被执行val.call,并且传入了几个参数(this, this.$store.state, this.$store.getters)
res["countPlusLocalState"] = this.$store.state['state => state.count']
  • 最终能够形成一个新的数组对象结构,并返回。
  • 因为最终生成的数据就是 computed 的数据格式,所以直接将这个对象传给 computed 就可以直接使用了。

这是mapState经过源代码解析前和后的对比:

// states 为对象时候,mapState转换前
computed: mapState({
    count: state => state.count,
    // 传字符串参数 'count' 等同于 `state => state.count`
    countAlias: 'count',
    // 为了能够使用 `this` 获取局部状态,必须使用常规函数
    countPlusLocalState (state) {
          return state.count + this.localCount
    }
})

// states 为对象时候,mapState转换后
computed: { 
    count() { 
       // 直接转换为一般的计算属性的使用方式
        return this.$store.state.count 
    },
    countAlias() { 
        // 也是转为一般的计算属性的使用方式,只不过有指定名字的会使用中括号括起来
        return this.$store.state['count']
    }, 
    countPlusLocalState() { 
        // 因为是函数,所以会被直接执行,并且传入了当前 store 上的 state 和 getters作为参数
        //(但这里没使用 getters)
        return this.$store.state.count + this.localCount 
    } 
}

组件仍然保有局部状态

使用 Vuex 并不意味着你需要将所有的状态放入 Vuex。虽然将所有的状态放到 Vuex 会使状态变化更显式和易调试,但也会使代码变得冗长和不直观。如果有些状态严格属于单个组件,最好还是作为组件的局部状态。你应该根据你的应用开发需要进行权衡和确定。

关于 getter

Vuex 允许我们在 store 中定义“getter”(可以认为是 store 的计算属性),就像计算属性一样,getter 的返回值会根据它的依赖被缓存起来,且只有当它的依赖值发生了改变才会被重新计算。

// 初始化 getter
const store = new Vuex.Store({
  state: {
    todos: [
      { id: 1, text: '...', done: true },
      { id: 2, text: '...', done: false }
    ]
  },
  getters: { // 这就是 getters
    doneTodos: state => {
      return state.todos.filter(todo => todo.done)
    }
  }
})

// 使用getter
store.getters.doneTodos // -> [{ id: 1, text: '...', done: true }]
// 或者可以this.$store.getters.xxxx 这样使用。
// 可以在第二个参数里面传一个 getter 作为参数
getters: {
  // ...
  doneTodosCount: (state, getters) => {
      // 传入了之前设置的doneTodos的 getters,所以直接使用了doneTodos
    return getters.doneTodos.length
  }
}

store.getters.doneTodosCount // -> 1

// 让 getter 返回一个函数,来实现给 getter 传参。在你对 store 里的数组进行查询时非常有用。
getters: {
  // ...
  getTodoById: (state) => (id) => { // 返回一个函数
    return state.todos.find(todo => todo.id === id)
  }
}

// 对返回的函数传入参数来使用
store.getters.getTodoById(2) // -> { id: 2, text: '...', done: false }

mapGetters 辅助函数

mapGetters 辅助函数仅仅是将 store 中的 getter 映射到局部计算属性。

参考 vuex 源代码,类似的处理也是跟...mapState类似:

export function mapGetters(getters) {
    const res = {}
    // 先格式化,然后再处理
    normalizeMap(getters).forEach(({key, val}) => {
        res[key] = function mappedGetter() {
            if (!(val in this.$store.getters)) {
                console.error(`[vuex] unknown getter: ${val}`)
            }
            return this.$store.getters[val]
        }
    })
    return res
}
相比 mapState 来说,他简单一点

唯一的区别就是它的 val 不能是函数,只能是一个字符串,而且会检查 val in this.$store.getters 的值,如果为 false 会输出一条错误日志。

关于三个点...mapstate 处理

其实三个点就是es6扩展运算符

可以将一个数组转为用逗号分隔的参数序列,也可以将对象进行展开,如果应用到 mapstate 身上就是:

需要注意:vue的 computed 是对象,里面的属性是对象属性。
computed: {
  // 一般 computed 对象属性
  now: function () {
    return Date.now()
  }
  // 使用对象展开运算将此对象混入到外部对象中
  ...mapGetters([
      'doneTodosCount',
      'anotherGetter',
      // ...
    ])

  // 转换后是这样,跟一般 computed 对象属性差不多
  doneTodosCount:function(){},
  anotherGetter:function(){}
}

参考:

脚本宝典总结

以上是脚本宝典为你收集整理的vuex 基本入门和使用(二)全部内容,希望文章能够帮你解决vuex 基本入门和使用(二)所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。