【问题标题】:How to validate route parameter in Nuxt?如何验证 Nuxt 中的路由参数?
【发布时间】:2019-03-02 21:30:21
【问题描述】:

我正在尝试验证页面组件中的路由参数,如下所示:

async validate({ params, store }) {
    await store.dispatch(types.VALIDATE_PARAMS_ASYNC, params.id)
}

然后在店里:

async [types.VALIDATE_PARAMS_ASYNC]({state, commit, dispatch}, payload) {
    try {
        const res = await this.$axios.$post('/api/params/validate', {
            params: payload
        })
        commit(types.MUTATE_SET_INFO, res.data) // this mutation is in another module. This doesn't work either
        return true
    } catch(e) {
        return false
    }
}

这根本不起作用。即使我输入了无效的参数,它仍然会加载页面。请帮忙!

【问题讨论】:

    标签: javascript vue.js async-await nuxt.js


    【解决方案1】:

    您的validate 方法必须返回一个布尔值:

    async validate({ params, store}) {
        // await operations
       return true // if the params are valid
       return false // will stop Nuxt.js to render the route and display the error page
    }
    

    见官方文档:https://nuxtjs.org/api/pages-validate#the-validate-method


    async validate({ params, store }) {
        return await store.dispatch(types.VALIDATE_PARAMS_ASYNC, params.id)
    }
    

    【讨论】:

    • 我正在返回布尔值!
    • 不在此处:异步验证({ params, store }) { await store.dispatch(types.VALIDATE_PARAMS_ASYNC, params.id) }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-26
    • 2015-06-17
    • 1970-01-01
    • 1970-01-01
    • 2018-12-10
    • 1970-01-01
    相关资源
    最近更新 更多