【发布时间】:2021-08-01 20:47:16
【问题描述】:
我的 nuxt 项目遇到问题。
当我使用 nuxt-link 路由页面时,它不会在我的页面中呈现组件,我猜这不是在进行 fetch 调用。
但是当我使用正常的 href 链接时,我的页面工作正常。一切就绪。
这是博客列表页面组件中的链接
//博客列表页sn-p
<template>
<div>
<div v-for="blog in blogs.response.posts" :key="blog.id" class="col-md-3">
<nuxt-link :to="`/blogs/${blog.id}`" class="theme-blog-item-link"> Click to View Blog </nuxt-link>
</div>
</div>
</template>
<script>
export default {
data() {
return {
blogs: [],
}
},
async fetch() {
this.blogs = await fetch('https://www.happyvoyaging.com/api/blog/list?limit=4').then((res) => res.json())
},
}
</script>
但是如果我用 href 标记替换 nuxt-link 就可以了
<a :href="`/blogs/${blog.id}`" class="theme-blog-item-link">
Click to View Details
</a>
通过单击该链接,我想通过给定的 ID 查看博客的详细信息。即_id.vue,该页面的代码如下。
//这是具体的博客详情页面代码
<template>
<div class="theme-blog-post">
<div v-html="blogs.response.description" class="blogdesc"></div>
</div>
</template>
<script>
export default {
data(){
return {
blogs: []
}
},
async fetch() {
const blogid = this.$route.params.id
this.blogs = await fetch('https://www.happyvoyaging.com/api/blog/detail?id='+blogid+'').then((res) => res.json())
},
}
</script>
问题出在 blogdetails 页面上,通过 nuxt-link 路由而不是渲染组件,而是通过正常的 a href 链接,它工作正常
我在控制台中收到此错误
vue.runtime.esm.js?2b0e:619 [Vue warn]: Unknown custom element: <PageNotFound> - did you register the component correctly? For recursive components, make sure to provide the "name" option.
found in
---> <Error> at layouts/error.vue
<Nuxt>
<Layouts/default.vue> at layouts/default.vue
<Root>
【问题讨论】:
-
给定模板中没有
<nuxt-link>? -
为什么在目标模板中需要
?使用 路由到给定模板不起作用,但使用 标记,它可以正常工作 -
请分享不起作用的代码。
-
我已经更新了问题以明确表示,我已经分享了代码。有两个页面,博客列表和博客详细信息,从博客列表页面,我使用
路由到博客详细信息页面。 -
请给你的sn-ps代码命名并给出更广泛的范围,例如
blog.id取自哪里?除了这个,剩下的逻辑是什么?fetch()应该是“详细信息”页面吗?您应该将其传递给特定的博客,而不是整个博客列表。