我需要在Nuxt2中将/index用作URL的一部分。

huangapple go评论93阅读模式
英文:

I need to use /index as part of url on nuxt2

问题

我无法提供代码部分的翻译,请提供您需要的文本翻译。

英文:

Task request path rendering xxx.com/index/news, xxx.com/index/User this effect,I set the file tree to:

  1. pages/
  2. --| index/
  3. -----| News/
  4. -------| index.vue
  5. -----| User/
  6. -------| index.vue
  7. --| index.vue

But I can't successfully jump to other pages except the root directory.

What do I need to make the path of the webpage show the desired effect?

答案1

得分: 1

主页(位于根路径)可以安全地命名为 index.vue,没有其他方法将页面绑定到 / 路径。

但问题确实在于 无法同时使用相同名称的页面和文件夹。它们会重叠。

解决此问题的方法可以是从 nuxt.config.js 中使用自定义路由将根路径 / 映射到您的主页:

  1. router: {
  2. extendRoutes (routes, resolve) {
  3. routes.push(
  4. {
  5. name: 'index_home',
  6. path: '/',
  7. component: resolve(__dirname, 'pages/home.vue')
  8. },
  9. )
  10. }
  11. },

请参阅 nuxt.config.js / router 文档

注意:您还可以从此处删除自动生成的 /home 路由,它将在 routes 数组中。

英文:

The home page (at the root path) can safely be named index.vue, there is no other way to bind a page to the / path.

But indeed, the problem is that you can't have both a page and a folder named the same. They will overlap.

A solution for this can be to use a custom routing from the nuxt.config.js to map the root path / to your home page:

  1. router: {
  2. extendRoutes (routes, resolve) {
  3. routes.push(
  4. {
  5. name: 'index_home',
  6. path: '/',
  7. component: resolve(__dirname, 'pages/home.vue')
  8. },
  9. )
  10. }
  11. },

See nuxt.config.js / router documentation

Note: you could also remove the automatically created /home route from here, it will be in the routes array.

huangapple
  • 本文由 发表于 2023年7月10日 18:15:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76652764.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定