angular: Angular Router does not destroy component if an error was thrown

I’m submitting a…

[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report  <!-- Please search GitHub for a similar issue or PR before submitting -->
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

If component 1 throws an error, and then a user navigates to component 2, the error in component 1 will prevent component 1 from being destroyed by the router.

Expected behavior

Components should be destroyed by the angular router whether they throw an error or not.

Minimal reproduction of the problem with instructions

Link to Reproduce here

  1. Notice error on Developer console on Login Component
  2. Click login button using diagnosebug for the username and no password.
  3. Scroll down page.
  4. See that Login component is still visible beneath Dashboard component
  5. Click Logout button.
  6. Scroll up and notice another duplicate copy of Login component, and Dashboard component still visible.
  7. Can be repeated to infinite resulting in more duplicate components never being destroyed.

Environment


Angular version: 4.3.6


Browser:
- [x ] Chrome (desktop) version 63
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX
 
For Tooling issues:
- Node version: XX  
- Platform:  

Others:

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 11
  • Comments: 21 (1 by maintainers)

Most upvoted comments

Attached you can find a simple sample application which reproduces the problem.

The setup was done using angular-cli as follows:

$ ng new AngularIssue -sg -st --style=scss -sc --routing
$ cd AngularIssue\src\app\
$ mkdir components
$ cd components
$ ng g component ExampleOne --spec=false
$ ng g component ExampleTwo --spec=false

I added the BrowserAnimationsModule and the ReactiveFormsModule in the app.module.ts file and I updated the routing to include two routes to each component.

The app.component.html file was altered as follows:

<h1>Example App</h1>

<div>
  <ul>
    <li><a [routerLink]="['one']">One</a></li>
    <li><a [routerLink]="['two']">Two</a></li>
  </ul>
</div>

<div style="border: 2px solid red;">
  <router-outlet></router-outlet>
</div>

Component “One” was left as-is, component “Two” was changed to include the following HTML:

<p>
  example-two works!
</p>

<form [formGroup]="formGroup"></form>

The form group does not exists. When using ng serve or ng build to run the application, when clicking on the link to component “Two” it will crash and the content will be appended to the div.

Note that running ng build --prod will throw an error saying that “Property ‘formGroup’ does not exist on type ‘ExampleTwoComponent’.”, so a production build is covered.

issue-19273.zip

Same problem here!

I am also seeing this issue, i have tried to create a reproducable stackblitz but i havent been able to recreate the issue…

So if any of you can recreate the bug in a very very simple plunkr app then i’m sure it will be helpful for the angular team 😃

23 days goes by, can anyone from angular take a look at this? Or give an estimate of when they could?

So this is a duplicate of an older bug issued here #19093 But I think someone of Angular team just looked at it a week ago

I also have same error but can’t get rid of BrowserAnimationModule (cause whole app is based on angular material components… ). I really think this happen only when there is an error thrown by Angular due to a DOM problem (something wrong in {{ }} templating or in a custom directive). I couldn’t reproduce it in another case.

I really hope Angular team could have a look on it soon, as soon as they release Angular 5

I had a similar issue in my app, and removing the BrowserAnimationModule from the app.modules.ts file fixed the problem, as mentioned before.

It turned out the issue was because the second component was throwing an error. When we resolved the error in the second component the appended routing issue was resolved.

Anybody there?