mapState,mapMutations的使用方式

组件上的引入:

import { mapState, mapMutations } from "vuex";

mapState使用有两种方式,如下

  computed: {
    lang_() {
      return this.$store.state.loginInfo.lang;   //附带上store的直接使用方式
    },
    //下述中的 ... 是拓展运算符
    // 使用 [] 是解构赋值

    ...mapState({
      lang: state => state.loginInfo.lang   //这是mapState的使用方式
    })
    //需要进一步处理的话,用下面方式写
    ...mapState({
      infoObj: state => {
        let info = state.accountInfo.infoObj;
        return self.formSexToStr(info);
      }
    }),
  },

mapActionsmapMutations使用方式都是一样的,如下,放到methods中

methods: {
    ...mapMutations(["SETLOGINSTATE", "setUserInfo"]),

    ...mapActions([
      'increment',            // 将 `this.increment()` 映射为 `this.$store.dispatch('increment')`
    ]),
    ...mapActions({
      add: 'increment'       // 将 `this.add()` 映射为 `this.$store.dispatch('increment')`
    })
}


使用栗子,先是放入localStorage,后执行store里面定义的方法,从而改变state

//方法中要是有先存储,就不用这里
localStorage.setItem("infoObj", JSON.stringify(this.form));   
//存放到store
this.setUserInfo({ infoObj: this.form });

--by Affandi ⊙▽⊙

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容