作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 vue.js 的新手,我有一个简单的问题。
我有如下代码:
asyncData({ params, error }) {
return Job.api()
.fetchByID(params.id)
.then((response) => {
const selectedJob = response.entities.jobs[0]
if (selectedJob) {
const industryID = selectedJob.industryId
return Industry.api()
.fetchByID(industryID)
.then((result) => {
const jobInd = result.response.data
return {
tagsArray:
selectedJob.tags === 'null' || selectedJob.tags === ''
? []
: selectedJob.tags.split(','),
job: selectedJob,
jobIndustry: jobInd,
}
})
.catch(() => {
error({
statusCode: 404,
message: 'Job Industry not found',
})
})
}
})
.catch(() => {
error({
statusCode: 404,
message: 'Job not found',
})
})
},
我想知道如何在捕获到错误404后将页面重定向到首页。
最佳答案
asyncData()
的参数是 Nuxt context , 其中包括 redirect()
您可以使用它来重定向用户:
export default {
asyncData({ redirect, params, error }) {
return Job.api()
.fetchByID(params.id)
.then(/*...*/)
.catch(() => {
redirect('/')
})
}
}
关于vue.js - 如何在 Nuxt 的 asyncData 中重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66793153/
我是一名优秀的程序员,十分优秀!