Issues of vuex

1, mutation is used to synchronize the data to make processing services (vuex the store data is the only way to change mutation), mutation induction can not be the most important is for debugging, not the state can not be modified;
2, Action processing asynchronous data eventually submitted to database, submitting a mutation, Action can contain any asynchronous operation, rather than directly change state.
3, Actions to support the same load mode and manner distribute objects:

// 以载荷形式分发
store.dispatch('incrementAsync', {
  amount: 10
})

// 以对象形式分发
store.dispatch({
  type: 'incrementAsync',
  amount: 10
})

4, mapState effect: when a component needs to acquire more state time, these states are declared to calculate the property will be some duplication and redundancy, can use mapState helpers help us generate calculate property,

computed: mapState({
    count: 'count', // 第一种写法
    sex: (state) => state.sex, // 第二种写法
    from: function (state) { // 用普通函数this指向vue实例,要注意
      return this.str + ':' + state.from
    },
    // 注意下面的写法看起来和上面相同,事实上箭头函数的this指针并没有指向vue实例,因此不要滥用箭头函数
    // from: (state) => this.str + ':' + state.from
    myCmpted: function () {
      // 这里不需要state,测试一下computed的原有用法
      return '测试' + this.str
    }
  }),
代码示例来源:闲人王昱珩 
也可以使用ES6中的扩展运算符”…”,【不知道扩展运算符的朋友可自行百度】	
	   computed:{
            ...mapState(['count','info']),
            // count(){
            //     return this.$store.state.count
            // },
            //  info(){
            //     return this.$store.state.info
            // }
    
        },

5, mapMutations / mapActions is to bind the mutation / action function to methods which, when the normal method of adjusting transmission parameters inside.
6, prior to the need to deploy "config / index.js / assetsPublicPath: ' /'", in the modified path configuration server.
7, "getter" (can be considered to calculate the property store). As calculated as property, getter return value will be based on its dependencies are cached, and only when it is dependent on the value has changed will be re-calculated;
Parameters: accepting state as its first parameter; can also accept other getter as the second parameter;
noted, the cache is part of a getter Vue wherein a responsive system in the accessed attribute; getter when accessed through methods, to call every time, but the results are not cached.
mapGetters auxiliary function only in the map store to a local computing getter properties:

Constantly update. . .

发布了19 篇原创文章 · 获赞 11 · 访问量 1万+

Guess you like

Origin blog.csdn.net/weixin_43392673/article/details/88024816