redirect to another component angular without routing code example
Example 1: redirect angular
import { RoleComponent } from './role/role.component';
import { AppComponent } from './app.component';
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
const routes: Routes = [
{ path: '', redirectTo: '/AppComponent', pathMatch: 'full' },
{ path: 'role', component: RoleComponent },
];
@NgModule({
imports: [ RouterModule.forRoot(routes) ],
exports: [ RouterModule ]
})
export class AppRoutingModule {
constructor(private router: Router)
}
functionOnWhichRedirectShouldHappen(){
router.navigate(['/role']);
}
Example 2: how to add changes every time you route navigate to page in angular
import { Router,NavigationStart} from '@angular/router';
constructor(private router: Router){}
ngOnInit(){
this.router.events.subscribe(event =>{
if (event instanceof NavigationStart){
}
})
}