【发布时间】:2020-09-07 10:23:52
【问题描述】:
我想知道如何在 nuxtjs 中重命名路由。
特别是,我有一个名为 products 的文件夹,其中包含 _id.vue 和 index.vue 文件。但是,我不希望路由是 www.mysite.com/products/productID,而是 www.mysite.com/productID。也就是说,我想从路由中移除产品。
知道我该怎么做吗?
【问题讨论】:
标签: javascript nuxt.js
我想知道如何在 nuxtjs 中重命名路由。
特别是,我有一个名为 products 的文件夹,其中包含 _id.vue 和 index.vue 文件。但是,我不希望路由是 www.mysite.com/products/productID,而是 www.mysite.com/productID。也就是说,我想从路由中移除产品。
知道我该怎么做吗?
【问题讨论】:
标签: javascript nuxt.js
Docks suggest that top-level dynamic routes will work fine。查看示例中的_slug 文件夹。
因此,以下文件夹结构有效:
pages
--| _product/
-----| index.vue
在您的index.vue 中,您可以通过$route 对象(check this answer for more details)访问product 参数,因此,它可以包含如下内容:
<!-- pages/_product/index.vue -->
<template>
<div class="flex items-center justify-center text-center py-20">
{{ $route.params.product }}
</div>
</template>
<script>
export default {}
</script>
我已经对此进行了测试,并且可以正常工作。如果进行此设置,您将转到www.mysite.com/shampoo/,您将看到shampoo 出现在屏幕上,如果您转到www.mysite.com/r2-d2/,您将看到r2-d2,依此类推。
【讨论】: