【问题标题】:Unknown action type in Nuxt Vuex storeNuxt Vuex 商店中的未知操作类型
【发布时间】:2021-06-27 14:32:46
【问题描述】:

我在从 vuex 调用操作时遇到问题。每次我尝试访问 loginUser 操作时,我都会从 vuex 收到错误“未知操作类型”。也许我没有以正确的方式称呼它。请告诉我我的代码有什么问题。

存储:user.js

import axios from 'axios'

export const state = () => ({
    users: [],
    loggedIn: false,
})

export const getters = {
    getLoggedIn: (state) => { return state.loggedIn },
}

export const actions = {
    loginUser({ commit }, payload){
        if(state.loggedIn){
            console.log("you're already logged in!")
        }else{
            return new Promise(async(resolve, reject) => {
                const { data } = await axios.post('/api/users/login-admin', {
                    login: payload.login, 
                    password: payload.password
                })
                if(data.success){
                    commit("loggedIn", true)
                    resolve()
                }else{
                    commit("loggedIn", false)
                    reject('an error has ocurred')
                }
                return data.success
            }).catch(err => alert(errCodes(err.code)))
        }
    },
}

export const mutations = { 
    setLoggedIn(state, payload) {
        state.loggedIn = payload
    }
}

登录.vue

    computed: {
        ...mapGetters(['getCount'] , {user: 'getLoggedIn'}),
        ...mapActions([
            'loginUser'
        ]),
    },
    methods: {
        onSubmit: function(){
            this.$store.dispatch({
                type: 'loginUser',
                email: this.login,
                pass: this.pass
            }).then(()=>{
                this.$router.push('../admin_2065')
                this.onReset()
            }).catch(e => console.log(e))
        },
        onReset(){
            this.login = ''
            this.pass = ''
            this.$nextTick().then(() => {
                this.ready = true
            })
        }
    },

错误:

任何帮助将不胜感激,谢谢。

【问题讨论】:

    标签: vue.js nuxt.js vuex


    【解决方案1】:

    mapActions 应该在方法选项内并添加命名空间user/

      computed: {
            ...mapGetters(['getCount'] , {user: 'getLoggedIn'}),
        },
        methods: {
         ...mapActions([
                'user/loginUser'
            ]),
            onSubmit: function(){
                this['user/loginUser']({
                    email: this.login,
                    pass: this.pass
                }).then(()=>{
                    this.$router.push('../admin_2065')
                    this.onReset()
                }).catch(e => console.log(e))
            },
            onReset(){
                this.login = ''
                this.pass = ''
                this.$nextTick().then(() => {
                    this.ready = true
                })
            }
        },
    

    【讨论】:

    • 我在方法里面改了但是还是说 [vuex] unknown action type: loginUser
    猜你喜欢
    • 2021-02-10
    • 2023-02-03
    • 2020-01-12
    • 2021-11-26
    • 2018-11-23
    • 2021-07-22
    • 1970-01-01
    • 2019-03-13
    • 1970-01-01
    相关资源
    最近更新 更多