ViewDestroyedError: trying to use destructed view: detectChanges in Angular 4

I am new to Angular 4 and am trying to create a simple routing, but when I try to redirect to a successful register with this.router.navigate(['./admin/login']);

, so it throws this error ViewDestroyedError: Attempt to use a destroyed view: detectChanges

in console.log

. This is what my file looks like register.component.ts

:

import { Component, OnDestroy } from '@angular/core';
import { Router } from "@angular/router";
import { ChangeDetectionStrategy } from '@angular/core';

import { FormValidationService } from "../../validations/form-validation.service";
import { FlashMessagesService } from 'angular2-flash-messages';
import { AuthService } from '../../services/auth.service';

@Component({
  templateUrl: 'register.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class RegisterComponent implements OnDestroy  {
  name: String;
  email: String;
  password: String;
  re_password: String;
  mobile:String;


  constructor(private formValidation: FormValidationService, 
            private flash: FlashMessagesService, 
            private auth: AuthService,
            private router: Router) { }




  registerUser(){   
    var user = {
        name: this.name,
        email: this.email,
        password: this.password,
        re_password: this.re_password,
        mobile:this.mobile,
    }



    if(!this.formValidation.validateEmail(this.email)){
        this.flash.show("Invalid email format!",{ cssClass: 'alert-danger', timeout: 3000 });
        return false;
    }

    this.auth.authRegisterUser(user).subscribe(data => {
      if(data.success){
        this.flash.show("User created successfully!", { cssClass: 'alert-success', timeout: 3000 });
        this.router.navigate(['./admin/login']);     // <-------This is the problem -------------->
      }else{
        this.flash.show(data.message, { cssClass: 'alert-success', timeout: 3000 });
        return false;
      }
    });

  }

}

      

And I created a file auth.module.ts

where I mentioned the route for these two.

import { NgModule } from '@angular/core';
import { FormsModule }   from '@angular/forms';
import { FlashMessagesModule } from 'angular2-flash-messages';

import { LoginComponent } from './login.component';
import { RegisterComponent } from './register.component';
import { AuthService } from '../../services/auth.service';


import { AuthRoutingModule } from './auth-routing.module';

@NgModule({
  imports: [ AuthRoutingModule, FormsModule, FlashMessagesModule ],
  declarations: [
    LoginComponent,
    RegisterComponent
  ],
  providers: [AuthService],
})
export class AuthModule { }

      

Also I have this routing file auth-routing.module.ts

here you can view the file:

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

import { LoginComponent } from './login.component';
import { RegisterComponent } from './register.component';

const routes: Routes = [
  {
    path: '',
    data: {
      title: 'Example Pages'
    },
    children: [
      {
        path: 'login',
        component: LoginComponent,
        data: {
          title: 'Login Page'
        }
      },
      {
        path: 'register',
        component: RegisterComponent,
        data: {
          title: 'Register Page'
        }
      }
    ]
  }
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class AuthRoutingModule {}

      

Now where the problem is failing. His showing this console is a screenshot of the problem.

enter image description here

Any suggestion would be helpful. Thank! (in advance)

+3


source to share


1 answer


The problem is not the redirection, but the flash messages. In your RegisterComponent html element, you have <flash-messages></flash-messages>

; after registering a user, you 1. trigger flash messages, 2. redirect to the login page.

this.flash.show("User created successfully!", { cssClass: 'alert-success', timeout: 3000 });
this.router.navigate(['./admin/login']);

      



The problem is that the Flash messages are reported to be displayed in the RegisterComponent.html file, which is destroyed by the redirect, resulting in Flash messages trying to display its message in a corrupted Register view.

I am currently unaware of this. It looks like the app can only have flash messages in one permanent location. One solution you can use is to install the flash message selector somewhere in app.component. Another solution would be to issue a notification from RegisterComponent that receives the selected view component that you want to display in the message, and sort the "mock" flash message with a flash-like type and timeout.

0


source







All Articles