Angular 2:无法解析Router的所有参数

时间:2016-09-07 14:44:12

标签: angular angular-routing

目标

让路由工作而不失去理智。

错误

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>

4 个答案:

答案 0 :(得分:8)

现在感谢较旧的帖子,但我遇到了同样的问题并且刚刚解决了它。

错误建议Angular无法解析Router类的某些依赖项。

<强> 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],
})

我通过不将路由器注入提供程序来修复此问题,并且仅注入构造函数,因此最终得到:

@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的新手,所以我无法详细说明原因!

答案 1 :(得分:1)

以下是我使用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>

答案 2 :(得分:1)

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

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

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

答案 3 :(得分:0)

由于您的路由器插座位于 app.component.html 添加

import {ROUTER_DIRECTIVES} from '@angular/router'

directives: [ROUTER_DIRECTIVES]

app.component.ts

我的设置就像那样,它正在发挥作用。