【问题标题】:cant use [routerLink] inside the component不能在组件内使用 [routerLink]
【发布时间】:2016-09-18 13:53:23
【问题描述】:

我有 2 个<router-outlet>,一切正常,这是我在配置文件组件中的RouteConfig

@Component({
    selector : "profile",
    templateUrl: '/client/tmpl/profile.html',
    directives: [ ROUTER_DIRECTIVES],
})

@RouteConfig([
     {name: 'Home', component: homeProfile ,path: '/' ,useAsDefault:true},
     {name: 'Credit', component: credit ,path: '/credit' },
     {name: 'BuyCredit', component: buyCredit ,path: '/buycredit' }
])

我的问题是当我想在 credit 组件中使用 [routerLink] 时会抛出错误: “credit”没有路由配置。在[空]

这是我的credit 组件:

import {ROUTER_DIRECTIVES,RouteConfig} from 'angular2/router';
 @Component({
    selector : "credit",
    templateUrl : "client/tmpl/profile/credit.html",
    directives: [ROUTER_DIRECTIVES]

})

模板:

<ul>
                                 <li><a  [routerLink]="['Profile' , 'Home']"  href="#">home</a> </li>
</ul>

为什么要在credit Component里面使用RouteConfig?以及如何使用?

【问题讨论】:

    标签: javascript angularjs routes angular angular2-routing


    【解决方案1】:

    试试这个

    <a [routerLink]="['/Profile' , 'Home']"  href="#">home</a>
    

    如我所见,您在主组件中定义了Profile 路由,然后Profile 具有子组件,即:Home

    所以,发生异常是因为:

    • routerLink中的Profile表示在该组件的routeConfig中查找路由。

    • 但您需要在 routerLink 中执行 /Profile 以告诉它在根(主组件)的 routeConfig 中查找 Profile 路由

    【讨论】:

      猜你喜欢
      • 2016-08-02
      • 2017-04-24
      • 2016-03-25
      • 2020-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-27
      • 1970-01-01
      相关资源
      最近更新 更多