【发布时间】:2020-09-27 01:19:14
【问题描述】:
我正在使用带有 vue-router 的 vue,并且路由在路由到子路由时不起作用。
{
path: '/user',
name: 'User',
component: User,
children: [
{
path: 'profile',
component: Profile,
},
],
}
以编程方式路由到 /user/profile
<template>
<div>
<button @click="goToUserProfile()">create new</button>
</div>
</template>
<script>
export default {
methods: {
goToUserProfile() {
this.$router.push('/user/profile') // Routing doesn't work
.catch(console.log);
},
},
};
</script>
【问题讨论】:
-
错误是什么(如果有)?你试过这个。$router.push({name: 'whatever you name your route'}) ?
-
当我使用 this.$router.push('/user/profile');没有抛出错误,但是当我使用 this.$route.push({name: '/user/profile'});抛出错误'[vue-router] 名为'/user/profile'的路由不存在'
-
检查您的路线并查看您的路线的名称属性
-
您可以尝试将
name: 'UserProfile'添加到嵌套路由中,然后像this.$router.push({ name: 'UserProfile' });一样对UserProfile进行编程导航 -
现在抛出的错误显示“NavigationDuplicated”
标签: javascript vue.js vuejs2 vue-component vue-router