Angularjs 2在app.module.ts中向导入添加“路由”时出错

时间:2016-11-02 11:43:45

标签: angular routing

每当我将“路由”添加到app.module.ts

中的导入时,我都会收到错误消息
app/app.routing.ts(18,23): error TS2304: Cannot find name 'ModuleWithProvider'.
[0] app/app.routing.ts(18,65): error TS2304: Cannot find name 'AppRoutes'.

app.module.ts

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

import { AppComponent }  from './app.component';

import { SearchComponent }  from './components/search/search.component';
import { NavbarComponent }  from './components/navbar/navbar.component';
import { AboutComponent }  from './components/about/about.component';

import { routing }  from './app.routing';

@NgModule({
  imports:      [ BrowserModule, routing ],
  declarations: [ AppComponent,
                  SearchComponent,
                  NavbarComponent,
                  AboutComponent],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

路由文件

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

import {SearchComponent} from './components/search/search.component';
import {AboutComponent} from './components/about/about.component';

const appRoutes: Routes = [
  {
    path:'',
    component:SearchComponent
  },
  {
    path:'about',
    component:AboutComponent
  }
];

export const routing: ModuleWithProvider = RouterModule.forRoot(AppRoutes);

1 个答案:

答案 0 :(得分:0)

好的,这是解决方案。

您的代码中有两个拼写错误。

路由文件

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

import {SearchComponent} from './components/search/search.component';
import {AboutComponent} from './components/about/about.component';

const appRoutes: Routes = [
  {
    path:'',
    component:SearchComponent
  },
  {
    path:'about',
    component:AboutComponent
  }
];

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

在最后一行,你编写了ModuleWithProvider而不是ModuleWithProvider s ,还有AppRoutes而不是 a ppRoutes