javascript - 如何验证 Nuxt 中的路由参数?

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

我正在尝试像这样在我的页面组件中验证路由参数:

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
    }
}

这根本行不通。即使我输入无效参数,它仍会加载页面。请帮忙!

最佳答案

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

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)
}

关于javascript - 如何验证 Nuxt 中的路由参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52528983/

相关文章:

async-await - 配置等待的静态分析

javascript - 尝试更改 h1 标签的 insidehtml 但抛出错误

html - 减少 radio 标签行之间的空间

javascript - 如何使用vue.js添加购物车?

javascript - defineEmits 函数的 Vue linting 错误

react-native - 在 React Native 中,这两种 Async Await 方法哪个更好,为什么?

asynchronous - 将 Either 与 Future 一起使用

JavaScript Window.Open 位置功能不起作用

javascript - 如何获取选择类型名称和选择的值

javascript - 无法使用选中的 ="Checked"属性找到单选按钮元素