angular2中的嵌套路由

时间:2016-08-09 03:15:58

标签: angular angular2-router

Hello All Angular2 Developers,

这是我面临的挑战,不知道如何解决它。

我的app.component.ts文件如下:

import { Component } from '@angular/core';
import {ROUTER_DIRECTIVES} from '@angular/router';

import { LeftMenuComponent } from './left-menu.component';
import {HeaderComponent} from './header.component';
import {TotalContainer} from './container-total.component';

@Component({
  selector: 'my-app',
  template: `
    <myheader [display]="display"></myheader>
    <router-outlet></router-outlet>
  `,
  directives:[HeaderComponent,TotalContainer,ROUTER_DIRECTIVES]
})
export class AppComponent { 
    display = true;
}

我已经在header.component中编写了标题的所有视图,并以HTML格式加载。在“header.component”中,我正在加载“left-menu.component”。在left-menu.component中,我有超链接要加载到“app.component”中。

我得到的错误是“没有路由器提供商”。

我继续以angular.io样本为基础,但无法解决它。

以下是我编写代码的方法。 header.component.ts

import {Component, Input} from '@angular/core';
import {ROUTER_DIRECTIVES} from '@angular/router';

import {LeftMenuComponent} from './left-menu.component';

@Component({
    selector:'myheader',
    template:`
        <nav class="navbar navbar-default">
            <div>
             ....
            </div>
        </nav>
        <left-menu [class.disp]="display"></left-menu>

    `,
    directives:[ROUTER_DIRECTIVES, LeftMenuComponent]
})
export class HeaderComponent{
    @Input() display;
}

左menu.component.ts

import {Component} from '@angular/core';
import {ROUTER_DIRECTIVES} from '@angular/router';

import {KeyEventsPlugin} from '@angular/platform-browser/src/dom/events/key_events';

@Component({
    selector:'left-menu',
    template:`
        <ul class="list-group">
            <li class="list-group-item">
                <span class="glyphicon glyphicon-share-alt"></span>
                <span class="badge">14</span>
                    <a [routerLink]="['/followups']" routerLinkActive="active-link">Follow Ups</a>
            </li>
        </ul>
    `,
    directives:[ROUTER_DIRECTIVES]
})
export class LeftMenuComponent{

}

app.routes.ts

import { provideRouter, RouterConfig }  from '@angular/router';
import {FollowUpsComponent} from '../components/followups.component';
import {TotalContainer} from '../components/container-total.component';

const routes: RouterConfig = [
    {
        path:'total-container',
        component:TotalContainer
    },
    {
        path:'followups',
        component: FollowUpsComponent,
    },
    {
        path:'',
        redirectTo:'/total-container',
        pathMatch:'full'
    },
    {
        path:'**',
        redirectTo:'/total-container'
    }
];


export const appRouterProviders = [
    provideRouter(routes)
];

main.ts

import { bootstrap }    from '@angular/platform-browser-dynamic';
import { AppComponent } from './components/app.component';

import { appRouterProviders } from './routes/app.routes'; 
bootstrap(AppComponent,[
    appRouterProviders
]); 

bootstrap(AppComponent);

任何人都可以帮助我.a

1 个答案:

答案 0 :(得分:4)

在你的main.ts中,你似乎意外地两次调用bootstrap:

import { bootstrap }    from '@angular/platform-browser-dynamic';
import { AppComponent } from './components/app.component';

import { appRouterProviders } from './routes/app.routes'; 
bootstrap(AppComponent,[
    appRouterProviders
]); 

bootstrap(AppComponent); <-----delete this! 

删除第二个,你应该好好去