angular2路由上的路由数组出错

时间:2017-04-13 17:31:23

标签: angular

这是我的app.module.ts文件

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { RouterModule, Routes} from '@angular/router';

import { AppComponent } from './app.component';
import { CronoComponent } from './crono/crono.component';
import { ClockFactory } from './classes/clockFactory';
import { PruebaComponent } from './prueba/prueba.component';

const routes: Routes = [
  { path:'', redirectTo:'home'},
  { path:'crono', component:'CronoComponent' },
  { path:'prueba', component:'PruebaComponent' }
];

@NgModule({
  declarations: [
    AppComponent,
    CronoComponent,
    PruebaComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    RouterModule.forRoot(routes)
  ],
  providers: [ClockFactory],
  bootstrap: [AppComponent]
})
export class AppModule { }

我在routes数组声明中有一个错误,说路由的内容不能分配给Routes数组。这是它显示的完整错误消息

Type '({ path: string; redirectTo: string; } | { path: string; component: string; })[]' is not assignable to type 'Route[]'.
  Type '{ path: string; redirectTo: string; } | { path: string; component: string; }' is not assignable to type 'Route'.
    Type '{ path: string; component: string; }' is not assignable to type 'Route'.
      Types of property 'component' are incompatible.
        Type 'string' is not assignable to type 'Type<any>'

1 个答案:

答案 0 :(得分:0)

  1. 要使用redirectTo,您必须使用pathMatch
  2. component不应该是字符串,而是组件本身。
  3. Official documentation