【发布时间】:2020-09-29 12:50:20
【问题描述】:
出于 SEO 的目的,我想在动态路径的末尾添加一个字符串(产品标题、类别标题……)。 我的产品页面结构是这样的:
pages:
product (folder)
- _id.vue
所以为了显示产品单页,我使用 URL example.com/product/1 其中1 是产品 ID。
然后在 _id.vue 页面中获取我的产品:
async fetch(){
let response = await this.axiosFetch(`product/${this.$route.params.id}`)
if(this.resOk(response.status)){
if(this.notEmpty(response.data)){
this.product = response.data
}
}else{
this.$nuxt.error({ statusCode: 404, message: "not found" })
}
},
现在如上所述,我想在 url 末尾添加产品标题,例如 example.com/product/1/nike-shoe
我该怎么做??
更新
还有一个问题不能仅通过 url 上的参数来解决:
我有一个类别页面example.com/category。在页面获取我获取这样的猫(在页面上创建 catId 是 0 来获取父母并且每个父母我获取孩子):
async fetch(){
let res = await this.$axios.$get(`category/${this.catId})
this.catArray.push(res)
}
我从父 ID 中获取父级,获取子级并加载下面的父级等等。所有这些都在 example.com/category 中。没有路由更改仅将 catId 发送到路由查询(因此我可以使用 catId 从另一个页面导航)。那么如何在 url 末尾添加每个选定类别的标题并且仍然在同一页面中??
【问题讨论】:
-
你试过
this.$route.push吗? -
product(folder)->_id(folder)->_slug.vue怎么样? -
@ma_jafari ,push 会改变路由并指向它,因为没有这样的页面它会给出 404
-
@Jazuly,我知道你在说什么。发送到该页面并获取我获取的第一个参数,对吗?如果有的话,我更喜欢其他可能的选择!
标签: vue.js vuejs2 nuxt.js vue-router