Angular2 router appends component instead of replacing it

ⅰ亾dé卋堺 提交于 2019-11-27 18:44:57

By using the method of elimination, I found out that the culprit of the issue was the BrowserAnimations module in my app.module.ts. By removing it from my imports it the problem went away. I'll look into creating a Plunker to demonstrate it.

Update: This is described in this Github issue.

Update 2017-12-13: This has now been fixed with this PR, fix(animations): properly recover and cleanup DOM when CD failures occur.

This happens also when component A is throwing an error, so when navigating to Component B, Component A could not be destroyed due to the error. This is a bug with Angular. Until they fix, find the cause of the error being thrown and fix it. Check your dev tools console.

I had a very similar issue, also using Firebase.

See the components being appended to router outlet

However, I found that the issue was coming from an error within one of my components, not related to my routing. One of the components had a reference to a "FormsArray", which was not used and malformed. It threw errors in the devtools console, but I didn't think of checking there, since everything was compiling fine.

Not sure if that will help anybody.

I was using NgZone inside the component and my routerLink was having the same issue, without any errors inside console.

Changed the routerLink to a (click) inside and called a function like this:

constructor(
    (...)
    private zone: NgZone,
    (...)
  ) { }

goToPage() {
    this.zone.run(() => this.router.navigate(['/page']));
}

I had a similar issue , and the reason was that i used a directive that is not declared in component A and there was no error in compile or console. so when navigate to component B the router was appending the content.

To debug I commented out all the html markup in both components leaving only a h1 to see if the content was appending. with some tests i found the directive and by removing it, router back to normal again.

Jaime Gonzalez

I'm not sure if this matches your situation exactly, however I have had previous Components append to the DOM when trying to load a different route, and finally I figured that the use of Hash was conflicting with addresses of the component,

export const AppRouting = RouterModule.forRoot(routes, { useHash: false });

This fixed all my problems with unwanted appended components

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!