2017-01-02 72 views
23

我試圖通過單擊按鈕導航到另一個頁面,但它無法工作。可能是什麼問題呢。我現在正在學習角2,現在對我來說有點困難。導航到角度爲2的按鈕的另一個頁面

//Routes/Path in a folder call AdminBoard 

export const AdminRoutes: Routes =[ 

    { 
    path: 'dashboard', 

    component: AdminComponent, 
    children: [ 
     {path: '', redirectTo: 'Home'}, 
     {path: 'Home', component: HomeComponent}, 
     {path: 'Service', component: ServiceComponent}, 
     {path: 'Service/Sign_in', component:CustomerComponent} 

    ] 

    } 

]; 

//Button is also in a different folder. Click button to navigate to this page   {path: 'Service/Sign_in', component:CustomerComponent} 

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

可能重複的[Angular 2重定向點擊](http://stackoverflow.com/questions/37252146/angular-2-redirect-on-click) – Habeeb

+1

嘗試這樣的事情:'' –

回答

56

使用它這個樣子,應該工作:

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

您也可以使用router.navigateByUrl('..')這樣的:

<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'); 
}; 

更新

您必須初始化router這樣的構造函數:

constructor(private router: Router) { } 

只有那麼你可以使用this.router

4
<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

應該將btnClick函數放在導出類AppModule {}? – Jurassic

8

您可以通過以下方式使用routerLink,

<input type="button" value="Add Bulk Enquiry" [routerLink]="['../addBulkEnquiry']" class="btn"> 

或在您的情況使用<button [routerLink]="['./url']">,更多信息,你可以在GitHub上讀取整個堆棧跟蹤https://github.com/angular/angular/issues/9471

的方法也是正確的,但他們創建了對組件文件的依賴關係。

希望您的關注得到解決。

+1

正是我在找的,謝謝。 –

相關問題