我正在尝试从点击事件
上的函数显式导航到路线<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 :(得分: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']);