简体   繁体   English

Angular2路由器不替换组件

[英]Angular2 router doesn't replace component

When I use router.navigate['some-route'], the component of the new route is added on top of the component of the old route instead of replacing it: 当我使用router.navigate ['some-route']时,新路由的组件会添加到旧路由的组件之上,而不是替换它:

Current route is the login page: 当前路线是登录页面:

在此处输入图片说明

After logging-in I'm navigating to my main page by doing: 登录后,我通过以下操作导航到主页:

this.router.navigate(['/form-builder']);

, but it's component is simply added on top of the login component without replacing it: ,但它的组件只是添加到了登录组件的顶部,而无需替换它:

在此处输入图片说明

Root component template: 根组件模板:

<router-outlet></router-outlet>

Routing configuration: 路由配置:

const routes: Routes = [
    { path: '', redirectTo: 'form-builder', pathMatch: 'full' },
    { path: 'form-builder', component: FormEditorComponent, canActivate: [AuthGuard] },
    { path: 'login', component: LoginComponent },
    { path: 'signup', component: SignupComponent },
];

What am I doing wrong? 我究竟做错了什么?

When you see the component being added on TOP of the current component, it usually means there's some error being thrown. 当您看到该组件被添加到当前组件的TOP上时,通常意味着会抛出一些错误。 Check your dev console, and tell us if you're getting some errors when navigating. 检查您的开发控制台,并告诉我们在浏览时是否遇到一些错误。 If there is, everytime you navigate somewhere else, it should keep on stacking components on top of eachother. 如果有的话,每次您导航到其他地方时,它都应继续将组件堆叠在一起。

No, I'm not sure why, or if Angular meant for it to happen this way, but it does. 不,我不确定为什么,或者Angular是否打算以这种方式发生,但是确实如此。 Just speaking from experience. 只是从经验上讲。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM