在module.ts中
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { Router } from '@angular/router'
import { AppComponent } from './app.component';
@NgModule({
imports: [
BrowserModule
],
declarations: [
AppComponent
],
bootstrap: [ AppComponent ]
})
export class AppModule { }
组件中的
import { Component } from '@angular/core';
import { Router } from '@angular/router';
import { GlobalService } from './global.service';
@Component({
selector: 'cwf',
templateUrl: 'app/app.template.html'
})
export class AppComponent {
public homeConfig = {};
constructor(private _router:Router, private _globalService: GlobalService){}
我收到错误,因为我在构造函数中定义路由器以导航路由。如果我评论构造函数行,它工作正常。我需要添加提供商以及如何添加。
答案 0 :(得分:5)
需要导入RouterModule
@NgModule({
imports: [
BrowserModule,
RouterModule
],
或通常已经完成
const appRoutes: Routes = [
{
path: 'heroes',
component: HeroesComponent
}
];
export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);
import { routing } from './app.routing';
@NgModule({
imports: [
BrowserModule,
routing
],