我仍在学习如何使用 NuxtJS,我在这里看到了文档:https://nuxtjs.org/guide/routing#unknown-dynamic-nested-routes如果您在名为“_.vue”的页面文件夹中添加一个文件,如果页面文件夹中没有指定路径,那么它就是一个包罗万象的文件。
这很好用,但我现在需要做的是能够将参数传递给该文件。我目前正在做我想在 pages/_slug/index.vue 中复制的内容,即:
asyncData ({ params }) {
return axios.get(`https://example.com/wp-json/wp/v2/pages?slug=${params.slug}`)
.then((res) => {
return {
page: res.data[0]
}
})
.catch(error => {
console.log(error)
this.errored = true
})
},
因为它在“_slug”文件夹中,所以我可以将 params.slug 传递给它并且它运行良好。但我现在正试图在 _.vue 文件中捕获所有内容,但不确定在此文件中获取 slug 参数的最佳方法。我尝试将该功能添加到该文件中,但它不起作用。
有没有办法将slug参数传递给NuxtJS中的_.vue文件?
最佳答案
使用参数.pathMatch:
asyncData ({ params }) {
return axios.get(`https://example.com/wp-json/wp/v2/pages?slug=${params.pathMatch}`)
.then((res) => {
return {
page: res.data[0]
}
})
.catch(error => {
console.log(error)
this.errored = true
})
},
关于javascript - NuxtJS 中带有 Slug 参数的未知动态嵌套路由?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54508373/