Vue.js 参数无效的Vue路由器
我正在学习如何使用vue路由器,但我不知道如何做一些事情 我有一个路径为“/entry/:id/”的路由。 所以,我想要一个详细的页面 我使用v-for循环遍历每个条目,显然,从该条目中获取id,然后将其作为param发送到路由器链接,如下所示:Vue.js 参数无效的Vue路由器,vue.js,vuejs2,Vue.js,Vuejs2,我正在学习如何使用vue路由器,但我不知道如何做一些事情 我有一个路径为“/entry/:id/”的路由。 所以,我想要一个详细的页面 我使用v-for循环遍历每个条目,显然,从该条目中获取id,然后将其作为param发送到路由器链接,如下所示: <router-link :to="{ name: 'entry-detail', params: {id: entry.id} }"> 当链接被访问时,我获得this.$route.params.id,并且我能够向API发出get请
<router-link :to="{ name: 'entry-detail', params: {id: entry.id} }">
当链接被访问时,我获得this.$route.params.id,并且我能够向API发出get请求,以获取特定条目。
问题是,当我使用一个不存在的无效id(例如/entry/invalid_id/)访问条目详细信息页面时,它会让我得到没有数据的布局,这很公平,但我如何能够从该页面重定向或显示404模板,而不是仅仅拥有一个空白填充页面
顺便说一句,对不起,我的语法错误
提前感谢您的回答 假设您的ajax响应返回错误代码,那么您应该能够捕获并在响应中处理它,我在这里使用: 如果没有得到错误响应,则可以在数据为空时重定向:
axios.get('/api/results/' + this.$route.params.id).then(response => {
// success, set the model variables
if(response.data == null){
this.$router.push('/notFound')
}
// success, set the model variables
}).catch(error => {
// Handle errors
})
假设您的ajax响应返回错误代码,那么您应该能够捕获并在响应中处理它,我在这里使用: 如果没有得到错误响应,则可以在数据为空时重定向:
axios.get('/api/results/' + this.$route.params.id).then(response => {
// success, set the model variables
if(response.data == null){
this.$router.push('/notFound')
}
// success, set the model variables
}).catch(error => {
// Handle errors
})
我也用axios。谢谢你的回答,我想这解决了我的问题。我也使用axios。谢谢你的回答,我想这解决了我的问题。