子组件中的Angular2 RouterLink期望在父组件中定义路由

时间:2016-02-10 07:22:01

标签: typescript angular angular2-routing

我使用两个组件,一个是父组件,第二个组件是其模板中的子组件。现在在子组件中,我已经定义了一个路由,并在子组件的模板中放置了路由器插座和路由器链接。因此,当我点击此链接时,它应该更新其自己的路由器插座的内容,但它给出以下错误,并且正在搜索要在其父组件中定义的路由:

EXCEPTION: Component "ParentComponent" has no route config.in[['ChildRoute'] in ChildComponent@2: 26]

组件是:

@Component({
    selector: 'parent-cmp',
    template: '<child-cmp></child-cmp>',
    directives: [
        ROUTER_DIRECTIVES,
        ChildComponent
    ]
})
export class ParentComponent { }

@Component({
    selector: 'child-cmp',
    template: `
        <router-outlet></router-outlet>
        <a [routerLink]="['ChildRoute']">Edit</a>
    `,
    directives: [ROUTER_DIRECTIVES]
})
@RouteConfig([
    { path: '/edit', name: 'ChildRoute', component: ChildEditComponent }
])
export class ChildComponent { }

假设进口是正确的,为了简洁,我在这里没有提及。

4 个答案:

答案 0 :(得分:4)

当您建立到子路线的链接时,

之前定义路线父亲

试试这个,

<a [routerLink]="['ParentRoute','ChildRoute']">Edit</a>

答案 1 :(得分:1)

要从父级访问子路由,请始终使用

<a [routerLink]=['ParentRouteElement','ChildRouteElement']

并且还应该有一个childrouteElement asDefault。

答案 2 :(得分:0)

在主要路线配置中,您应该:

@RouteConfig([
  { name: "Videos", component: ChildrenCmp, path: "/videos/..."}
])

比子组件:

@RouteConfig([
  { name: "ChildCmp" component: ChildCmp, path: "/",
useAsDefault: true}])

Working example on Plunker

答案 3 :(得分:0)

当您引导应用程序时,您可能已将 #307 = B_SPLINE_CURVE_WITH_KNOTS ( 'NONE', 3,( #114, #212, #179, #317, #91, #191, #141, #97, #150, #63, #121, #243, #75, #43, #15, #164, #315, #284, #302, #70, #269, #251, #151, #220, #178, #172, #248, #185, #156, #249, #300, #47, #124, #335, #360, #59, #357, #343, #12, #5, #112, #324, #344, #193, #329, #320, #313, #222, #51, #167, #286, #268, #310, #32, #267, #52, #66, #69 ),.UNSPECIFIED., .T., .F., ( 4, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4 ), ( 0.0000000000000000000, 0.0003907630664662950000, 0.0007815261329325900100, 0.001172289199398885000, 0.001563052265865180000, 0.002344578398797811600, 0.003126104531730443300, 0.003516867598196761600, 0.003907630664663080300, 0.004298393731129399400, 0.004689156797595717700, 0.005079919864062035900, 0.005470682930528354200, 0.005861445996994673300, 0.006252209063460992400, 0.006642972129927311600, 0.007033735196393629000, 0.007815261329326231600, 0.008206024395792531700, 0.008596787462258833500, 0.008987550528725133500, 0.009378313595191433600, 0.009769076661657735300, 0.01015983972812403500, 0.01055060279459033500, 0.01094136586105663500, 0.01133212892752293700, 0.01172289199398923700, 0.01250441812692185600 ), .UNSPECIFIED. ) ; 引用为路由器主要组件而不是ParentComponent

您可能需要具有以下内容:

ChildComponent