0

您好所有 Angular2 开发人员,

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

我的 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;
}

left-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{

}

应用程序.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

4

1 回答 1

4

在您的 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); <-----delete this! 

删除第二个,你应该很高兴

于 2016-08-09T04:25:31.757 回答