请告诉我我的错误在哪里,我的应用程序运行AppComponent代码两次。 我有5个文件:
main.ts:
import { bootstrap } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppComponent, environment } from './app/';
import { APP_ROUTER_PROVIDERS } from './app/routes';
import {HTTP_PROVIDERS} from '@angular/http';
import {ServiceProvider} from "./app/providers/app.service.provider"
if (environment.production) {
enableProdMode();
}
bootstrap(AppComponent, [ServiceProvider, APP_ROUTER_PROVIDERS, HTTP_PROVIDERS]);
routes.ts :
import {provideRouter, RouterConfig} from '@angular/router';
import {AppComponent} from "./app.component";
import {ReportDetailComponent} from "./component/AppReportDetailComponent";
import {ReportsListComponent} from "./component/AppReportListComponent";
import {ReportCreateComponent} from "./component/AppReportCreateComponent";
export const routes:RouterConfig = [
{
path: 'reports',
children: [
{path: ':id', component: ReportDetailComponent},
{path:'', component: AppComponent },
{path: 'create', component: ReportCreateComponent},
{path: 'list', component: ReportsListComponent},
]
}
];
export const APP_ROUTER_PROVIDERS = [provideRouter(routes)];
app.component:
import {Component, OnInit} from '@angular/core';
import {ReportNavComponent} from "./component/AppReportNavComponent";
import { ROUTER_DIRECTIVES } from '@angular/router';
@Component({
moduleId: module.id,
selector: 'app-root',
templateUrl: 'tpl/app.component.html',
styleUrls: ['app.component.css'],
directives: [ROUTER_DIRECTIVES, ReportNavComponent]
})
export class AppComponent {
constructor() {
}
}
app.component.html:
<report-nav></report-nav>
<router-outlet></router-outlet>
并持续 AppReportNavComponent.ts:
import {Component} from "@angular/core";
import {ROUTER_DIRECTIVES} from '@angular/router';
@Component({
selector: "report-nav",
directives: [ROUTER_DIRECTIVES],
template: `<nav>
<a [routerLink]="['/list']">List</a>
<a [routerLink]="['/create']">Create new</a>
</nav>`
})
export class ReportNavComponent {
constructor() {
}
}
如果我去/报告我应该看到两个链接“列表”和“创建” 但是在app-root标签里面我看到了辅助app-root标签(在图片上) screenshot 我的问题是:为什么?
答案 0 :(得分:14)
由于您的默认路线指向AppComponent
,因此您的路线正在AppComponent
内呈现AppComponent
:
{path:'', component: AppComponent },
您可能应该放置DashboardComponent
或HomeComponent
。