仅在VueJS中完成调度和提交后触发路由

问题描述 投票:0回答:1

我有一个表单提交,它接收电子邮件和密码,然后将它们传递到商店中名为userSignIn的动作中

SignIn.vue:

onSubmit () {
    if (this.$refs.form.validate()) {
    const user = {
        email: this.email,
        password: this.password
    }
    this.$store.dispatch('userSignIn', user)
        .then(() => {
            this.$router.push('/')
        }).catch(err => {
            console.log(err)
        })
    }
}

在商店里,我确实有像这样的userSignIn动作

store.js动作:

userSignIn ({commit, getters}, payload) {
    getters.Api.post(`user/signin`, {
        email: payload.email,
        password: payload.password
    }).then(res => {
        commit('userSignIn', res.data.token)
    }).catch(err => {
        console.log(err)
    })
}

路由(this.$router.push('/'))应该只在userSignIn提交(commit('userSignIn', res.data.token))之后完成。但是在提交之前实际发生的路由触发器会产生什么结果和错误,因为用户令牌尚未设置。

如何在完成this.$router.push('/')dispatch之后触发某些东西(在这种情况下是commit)?

vue.js vuejs2 es6-promise vue-router vuex
1个回答
1
投票

返回承诺就行了。

userSignIn ({commit, getters}, payload) {
    return getters.Api.post(`user/signin`, {
        ......
    })
© www.soinside.com 2019 - 2024. All rights reserved.