我正在尝试学习角度2路由并且我被困在这里我无法获得要显示的子组件的模板。我有以下两页
page1.ts
import {Component} from 'angular2/core';
@Component({
selector: "page1",
template: `page 1 goes here.`
})
export class Page1Cmp{}
page2.ts
import {Component} from 'angular2/core';
import {RouterLink,RouteConfig} from 'angular2/router';
import {ChildCmp} from './child';
@Component({
selector: "page2",
template: `Hello its page 2
<router-outlet></router-outlet>`
})
@RouteConfig([
{path: "/", component: ChildCmp, name: "Child", useAsDefault: true}
])
export class Page2Cmp{}
这是page2.ts尝试路由的子组件
child.ts
import {Component} from 'angular2/core';
@Component({
selector: "child",
template: `child content goes here.`
})
export class ChildCmp{}
此子组件未在第2页上显示 这是根组件
app.ts
import {Component} from "angular2/core";
import {bootstrap} from "angular2/platform/browser";
import {ROUTER_DIRECTIVES, RouteConfig, ROUTER_PROVIDERS} from "angular2/router";
import {Page1Cmp} from './page1';
import {Page2Cmp} from './page2';
@Component({
selector: "app",
template: `<a [routerLink]="['Page1']">Page1</a> | <a [routerLink]="['Page2']">Page2</a>
<router-outlet></router-outlet>
`,
directives: [ROUTER_DIRECTIVES]
})
@RouteConfig([
{path: "/page1", name: "Page1", component: Page1Cmp, useAsDefault: true},
{path: "/page2/...", name: "Page2", component: Page2Cmp}
])
class MyApp{}
bootstrap(MyApp, [
ROUTER_PROVIDERS
]);
当我路由到page2时,我没有看到child.ts组件的模板。我在plunker here
上重新解决了这个问题答案 0 :(得分:0)
<强> plunker 强>
import {RouterLink,RouteConfig,ROUTER_DIRECTIVES} from 'angular2/router';
@Component({
selector: "page2",
directives: [ROUTER_DIRECTIVES],
//add this and it will start working as expected.
template: `<br>Hello its page 2<br>
<router-outlet></router-outlet>`
})