2 回答

TA貢獻1811條經驗 獲得超5個贊
為“/用戶/配置文件”提供路由名稱“配置文件”
{
path: '/user',
name: 'User',
component: User,
children: [
{
path: 'profile',
name: "Profile",
component: Profile,
},
],
}
導航 使用路由名稱
this.$router.push({name: "Profile"});
您的用戶組件應像這樣聲明
用戶.vue
<template>
<div>
<p>this is user component</p>
<!-- your Profile component will replace this route-view -->
<route-view />
</div>
</template>
演示
https://codesandbox.io/s/falling-bash-6dl7m

TA貢獻1829條經驗 獲得超6個贊
請確保輸入“用戶組件”模板以顯示嵌套(子)路由。<router-view></router-view>
<template>
<div>
<button @click="goToUserProfile()">create new</button>
<router-view></router-view> <!-- placeholder for children routes -->
</div>
</template>
然后,您可以通過兩者訪問和this.$router.push('/user/profile')this.$router.push({ name: 'UserProfile' })
正如 Vue 路由器文檔所述:要將組件渲染到此嵌套插座中,我們需要使用 VueRouter 中的子選項。
https://router.vuejs.org/guide/essentials/nested-routes.html
希望這有幫助。
添加回答
舉報