Angular 2 - การกำหนดเส้นทางจากองค์ประกอบ 'ลูกคนแรก' ไปยังองค์ประกอบ 'ลูกคนที่สอง' ขององค์ประกอบหลัก

ฉันกำลังพยายามกำหนดเส้นทางแบบง่ายจากองค์ประกอบลูกไปยังองค์ประกอบลูกอื่นขององค์ประกอบหลัก 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 ฉันต้องการแสดงเทมเพลตขององค์ประกอบลูกที่สอง แต่ฉันได้รับข้อผิดพลาดดังต่อไปนี้:

ส่วนประกอบ "FirstChildCmp" ไม่มีการกำหนดค่าเส้นทาง

แต่ฉันไม่ต้องการกำหนดการกำหนดค่าใน FirstChildCmp ฉันต้องการใช้การกำหนดค่าของผู้ปกครองแทน ฉันได้สร้างปัญหาซ้ำแล้วซ้ำอีกใน phunker ที่นี่


person Eesa    schedule 27.04.2016    source แหล่งที่มา


คำตอบ (1)


FirstChild.ts Plnkr

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

ใน ['SecondChild'] คุณพลาดเครื่องหมายคำพูด มันจะต้องมีสตริง

person Ankit Singh    schedule 27.04.2016