【问题标题】:Angular2 TypeError: linkParams.forEach is not a functionAngular2 TypeError:linkParams.forEach 不是函数
【发布时间】:2016-04-08 05:51:21
【问题描述】:

我正在尝试从点击事件中的函数显式导航到路由

<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([
    { path: '/contact', name: 'Contact', component: ContactsComponent },
    { path: '/notes', name: 'Notes', component: NotesComponent }
])

【问题讨论】:

  • 发布Contact路由配置?
  • 你能展示AppComponent的@Component装饰器和bootstrap()吗?
  • 如果我使用&lt;a [routerLink]="['Contact']"&gt;Contact&lt;/a&gt;,它会起作用,但这不是我想要的。当我从菜单中选择联系选项时,我需要做一些事情
  • 不需要更多代码。对navigate 的调用是错误的????

标签: typescript angular angular2-routing


【解决方案1】:

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

【讨论】:

【解决方案2】:
 this._router.navigate(['Contact']);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多