Angular2路由组件没有路由配置

时间:2016-03-29 19:18:10

标签: angular

您好我正在尝试为我的applciation创建路由系统,但在创建没有子路由的路由时,我似乎遇到了一些问题。这是我的代码:

bootstrap(CommercifyComponent, [
    ROUTER_PROVIDERS,
    HTTP_PROVIDERS,
]);   

@Component({                                                     
    selector: 'commercify',
    templateUrl: './app/commercify.view.html',
    styleUrls: ['./app/commercify.style.css'],
    directives: [ROUTER_DIRECTIVES]  
})
@RouteConfig([
    { path: '/Dashboard', name: 'Dashboard', component: DashboardComponent },       
    { path: '/Catalog/...', name: 'Catalog', component: CatalogComponent, useAsDefault : true }
])          
export class CommercifyComponent {}

这是我的CommercifyComponent视图:

这是我的DashboardComponent:

 @Component({
    selector: 'dashboard',
    template: '<base-component>This is dashboard component</base-component>',
    directives: [BaseComponent, ROUTER_DIRECTIVES]   
})
export class DashboardComponent { }

这是用户在仪表板中的基本组件:

@Component({
    selector: 'base-component',
    templateUrl: 'app/common/components/base.view.html',
    styleUrls: ['app/common/components/base.style.css'],
    directives: [LeftNavbarComponentt]
})
export class BaseComponent { }

这就是它的观点:

 <left-navbar></left-navbar>
 <ng-content></ng-content>

这是LeftNavbarComponent:

@Component({
    selector: 'left-navbar',
    templateUrl: 'app/common/components/left-navbar.view.html',
    styleUrls: ['app/common/components/left-navbar.style.css'],
    directives: [ROUTER_DIRECTIVES]
})       
export class LeftNavbarComponent {
    private router: Router;

    constructor(router: Router) {
       this.router = router;
    }

    public isRouteActive(routeName: string) {
         return this.router.isRouteActive(this.router.generate([routeName]));
     } 
}

这是左侧导航栏html:

<nav class="left-navbar navbar-inverse navbar">
<ul class="list-unstyled">
    <li class="text-center menu-item">
        <a href=""
           class="navbar-link left-navbar-link"
           [class.navbar-link-active]="isRouteActive('Dashboard')"
           [routerLink]="['Dashboard']">
            <i class="menu-icon fa fa-tachometer fa-3x"></i>
            <span class="left-arrow"></span>
            Dashboard
        </a>
        <ul class="child-menu list-unstyled">
            <li>Dashboard 1</li>
            <li>Dashboard 1</li>
            <li>Dashboard 1</li>
        </ul>
    </li>
    <li class="text-center menu-item">
        <a href="" class="navbar-link left-navbar-link"
           [class.navbar-link-active]="isRouteActive('Catalog')"
           [routerLink]="['Catalog']">
            <i class="menu-icon fa fa-shopping-basket fa-3x"></i>
            <span class="left-arrow"></span>
            Catalog
        </a>
    </li>
</ul>

这是目录组件:

@Component({
    templateUrl: './app/catalog/catalog.view.html',
    directives: [ROUTER_DIRECTIVES, BaseComponent]
})
@RouteConfig([
    { path: '/Templates', name: 'Templates', component: TemplatesComponent, useAsDefault: true },
])                                     
export class CatalogComponent {}

最后这是CatalogComponent视图:

<router-outlet></router-outlet>

从代码中可以看出,我的CommercifyComponent有2条路由配置Dashboard,它永远不会有子路由和Catalog有1个子路由模板,我省略了其余代码,因为它与当前问题无关

默认情况下,当应用程序加载时,我将转到以下URL:

http://localhost:5000/Catalog/Templates

并且设置[routerLink]="['Catalog']"的链接将具有router-link-active类,但当我尝试点击[routerLink]="['Dashboard']"的链接时,我会遇到以下例外情况:

enter image description here

查看此代码使我想要将RouteConfig添加到DashboardComponent,但我不希望为此组件提供任何子路由。

我在这里做错了什么?

1 个答案:

答案 0 :(得分:3)

似乎它不喜欢你在DashboardComponent的子(LeftNavbarComponent)上指定路由的事实,它没有子路由。所以你说你不想要仪表板组件的子路由,但是你要在它的子节点中指定路由