Angular2 TypeError:linkParams.forEach不是函数

时间:2016-04-08 05:51:21

标签: typescript angular angular2-routing

我正在尝试从点击事件

上的函数显式导航到路线
<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 }
])

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

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