【发布时间】:2021-08-12 05:20:38
【问题描述】:
我正在尝试从具有嵌套子项的嵌套视图访问应用程序“/”或命名组件“Home”的根路径。
const routes = [
{
path: "/",
name: "Home",
component: Home,
},
...
{
path: "/account",
name: "Account",
component: AccountHome,
children: [
{
name: "AccountAvatars",
path: "avatars",
component: AccountAvatar,
},
...
],
},
];
const router = new VueRouter({
mode: "history",
base: '/',
routes,
scrollBehavior(to, from, savedPosition) {
return { x: 0, y: 0 };
},
});
Vue.use(VueRouter);
export default router;
....
当我想使用 router-link 或手动使用 /account/avatars 中的 this.$router.push({name: 'Home'}) 或 this.$router.push({name: '/'}) 时,例如单击 router-link 或 $router.push 根本不起作用。
App.vue
<template>
<div id="app">
<global-header />
<router-view />
<global-footer />
</div>
</template>
首页.vue
<template>
<div class="home">
<offer />
<home-menu />
<game-catalog/>
<benefits />
<news />
</div>
</template>
AccountHome.vue
<template>
<div class="container">
<div
class="account"
>
<account-left-sidebar />
<router-view />
<account-right-sidebar />
</div>
</div>
</template>
路由器实例
const router = new VueRouter({
mode: 'history',
base: process.env.BASE_URL,
routes,
scrollBehavior(to, from, savedPosition) {
return { x: 0, y: 0 }
}
})
【问题讨论】:
-
你在哪里调用 router.push?
-
@FatimaMazhit 在 onClick 处理程序中,不使用路由器链接时附加到 。 onClick 处理程序有效,但处理程序函数主体中的 this.$router.push 不起作用:(
-
您确定要调用该方法吗?你能从这里分享代码吗?
标签: vue.js vuejs2 vue-router