Vue学习笔记 store 找不到Module里的action

子模块user.ts

const moduleUser = {
  state: (): {
    username: "",
  }),
  mutations: {
 },
  actions: {
    login({ commit }: any) {
      console.log("login");
    },
  },
  getters: {},
};

export default moduleUser;

store 的index.ts

import { createStore } from "vuex";
import moduleUser from "./user";
const store = createStore({
  state: {},
  getters: {

  },
  mutations: {},
  actions: {},
  modules: {
    user: moduleUser,
  },
});
export default store;

在调用过程中提示未找到"user/login"
在这里插入图片描述

 const store = useStore();
 const submitForm = (formEl: FormInstance | undefined) => {
      if (!formEl) return;
      formEl.validate((valid) => {
        if (valid) {

          store.dispatch("user/login");

        } else {
          return false;
        }
      });
    };

原因是没是添加命名空间
namespaced: true,

const moduleUser = {
 namespaced: true,
  state: (): {
    username: "",
  }),
  mutations: {
 },
  actions: {
    login({ commit }: any) {
      console.log("login");
    },
  },
  getters: {},
};

export default moduleUser;

你可能感兴趣的:(Vue,vue.js)