渲染默认子 vue-router 4
Render default child vue-router 4
我想询问有关渲染默认子视图的问题。
我知道 存在类似的问题,但我很确定它适用于 VUE2 和更旧的 vue-router。我在呈现视图类别的默认子项时遇到问题。当我去 <router-link :to="{ name: routeName, params: { category: item.id } }
除了 <router-view />
它是空的...
之外一切都正常
我想出了一个使用 beforeUpdate(),beforeCreate() 的解决方案,但这似乎是在重新发明轮子。
如果我转到 /category/{id} 然后转到 /category/{id}/room/{id} 并返回一页,将呈现默认视图
如何在转到 /category/{id} 后使默认子加载?
router.js
{
path: '/category/:category',
name: 'Category',
props: true,
component: Category,
children: [
{
path: '',
name: 'FavouriteDevicesInCategory',
component: Devices,
props: true,
},
{
path: 'room/:room',
name: 'Devices',
component: Devices,
props: true,
},
],
},
Category.vue - 查看
<template>
<div class="control">
<div class="left">
[...]
</div>
<div class="right">
<router-view />
</div>
</div>
</template>
<script>
export default {
props: ['category', 'room'],
/** generate default router-view */
beforeUpdate() {
this.$router.push('');
},
beforeCreate() {
this.$router.push('');
},
};
如果您的默认组件是 Category
,您必须像这样创建路由:
{
path: '/category',
name: 'Category',
props: true,
component: Category,
children: [
{
path: '/:categoryId',
name: 'FavouriteDevicesInCategory',
component: Devices,
props: true,
},
{
path: 'room/:roomId',
name: 'Devices',
component: Devices,
props: true,
},
],
},
默认情况下它将呈现 Category
组件,如果您将 id
添加到 url 它将呈现 Devices
组件
我想我找到了一个解决方案,有点乱,但它似乎是解决这个问题的最佳方案
{
path: '/category/:category',
// name: 'Category',
props: true,
component: Category,
children: [
{
path: '',
name: 'Category',
component: Devices,
props: true,
},
{
path: 'room/:room',
name: 'Devices',
component: Devices,
props: true,
},
],
},
我想询问有关渲染默认子视图的问题。
我知道 <router-link :to="{ name: routeName, params: { category: item.id } }
除了 <router-view />
它是空的...
我想出了一个使用 beforeUpdate(),beforeCreate() 的解决方案,但这似乎是在重新发明轮子。 如果我转到 /category/{id} 然后转到 /category/{id}/room/{id} 并返回一页,将呈现默认视图
如何在转到 /category/{id} 后使默认子加载?
router.js
{
path: '/category/:category',
name: 'Category',
props: true,
component: Category,
children: [
{
path: '',
name: 'FavouriteDevicesInCategory',
component: Devices,
props: true,
},
{
path: 'room/:room',
name: 'Devices',
component: Devices,
props: true,
},
],
},
Category.vue - 查看
<template>
<div class="control">
<div class="left">
[...]
</div>
<div class="right">
<router-view />
</div>
</div>
</template>
<script>
export default {
props: ['category', 'room'],
/** generate default router-view */
beforeUpdate() {
this.$router.push('');
},
beforeCreate() {
this.$router.push('');
},
};
如果您的默认组件是 Category
,您必须像这样创建路由:
{
path: '/category',
name: 'Category',
props: true,
component: Category,
children: [
{
path: '/:categoryId',
name: 'FavouriteDevicesInCategory',
component: Devices,
props: true,
},
{
path: 'room/:roomId',
name: 'Devices',
component: Devices,
props: true,
},
],
},
默认情况下它将呈现 Category
组件,如果您将 id
添加到 url 它将呈现 Devices
组件
我想我找到了一个解决方案,有点乱,但它似乎是解决这个问题的最佳方案
{
path: '/category/:category',
// name: 'Category',
props: true,
component: Category,
children: [
{
path: '',
name: 'Category',
component: Devices,
props: true,
},
{
path: 'room/:room',
name: 'Devices',
component: Devices,
props: true,
},
],
},