vuex的使用

50 阅读1分钟

1.模块化 定义模块

image.png

在index中导入注册

image.png

取值

import { mapState } from "vuex";

  ...mapState({
  //取出模块 modoua中的num 值
    user: (state) => state.modoua.num,
   }),
    
  或者 
  this.$store.state.modoua.num   //也可以使用值
    

使用模块方法

import {mapActions } from "vuex";

//解构出modoua模块中  mapActions(异步)  add 方法
...mapActions("modoua", ["add"]),

//需要时调用
  this.add()

非模块情况下 结构index.js中state中数据

import { mapState } from "vuex";


    ...mapState({
      user: (state) => state.haha,
    }),