gpt4 book ai didi

javascript - Angular 2 - 从 `first child` 组件路由到父组件的 `second child` 组件

转载 作者:行者123 更新时间:2023-11-29 19:13:41 24 4
gpt4 key购买 nike

我正在尝试执行从子组件到父组件的另一个子组件的简单路由。 router-outlet 在父组件中定义。这是我试图实现这一目标的方式:

parent.ts

import {Component} from 'angular2/core';
import {RouteConfig,ROUTER_PROVIDERS,ROUTER_DIRECTIVES} from 'angular2/router';
import {bootstrap} from 'angular2/platform/browser';
import {FirstChildCmp} from "./first_child";
import {SecondChildCmp} from "./second_child";

@Component({
selector: 'app',
template: `
<router-outlet></router-outlet>
`,
directives: [ROUTER_DIRECTIVES]
})
@RouteConfig([
{path: '/first_child', component: FirstChildCmp, name: 'FirstChild', useAsDefault:true},
{path: '/second_child',component: SecondChildCmp, name:'SecondChild'}
])
export class ParentCmp{}

bootstrap(ParentCmp,[
ROUTER_PROVIDERS
]);

first_child.ts

import {Component} from 'angular2/core';
import {ROUTER_DIRECTIVES} from 'angular2/router';

@Component({
selector: 'child',
template: `
<a [routerLink]="[SecondChild]">Go to second child</a>
`,
directives: [ROUTER_DIRECTIVES]
})
export class FirstChildCmp{}

second_child.ts

import {Component} from 'angular2/core';

@Component({
selector: 'child',
template: `
This is second child.
`
})
export class SecondChildCmp{}

当用户点击 Go to second child 我想显示第二个子组件的模板。但是我收到以下错误:

Component "FirstChildCmp" has no route config.

但我不想在 FirstChildCmp 中定义配置,而是想使用父级的配置。我在plunker here上重现了这个问题

最佳答案

FirstChild.ts Plnkr

@Component({
selector: 'child',
template: `
<a [routerLink]="['SecondChild']">Go to second child</a>
`,
directives: [ROUTER_DIRECTIVES]
})
export class FirstChildCmp{}

['SecondChild'] 中你漏掉了引号。它必须是一个字符串。

关于javascript - Angular 2 - 从 `first child` 组件路由到父组件的 `second child` 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36887095/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com