vue-antd-admin icon indicating copy to clipboard operation
vue-antd-admin copied to clipboard

Vuex TypeError: Cannot read properties of undefined (reading 'commit')

Open Sylor-huang opened this issue 4 years ago • 2 comments

Describe the bug

  • [ ] 登录后,保存用户信息时,发生报错:
vuex.esm.js?2f62:926 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'commit')
    at VueComponent.mappedMutation (vuex.esm.js?2f62:926)
    at VueComponent.afterLogin (signup_form.vue?5808:153)

image

  • [ ] 相关代码:

import { send_emails, sign_up } from "@/services/user";
import { setAuthorization } from "@/utils/request";
import { mapMutations } from "vuex";

...

methods: {
...mapMutations("email", ["setUser", "setPermissions", "setRoles"]),
...
// loginRes 的数据返回是对的,获取获取到 user, 但是在this.setUser时,发生了上面的报错
afterLogin(res) {
      const loginRes = res.data;
      const { user, permissions, roles } = loginRes;
        this.setUser(user);
        this.setPermissions(permissions);
        this.setRoles(roles);
        setAuthorization({
          token: loginRes.x_token,
          expireAt: new Date(loginRes.expireAt),
        });
        this.$emit("onsuccess")
    },
}
  • [ ] 版本
"name": "vue-antd-admin",
"version": "0.7.4",
"vuex": "^3.4.0"
 "vue": "^2.6.11",

Sylor-huang avatar Oct 11 '21 06:10 Sylor-huang

在main.js 里设置:

Vue.prototype.$store = store;

是可以正常工作的,不知道这样修改是否可以,麻烦大佬帮我看下,非常感谢~

Sylor-huang avatar Oct 11 '21 06:10 Sylor-huang

store 开启 nameSpace 命名空间后,应该传入 store/modules/account.js 的 account的命名, 所以你填写的 email 作为命名空间应该写错了

...mapMutations("account", ["setUser", "setPermissions", "setRoles"]),

CracKerMe avatar Jan 13 '22 03:01 CracKerMe