2016-04-08 53 views
2

我試圖從功能明確的導航路線上單擊事件Angular2類型錯誤:linkParams.forEach不是一個函數

<a (click)="loadContact()">Contact</a> 

AppComponent類這樣定義

export class AppComponent{ 

    constructor(private _router:Router){} 

    public loadContact = function(){ 
     ... 
     ... 
     this._router.navigate('[Contact]'); 
    } 
} 

但我我得到這個錯誤

TypeError: linkParams.forEach is not a function 

我該如何解決它?

編輯:

這裏是RouteConfig

@RouteConfig([ 
    { path: '/contact', name: 'Contact', component: ContactsComponent }, 
    { path: '/notes', name: 'Notes', component: NotesComponent } 
]) 
+0

帖子'Contact'路由配置? – dfsq

+0

你可以顯示AppComponent的'@ Component'裝飾器和'bootstrap()'嗎? – micronyks

+0

如果我使用'Contact',它可以工作,但那不是我想要的。當我從菜單中選擇聯繫人選項時,我需要做一些事情 – Shri

回答

7

TypeError: linkParams.forEach is not a function

錯誤原因:When a redirect, or router link is setup incorrectly and an array is not provided, an error is thrown

報道提供更好的錯誤處理太:https://github.com/angular/angular/issues/7952

你的代碼

這裏:

this._router.navigate('[Contact]'); 

是一個字符串。它應該是:

this._router.navigate(['Contact'] /* some array */); 

更多

https://angular.io/docs/ts/latest/guide/router.html#!#link-parameters-array

+0

哦!這工作..我改成'this._router.navigate(['聯繫']);' – Shri

+0

請更新答案 – Shri

+1

@shri我已經更新了答案 – basarat

1
this._router.navigate(['Contact']); 
相關問題