【问题标题】:angular2 with typescript inject Router error带有打字稿注入路由器错误的angular2
【发布时间】:2016-08-08 01:58:28
【问题描述】:

我有一个 select 元素,它通过 for 循环生成选项:

 <select (change)="setSelectedSome($event.target.value)">
     <option *ngFor="#some of Something" [selected]="SelectedSome == some" [value]="some.path" (click)="navigate(some.path)">{{some.name}}</option>
 </select> 

根据选择,我需要导航到一个组件。我更喜欢使用[routerLink],但由于将静态字符串解析为值,这似乎不起作用。因此,使用动态导航,我似乎无法将路由器注入构造函数。以下是错误:

例外:无法解析“Router”的所有参数(RouteRegistry、Router、?、Router)。确保所有参数都用 Inject 修饰或具有有效的类型注释,并且“Router”用 Injectable 修饰。

这里是组件:

/// <reference path="angular2/router.d.ts" />

import {Router, ROUTER_PROVIDERS, ROUTER_DIRECTIVES, LocationStrategy, HashLocationStrategy} from 'angular2/router';

@RouteConfig([
    { path: '/', name: 'Page', component: Page },
])
@Component({
    providers: [Router],
    directives: [ROUTER_DIRECTIVES],
    selector: 'page',
    template:
    '<div>
        <select (change)="setSelectedSome($event.target.value)">
           <option *ngFor="#some of Something" [selected]="SelectedSome == some" [value]="some.path" (click)="navigate(some.path)">{{some.name}}</option>
        </select>
        <router-outlet></router-outlet>
    </div>'
})
export class Page {
    constructor(private _router: Router) { }

    Something= [{path:'/Some', name: 'I'm Something!'];
    SelectedSome = this.Something[0];

    setSelectedSome(value) {
        this.SelectedSome = value;
    }

    navigate(routeName) {
        //Here i need to navigate, or if you can tell me how to use routerLink binding even better!
        this._router.navigate(routeName);
    }
}

bootstrap(Page, [ROUTER_PROVIDERS, provide(LocationStrategy, { useClass: HashLocationStrategy })]);

我尝试过的:

1。 从构造函数中移除了private 关键字,但_routernavigate(routeName) 方法中变得不可访问。

2。 将 Inject 添加到来自 Angular2/core 的导入列表中。然后将构造函数更改为:

constructor(@Inject() _router: Router) 
{ 
}

仍然没有工作。我该如何解决这个问题?

【问题讨论】:

    标签: typescript angular


    【解决方案1】:

    删除以下行:

    providers: [Router]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-27
      • 2023-01-03
      • 2022-10-13
      • 2021-07-11
      • 2016-04-23
      • 2017-11-09
      • 2017-10-30
      • 1970-01-01
      相关资源
      最近更新 更多