我刚刚开始使用新的路由库(@ angular / router v3.0.0-alpha.7),但是遵循官方文档会导致以下错误:
browser_adapter.ts:74: EXCEPTION: Error: Uncaught (in promise): Error: Cannot find primary outlet to load 'HomePage'
问题是-如何摆脱错误并使路由器表现出预期的效果?我错过了设置吗?
(使用alpha.6版本时,会出现相同的错误。)
app.component.ts
import { Component } from '@angular/core';
import { ROUTER_DIRECTIVES } from '@angular/router';
@Component({
selector: 'app',
template: `
<p>Angular 2 is running...</p>
<!-- Routed views go here -->
<router-outlet></router-outlet>
`,
providers: [ROUTER_DIRECTIVES]
})
export class AppComponent {
}
app.routes.ts
import { provideRouter, RouterConfig } from '@angular/router';
import { HomePage } from './pages/home/home';
export const routes: RouterConfig = [
{ path: '', component: HomePage }
];
export const APP_ROUTER_PROVIDERS = [
provideRouter(routes)
];
home.ts
import { Component } from '@angular/core';
@Component({
template: '<h1>Home Page</h1>'
})
export class HomePage {
}
主要
/* Avoid: 'error TS2304: Cannot find name <type>' during compilation */
///<reference path="../../typings/index.d.ts" />
import { bootstrap } from "@angular/platform-browser-dynamic";
import { APP_ROUTER_PROVIDERS } from './app.routes';
import { AppComponent } from "./app.component";
bootstrap(AppComponent, [
APP_ROUTER_PROVIDERS,
]).catch(err => console.error(err));
'<router-outlet></router-outlet>'
这也可能是由于拼写错误所致'<router-outlet></router-outlet>'