【问题标题】:Can i use ampersand instead of front slashes with the vue router?我可以在 vue 路由器中使用 & 号而不是前斜线吗?
【发布时间】:2021-11-03 13:43:50
【问题描述】:

我正在制作一个 VueJS 表格组件作为 php Web 应用程序的一部分。该应用程序使用“/”来更改目录,使用“&”来导航 Web 应用程序。我是否有可能在 vue 路由器上使用 & 符号,或者我必须想出一个自定义的导航解决方案?有什么推荐吗?

const routes = [
    {
        path: '&',
        component: Table
    },
    {
        path: '&create',
        component: Create
    },
    {
        path: '&single&:id',
        component: Single
    },
    {
        path: '&edit&:id',
        component: Edit
    }
]

【问题讨论】:

  • 您好,欢迎来到 SO。你能具体说明为什么需要这样做吗?
  • 将使用该插件的 PHP 项目很大,并且正在开发一段时间,该项目的约定是它使用 '/' 来更改目录,使用 '&' 来导航和参数。
  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: vue.js navigation vue-router vuejs3


【解决方案1】:

在这种情况下这是不可能的。使用vue-router 时,所有父路由都需要一个正斜杠。这不是子路由的情况,正如您在文档中的这个示例中看到的那样:

const router = new VueRouter({
  routes: [
    {
      path: '/user',
      component: User,
      children: [
        {
          path: 'profile',
          component: UserProfile
        },
        {
          path: 'posts',
          component: UserPosts
        }
      ]
    }
  ]
})

尽管如此,即使您只使用一个父路由而只使用子路由,您仍然需要为父路由添加正斜杠。也许您找到了解决这种情况的方法并提供如下路线:

const routes = [
    {
        path: '/table',
        component: Table,
        children: [
            {
                path: 'create',
                component: Create
            },
            {
                path: 'single',
                component: SingleContainer, // Wrapper
                children: [
                    {
                        path: ':id',
                        component: Single
                    }
                ]
            },
            {
                path: 'edit',
                component: EditContainer, // Wrapper
                children: [
                    {
                        path: ':id',
                        component: Edit
                    }
                ]
            }
        ]
    }
]

如果您也将它们命名为name: 'create',则可以在没有前斜杠的情况下调用它们,例如:

<router-link :to="{ name: 'create'}">Create</router-link>

但正如我所说,您仍然需要在您的router.js 中提供至少一个带有正斜杠的父路由。

如果可以在 url 请求服务器端替换 & 符号,我做了一些研究。但与号是保留字符,您可以在此处阅读:Reserved characters

【讨论】:

  • 感谢您澄清这一点。看来我们必须在应用程序的 php 部分使用 '&' 设置自定义路由,我将使用URLSearchParams 根据路由参数显示组件
猜你喜欢
  • 2019-11-07
  • 2021-02-26
  • 1970-01-01
  • 2022-01-06
  • 2018-12-18
  • 2019-03-24
  • 1970-01-01
  • 1970-01-01
  • 2020-07-26
相关资源
最近更新 更多