我正在用 Nuxt 和 Prismic 作为 CMS 建立一个博客。
我的 nuxt.config.js 看起来像这样:
mode: 'universal',
modules: ['@nuxtjs/prismic'],
target: 'static',
generate: {
fallback: '404.html',
},
项目使用构建命令“npm run generate”部署在 Netlify
在 pages 目录中,我有动态链接( _uid.vue ),我使用新的fetch根据路由获取帖子。
async fetch() {
const post = await this.$prismic.api.getByUID('blog-post', this.$route.params.uid)
this.post = post
},
这一切都有效!但是我想处理获取错误并显示相应的错误页面。例如,当我们尝试获取的帖子不存在或现在被删除时。我尝试了他们从我上面提供的关于获取的链接中显示的内容,但是我收到错误消息,即帖子未定义。
async fetch() {
const post = await await this.$prismic.api.getByUID('blog-post', this.$route.params.uid)
if (post.id === this.$route.params.id) {
this.post = post
} else {
// set status code on server and
if (process.server) {
this.$nuxt.context.res.statusCode = 404
}
// use throw new Error()
throw new Error('Post not found')
}
}