Navigate to another page with a button in angular 2

前端 未结 7 1042
天命终不由人
天命终不由人 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:10
     <button type="button" class="btn btn-primary-outline pull-right" (click)="btnClick();"><i class="fa fa-plus"></i> Add</button>
    
    
    import { Router } from '@angular/router';
    
    btnClick= function () {
            this.router.navigate(['/user']);
    };
    
    0 讨论(0)
  • 2020-12-12 18:13

    It is important that you decorate the router link and link with square brackets as follows:

    <a [routerLink]="['/service']"> <button class="btn btn-info"> link to other page </button></a>
    

    Where "/service" in this case is the path url specified in the routing component.

    0 讨论(0)
  • 2020-12-12 18:15

    you can change

        this.router.routeReuseStrategy.shouldReuseRoute = () => false;
    

    at the component level in constructor like bellow

        constructor(private router: Router) {
                this.router.routeReuseStrategy.shouldReuseRoute = () => false; 
    }
    
    0 讨论(0)
  • 2020-12-12 18:19

    Having the router link on the button seems to work fine for me:

    <button class="nav-link" routerLink="/" (click)="hideMenu()">
         <i class="fa fa-home"></i> 
         <span>Home</span>
    </button>
    
    0 讨论(0)
  • 2020-12-12 18:25

    Use it like this, should work:

     <a routerLink="/Service/Sign_in"><button class="btn btn-success pull-right" > Add Customer</button></a>
    

    You can also use router.navigateByUrl('..') like this:

    <button type="button" class="btn btn-primary-outline pull-right" (click)="btnClick();"><i class="fa fa-plus"></i> Add</button>    
    
    import { Router } from '@angular/router';
    
    btnClick= function () {
            this.router.navigateByUrl('/user');
    };
    

    Update

    You have to inject Router in the constructor like this:

    constructor(private router: Router) { }
    

    only then you are able to use this.router. Remember also to import RouterModule in your module.

    Update 2

    Now, After Angular v4 you can directly add routerLink attribute on the button (As mentioned by @mark in comment section) like below -

     <button [routerLink]="'/url'"> Button Label</button>
    
    0 讨论(0)
  • 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

    <button mat-button (click)="Dashbord()">Dashboard</button><br>
    

    In app-routing.module.ts file add dashboard

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

    Good luck.

    0 讨论(0)
提交回复
热议问题