简体   繁体   English

不能在组件内部使用[routerLink]

[英]cant use [routerLink] inside the component

Im have 2 <router-outlet> and everything is ok,this my RouteConfig inside the profile component: 我有2个<router-outlet> ,一切正常,这是Profile组件内的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' }
])

my problem is when I want use [routerLink] inside the credit component throw an Error: "credit" has no route config. 我的问题是,当我想在credit组件内使用[routerLink]时抛出错误:“信用”没有路由配置。 in [null] 在[null]中

and this is my credit Component : 这是我的credit组成部分:

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

})

template : 模板:

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

why i should use RouteConfig inside credit Component?and how to use? 为什么我应该在credit Component内使用RouteConfig?如何使用?

Try this 尝试这个

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

As I see, you have defined Profile route in the Main Component and then Profile has child Components ie: Home . 如我所见,您已经在Main Component中定义了Profile路由,然后Profile具有子Components,即: Home

So, exception occurs because: 因此,发生异常是因为:

  • Profile in the routerLink means look for the route in this component's routeConfig. routerLink中的Profile意味着在该组件的routeConfig中查找路由。

  • but you need to do /Profile in routerLink to tell it to look for Profile route in the root(Main Component)'s routeConfig 但是您需要在routerLink中执行/Profile ,以告诉它在根(主组件)的routeConfig中查找Profile路由

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM