Answers for "navigate to different component angular"

6

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']);
}
Posted by: Guest on February-27-2020
2

angular navigate using component

//ng new routing-app --routing
//ng generate component first
//ng generate component second
    
import { Router } from '@angular/router';

...

export class AboutUserComponent implements OnInit {
  user: User;

  constructor(
    private route: ActivatedRoute, 
    private service: UserService,
    private router: Router
  ) {}

  ngOnInit() {
    // grab the current username
    let username = this.route.snapshot.params['username'];
    this.service.getUser(username).then(user => this.user = user);
  }

  goBack() {
    this.router.navigate(['/about']);
  }

}
Posted by: Guest on September-01-2020
16

angular navigate using component

import {Router} from '@angular/router'; // import router from angular router

export class Component{ 				// Example component.. 
	constructor(private route:Router){} 
  
  	go(){
		this.route.navigate(['/page']); // navigate to other page
	}
}
Posted by: Guest on May-08-2020

Code answers related to "navigate to different component angular"

Code answers related to "Javascript"

Browse Popular Code Answers by Language