我对Vue中的默认 children 路由有问题.js 2.

当我最初访问localhost/listings时,它会正确地加载索引.vue和 map .小时候的vue.

当我使用路由链接导航到localhost/listings/1,然后使用路由链接返回到localhost/listings时,它仍然会加载节目.vue模板.This shouldn't happen?

我没有导航装置或任何干扰.有没有办法纠正这个问题?

我的路由:

window.router = new VueRouter({
    routes: [

        ...

        {
            path: '/listings',
            name: 'listing.index',
            component: require('./components/listing/index.vue'),
            children: [
                {
                    path: '',
                    component: require('./components/listing/map.vue')
                },
                {
                    path: ':id',
                    name: 'listing.show',
                    component: require('./components/listing/show.vue')
                }
            ]
        },

        ...

    ]
});

推荐答案

也许可以try 重新安排子元素们,路由是按照他们从上到下匹配的顺序排列的,所以这应该有望解决这个问题:

window.router = new VueRouter({
    routes: [

    ...

    {
        path: '/listings',
        name: 'listing.index',
        component: require('./components/listing/index.vue'),
        children: [
            {
                path: ':id',
                name: 'listing.show',
                component: require('./components/listing/show.vue')
            }
            {
                path: '',
                component: require('./components/listing/map.vue')
            },
        ]
    },

    ...

  ]
});

只需要澄清一下,你的path: ''基本上是一个"包罗万象",这可能就是为什么当它位于顶端时,它会立即被找到,因此路由永远不会传播到:id路由.

Vue.js相关问答推荐

在Nuxt项目中混合Scrolltrigger与Lenis

如何在Vue 3中将动态特性从主零部件同步到子零部件

vuejs中如何获取数组总长度

使用内部方法创建计算(computed)属性是否合法?

vue 3 中范围样式和深度样式的问题

JSDoc 单文件 Vue 组件

带有参数的 Vuex 映射 Getter - 缓存?

Vue + Webpack 构建显示空白页面

使用 Vue 和 JS 下载 CSV 文件

Vue.js 中的 mount挂载是什么意思?

如何在 vue.js 2 的其他组件中调用方法?

一个 div 组件中的 Vue.js 2.0 路由链接

使用 Vue.js (cdn) 在特定元素上切换显示/隐藏

Vue router路由,Root Vue 没有数据?

我可以避免使用 Vue.js 组件的重复样式吗?

基本 vue.js 2 和 vue-resource http 获取变量赋值

将 SignalR 与 Vue.js 和 Vuex 集成

Vuetify - 如何进行分页?

无法访问手表处理程序 vuejs 中的数据变量

使用 Vee-validate 禁用按钮,直到正确填写表单