【问题标题】:Vue 3 Router does not append a trailing slash to a parent view and so will not render the default child vue componentVue 3 路由器不会在父视图中附加斜杠,因此不会呈现默认的子 vue 组件
【发布时间】:2021-06-22 20:02:34
【问题描述】:

我的嵌套 vue-router 不会将尾部斜杠附加到父链接,因此不会呈现分配的组件。如果我在 url 中手动输入斜杠并在浏览器中按回车键,它将呈现。

嵌套视图使用 3 个组件:

  1. CustomersView.vue
  2. CustomersMainView.vue
  3. CustomersDetailView.vue

嵌套路径结构如下:

{
    path: '/app',
    component: Dashboard,
    children: [
        { path: 'dashboard', name: 'DashboardHome', component: DashboardHome, meta: {requiresAuth: true }},
        { path: 'my-profile', name: 'MyProfileView', component: MyProfileView, meta: {requiresAuth: true }},
        { path: 'contacts', name: 'ContactsView', component: ContactsView, meta: {requiresAuth: true }},
        {{
            path: 'customers',
            name: 'CustomersView',
            component: CustomersView,
            meta: {requiresAuth: true },
            children: [
                { path: '', name: 'CustomersMainView', component: CustomersMainView, meta: {requiresAuth: true } },
                { path: 'detail/:ID', name: 'CustomerDetailView', component: CustomerDetailView, meta: {requiresAuth: true } },
            ]
        },
}

我可以通过手动输入网址来查看所有这些视图。

myapp.com/customers/ <-- This renders the `CustomerMainView` properly

但是当使用我的侧边栏导航中的链接呈现链接时,它不会附加尾部斜杠:

<!-- Customers -->
    <div class="nav-category-container">
        <div class="nav-category-item">
            <router-link :to="{ name: 'CustomersView', params: {}}">Customers</router-link>
        </div>
    </div>

它指向

myapp.com/customers

它会省略尾部斜杠,因此不会按照路由器中的指示呈现 CustomerMainView 组件。

如何获取该链接以附加尾部斜杠?我以为它会自动完成。

【问题讨论】:

    标签: vue.js vuex vue-router


    【解决方案1】:

    您是否尝试在组件的子路由中添加斜杠?

     {
        path: 'customers',
        name: 'CustomersView',
        component: CustomersView,
        meta: {requiresAuth: true },
        children: [
            { path: 'customers', name: 'CustomersMainView', component: CustomersMainView, meta: {requiresAuth: true } },
            { path: 'detail/:ID', name: 'CustomerDetailView', component: CustomerDetailView, meta: {requiresAuth: true } },
        ]
    },
    

    【讨论】:

    • 我在客户的末尾放了一个斜线,它会附加斜线,但它不会加载组件 CustomersMainView。我尝试像您显示的那样添加“客户”,但这仅适用于 /customers/customers
    • 我还在子组件的前后添加了斜线,而且似乎也不起作用。
    • 如果我为父级“/stm/customers/”创建路径,那么单击时斜杠就在那里,但没有渲染 customerMainView 组件,但是如果我转到 url 并删除尾部斜杠它呈现组件。所以,我不知道发生了什么 LOL
    猜你喜欢
    • 2021-10-25
    • 2017-07-31
    • 2020-01-27
    • 2020-02-19
    • 1970-01-01
    • 2020-07-16
    • 2019-05-22
    • 1970-01-01
    • 2022-09-23
    相关资源
    最近更新 更多