Navigate to another page with a button in angular 2

前端 未结 7 1055
天命终不由人
天命终不由人 2020-12-12 18:00

I am trying to navigate to a another page by clicking a button but it fails to work. What could be the problem. I am now learning angular 2 and it\'s a bit tough for me now.

7条回答
  •  一生所求
    2020-12-12 18:28

    Follow these steps

    Add import in your main component

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

    In the same file add below code for constructor and method

    constructor(private route: Router) {}
    public Dashbord()
    {
     this.route.navigate(['/dashboard']);
    }
    

    This is your .html file code


    In app-routing.module.ts file add dashboard

    const routes: Routes =
    [
      { path: '', pathMatch: 'full' ,component: TestComponent},
      { path: 'dashboard', component: DashboardComponent } 
    ];
    

    Good luck.

提交回复
热议问题