Emm*_*bin 8 javascript routing angularjs typescript
我使用新的Angular 2 RC5路由器遇到问题(路由器版本是RC1).
这是我从开发控制台获得的日志:
EXCEPTION: Error in /templates/app.component.html:2:0
ORIGINAL EXCEPTION: No provider for Router!
Run Code Online (Sandbox Code Playgroud)
这是我的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 {}
Run Code Online (Sandbox Code Playgroud)
这是我的boot.ts文件:
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';
platformBrowserDynamic().bootstrapModule(AppModule);
Run Code Online (Sandbox Code Playgroud)
我的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);
Run Code Online (Sandbox Code Playgroud)
...和我的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;
}
}
Run Code Online (Sandbox Code Playgroud)
这里有什么我想念的吗?
在app.routes.ts文件中:
添加此导入状态
import { ModuleWithProviders } from '@angular/core';
Run Code Online (Sandbox Code Playgroud)
比
export const routing: ModuleWithProviders = RouterModule.forRoot(routes);
Run Code Online (Sandbox Code Playgroud)
这应该工作.
| 归档时间: |
|
| 查看次数: |
8090 次 |
| 最近记录: |