cant use [routerLink] inside the component

寵の児 提交于 2019-12-11 12:46:44

问题


Im have 2 <router-outlet> and everything is ok,this my RouteConfig inside the profile component:

@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. in [null]

and this is my credit Component :

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?


回答1:


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.

So, exception occurs because:

  • Profile in the routerLink means look for the route in this component's routeConfig.

  • but you need to do /Profile in routerLink to tell it to look for Profile route in the root(Main Component)'s routeConfig



来源:https://stackoverflow.com/questions/37361254/cant-use-routerlink-inside-the-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!