Vue.js/Vuex登录:[Vuex]未知操作类型:postLogin。
我对Vuejs相当陌生,但对Vuex更是如此——我了解Vue和Vuex的基本知识。我正在努力让我的登录工作。这就是我得到的错误:Vue.js/Vuex登录:[Vuex]未知操作类型:postLogin。,vue.js,vuejs2,vue-component,vuex,Vue.js,Vuejs2,Vue Component,Vuex,我对Vuejs相当陌生,但对Vuex更是如此——我了解Vue和Vuex的基本知识。我正在努力让我的登录工作。这就是我得到的错误: [vuex] unknown action type: postLogin. 我想成功地点击api。我已经在后端创建了用户(使用django)。所以我可以通过管理员门户登录。尝试通过API/axios使用vue前端登录 这里是我的component.vue代码: <v-card-text> <
[vuex] unknown action type: postLogin.
我想成功地点击api。我已经在后端创建了用户(使用django)。所以我可以通过管理员门户登录。尝试通过API/axios使用vue前端登录
这里是我的component.vue代码:
<v-card-text>
<v-form @submit.prevent="postLogin(email, password)">
<v-text-field v-model="email" prepend-icon="person" name="login" label="Login" type="text"></v-text-field>
<v-text-field v-model="password" prepend-icon="lock" name="password" label="Password" id="password" type="password"></v-text-field>
<v-card-actions>
<v-spacer></v-spacer>
<v-btn type ='submit' color="primary">Login</v-btn>
</v-card-actions>
</v-form>
</v-card-text>
下面是我尝试调用操作的组件脚本:
<script>
import postLogin from '@/store/modules/auth'
export default {
data: () => ({
drawer: null,
email: '',
password: ''
}),
props: {
source: String
},
methods: {
postLogin (email,password) {
this.$store
.dispatch('postLogin', {email, password}).then(()=> this.$router.push({name: 'dashboard'}))
}
}
}
</script>
从“@/store/modules/auth”导入postLogin
导出默认值{
数据:()=>({
出票人:空,
电子邮件:“”,
密码:“”
}),
道具:{
资料来源:String
},
方法:{
postLogin(电子邮件、密码){
这是一家$1的商店
.dispatch('postLogin',{email,password})。然后(()=>this.$router.push({name:'dashboard'}))
}
}
}
我已经尝试了所有方法,请帮助我。您的
vuex
似乎正在使用模块。如果使用它们进行设置(即在模块中声明namespaced:true
),则您的操作将不再位于全局命名空间上。您必须将代码调整为分派到auth/postLogin
:
this.$store
.dispatch('auth/postLogin',{email,password})
。然后(()=>这个。$router.push({name:'dashboard'}))代码>
假设您将此名称空间模块注册到根
存储时,您也将其命名为auth
,则上述操作将起作用。如果将其命名为其他名称,则必须相应地调整dispatch()
调用:
//在src/store/index.js中
从“./modules/auth”导入身份验证;
...
const store=新的Vuex.store({
...
模块:{
auth,//现在发生了什么,您实际希望发生什么?这是我收到的错误:[vuex]未知操作类型:postLogin。我希望成功命中api。我已经在后端创建了用户(使用django)。因此,我可以通过管理门户登录。尝试通过API/axios使用vue前端登录。请共享您的存储代码调度调用和操作似乎是正确的(逻辑除外)所以我觉得您没有正确设置Vuex。@boussadjrabrahim我的存储代码在auth.js下的问题中。非常感谢!原来我有一个index.js和一个store.js,这在应用程序中造成了歧义。@WernerKotze我明白了。很高兴您解决了它。:)
<script>
import postLogin from '@/store/modules/auth'
export default {
data: () => ({
drawer: null,
email: '',
password: ''
}),
props: {
source: String
},
methods: {
postLogin (email,password) {
this.$store
.dispatch('postLogin', {email, password}).then(()=> this.$router.push({name: 'dashboard'}))
}
}
}
</script>