Angular 2: 无法解析路由器的所有参数

Angular 2: Can't resolve all parameters for Router

目标

在不失去理智的情况下让路由正常工作。

错误

Error: Can't resolve all parameters for Router: (?, ?, ?, ?, ?, ?, ?, ?)

app.routing.ts

import { ModuleWithProviders } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

import { NavbarComponent } from './navbar/navbar.component';
import { CustomerComponent } from './customer/customer.component';

export const appRoutes: Routes = [
    { path: '', redirectTo: 'customers', pathMatch: 'full' },
    { path: 'customers', component: CustomerComponent },
];

export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);

app.module.ts

import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { BrowserModule } from '@angular/platform-browser';

import { routing } from './app.routing';
import { AppComponent } from './app.component';
import { NavbarComponent } from './navbar/navbar.component';
import { CustomerComponent } from './customer/customer.component';

@NgModule({
    imports: [
        BrowserModule,
        FormsModule,
        routing
    ],
    declarations: [
        AppComponent,
        NavbarComponent,
        CustomerComponent,
    ],
    providers: [
        // ...
    ],
    bootstrap: [ AppComponent ]
})
export class AppModule {
    // ...
}

app.component.ts

import { RouterLink } from '@angular/router';
import { HTTP_PROVIDERS } from '@angular/http';
import { Component } from '@angular/core';

import { NavbarComponent } from './navbar/navbar.component';
import { CustomerComponent } from './customer/customer.component';

export { Config } from './config/env.config';

@Component({
    moduleId: module.id,
    selector: 'app',
    templateUrl: 'app.component.html',
    styleUrls: ['app.component.css'],
    directives: [NavbarComponent, CustomerComponent],
    providers: [HTTP_PROVIDERS, RouterLink]
})
export class AppComponent
{
    constructor() {
        // console.log('Environment config', Config);
    }

    ngOnInit() {
        // ...
    }
}

navbar.component.ts

import { Router, ROUTER_DIRECTIVES } from '@angular/router';
import { Component } from '@angular/core';

@Component({
    moduleId: module.id,
    selector: 'navbar',
    templateUrl: 'navbar.component.html',
    styleUrls: ['navbar.component.css'],
    directives: [ROUTER_DIRECTIVES],
    providers: [Router],
})
export class NavbarComponent
{
    version: string;
    versionIsVisible: boolean;

    constructor() {
        this.version = '<%= VERSION %>';
    }

    ngOnInit() {
        // ...
    }
}

app.component.html

<navbar></navbar>

<router-outlet></router-outlet>

navbar.component.html

<a routerLink="/customers">Customers</a>

这是我使用 Angular 2 RC5 进行的个人项目中的一些代码,该项目的路由器工作正常。我直接使用了文档中的信息,因此可能对您有所帮助。

app.routing.ts

import { Routes, RouterModule } from '@angular/router';
import { HomeComponent } from '../home/home.component';
import { ApprovalsComponent } from '../approvals/approvals.component';

const appRoutes : Routes = [
{
path: 'Home', component: HomeComponent ,
data: { Title: 'Home'}
},
{
path: 'Approvals', component: ApprovalsComponent,
data: { Title: 'My Approvals'} 
}]


export const appRoutingProviders: any[] = [


];

export const routing = RouterModule.forRoot(appRoutes);

app.module.ts

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpModule } from '@angular/http'

import { AppComponent }  from './app.component';
import { HomeComponent } from '../home/home.component'
import { ApprovalsComponent } from '../approvals/approvals.component'
import { routing, appRoutingProviders } from './app.routing'

@NgModule({
   imports:      [ BrowserModule, routing, HttpModule, BrowserModule],
   declarations: [ AppComponent, HomeComponent, ApprovalsComponent],
   providers : [appRoutingProviders],
   bootstrap:    [ AppComponent ]
   })

 export class AppModule { }

html for routerlink

  <a [routerLink]="['Home']">
                    Home
                </a>

你有没有在某处声明你的路由器插座?在我的 app.html 中,我有:

 <router-outlet></router-outlet>

因为你的路由器插座在 app.component.html 添加

import {ROUTER_DIRECTIVES} from '@angular/router'

directives: [ROUTER_DIRECTIVES]

app.component.ts

我的设置是这样的,它正在运行。

感谢老 post 现在,但我遇到了同样的问题并且刚刚解决了它。

错误提示 Angular 无法解析路由器 class 的某些依赖项。

navbar.component.ts

import { Router, ROUTER_DIRECTIVES } from '@angular/router';
import { Component } from '@angular/core';

@Component({
  moduleId: module.id,
  selector: 'navbar',
  templateUrl: 'navbar.component.html',
  styleUrls: ['navbar.component.css'],
  directives: [ROUTER_DIRECTIVES],
  providers: [Router],
})

我通过不将 Router 作为提供者注入并仅注入构造函数来修复此问题,因此您最终得到:

@Component({
    moduleId: module.id,
    selector: 'navbar',
    templateUrl: 'navbar.component.html',
    styleUrls: ['navbar.component.css'],
    directives: [ROUTER_DIRECTIVES]
  })

  export class NavbarComponent
  {
    version: string;
    versionIsVisible: boolean;

    constructor(private _router: Router) {
      this.version = '<%= VERSION %>';
    }

    ngOnInit() {
      // ...
    }
}

我是 angular2 的新手,所以我不能给你一个详细的原因!

你让它工作是因为在 app.module 中导入模块时,模块可通过 angular 的内置注入器用于所有组件。换句话说,在应用程序模块中导入模块也意味着使用 angular 的内置注入器将模块注册为服务,从而使其对整个应用程序可用。在你的情况下

@NgModule({
   imports:      [ BrowserModule, routing, HttpModule, BrowserModule],
   declarations: [ AppComponent, HomeComponent, ApprovalsComponent],
   providers : [appRoutingProviders],
   bootstrap:    [ AppComponent ]
   })

"routing" 已经在 app.routing.ts 文件中导入 "RouterModule",因此通过 angular 的注入器将 RouterModule 注册为整个应用程序可用的服务。因此,它消除了在任何组件中将 RouterModule 作为服务提供的必要性。