javascript - vue.js:错误未知动作类型?
问题描述
我创建了我的商店 store/user.js
export const state = () => ({
user: {},
});
export const mutations = {
};
export const actions = {
AUTH ({commit},{email, password}){
console.log('email, password =', email, password)
}
};
export const getters = {};
零件:
<template>
<form @submit.prevent="AUTH(model)">
<input type="text" required v-model.lazy = "model.email">
<input type="password" required v-model.lazy = "model.password" >
</template>
<script>
import { mapActions } from 'vuex'
export default {
data() {
return {
model:{
email:" " ,
password:" "
}
}
},
methods: {
...mapActions(['AUTH']),
}
}
在我的组件中,我试图从模块执行 vuex 操作,但我收到错误,即使定义了此操作:
unknown action type: AUTH,
我对问题一无所知。
index.js
import Vue from 'vue'
import Vuex from 'vuex'
import user from './modules/user.js'
Vue.use(Vuex);
const store = new Vuex.Store({
modules: {
user
}
})
解决方案
您需要使用createNamespacedHelpers
:
import { createNamespacedHelpers } from 'vuex'
const { mapState, mapActions } = createNamespacedHelpers('users')
否则,映射助手需要完整的模块命名空间:
...mapActions([
'users/AUTH'
])
// if you are only using one module in the component
...mapActions('users', [
'AUTH'
])
纽斯特
您正在混合经典模式和模块模式。使用模块模式时,Nuxt 从index.js
文件中创建存储实例。您只需导出状态、getter、突变和操作。状态应作为函数导出:
export const state = () => ({
foo: 0,
bar: 1
})
目录中的任何文件都store
将被视为一个模块,Nuxt 会自动将其注册为命名空间模块。
- store
-- index.js // the store
-- users.js // module 'users'
-- foo.js // module 'foo'
用户模块看起来是正确的。
对您的组件进行以下更改:
// template
<form @submit.prevent="submitForm">
// script
methods: {
...mapActions({
auth: 'users/AUTH'
}),
submitForm () {
this.auth(this.model)
}
}
推荐阅读
- android - 需要帮助来解析血糖仪 android BLE 上的膳食类型
- firebase - 扑。当我从未来函数收到值时,我的 UI 没有更新
- sql-server - 选择具有不同列的聚合值
- kotlin - 预期主构造函数调用
- ios - Firebase 动态链接不缩短 URL
- php - 我正在使用引导程序创建导航栏,但它不起作用,但是其他类的引导程序工作正常
- bevy - Bevy 如何在 2D 中渲染三角形(或多边形)
- google-colaboratory - 如何将字符串转换为日期时间作为列标题
- python - import _tkinter # 如果失败,您的 Python 可能未配置为在 python 3.8 中出现 Tk 错误
- spring-boot - 无法配置数据源:未指定“url”属性,无法配置嵌入式数据源。Gradle 项目中的错误