【发布时间】:2016-12-22 11:39:06
【问题描述】:
我在使用新的 Angular 2 RC5 路由器(路由器版本为 RC1)时遇到问题。
这是我从开发控制台得到的日志:
EXCEPTION: Error in /templates/app.component.html:2:0
ORIGINAL EXCEPTION: No provider for Router!
这是我的 app.modules.ts 的样子:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { RouterModule } from '@angular/router';
import { AppComponent } from './components/app.component';
import { routing } from './app.routes';
@NgModule({
declarations: [AppComponent],
imports: [BrowserModule, RouterModule, FormsModule, HttpModule, routing],
bootstrap: [AppComponent],
})
export class AppModule {}
这是我的 boot.ts 文件:
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';
platformBrowserDynamic().bootstrapModule(AppModule);
我的 app.routes.ts 文件...
import { Routes, RouterModule } from '@angular/router';
// Components
import { HomeComponent } from './components/home.component';
const routes: Routes = [
// Root
{ path: '/', name: 'Home', component: HomeComponent, useAsDefault: true},
];
// - Updated Export
export const routing = RouterModule.forRoot(routes);
...和我的 app.component.ts 文件:
import { Component, ViewContainerRef } from '@angular/core';
@Component({
selector: 'app',
templateUrl: '/templates/app.component.html'
})
export class AppComponent {
viewContainerRef: any;
public constructor(viewContainerRef:ViewContainerRef) {
// You need this small hack in order to catch application root view container ref
this.viewContainerRef = viewContainerRef;
}
}
这里有什么我遗漏的吗?
【问题讨论】:
-
"useAsDefault" 不再是有效属性。我还将删除路径属性上的“/”并将
添加到您的 index.html 文件中。 -
路由“名称”也需要删除。
标签: javascript angularjs typescript routing