【发布时间】:2019-08-05 23:30:20
【问题描述】:
问题
使用 Vue.js,如何在不更改 url 的情况下在动态路由中返回 404 错误?
我的 route.js 的一部分(一切正常)
{
path: '/work/:id',
name: 'work',
component: () => import( './views/Work.vue' )
},
{
path: '*',
name: 'NotFound',
component: () => import( './views/NotFound.vue' )
}
Work.vue 组件的一部分。我在哪里检查路由参数是否在我的静态 json 中,如果没有,请在路由输入之前打开 NotFound 组件
beforeRouteEnter (to, from, next) {
next(vm => {
vm.item = json.find(item => item.id == vm.$route.params.id)
if(!vm.item) {
next({name: NotFound})
}
})
}
问题
当我尝试 site.com/work/non-existent-id 时,“NotFound”组件打开,但网址来自 site.com/work/non-existent- id 到 site.com
我的看法
site.com/work/non-existent-id 打开组件“NotFound”,网址保持在site.com/work/non-existent-id
例子
https://vuejs.org/v2/guide/something - 返回 404 错误并停留在 url 中
【问题讨论】:
标签: javascript vue.js http-status-code-404 vue-router