使用angular 4/5进行路由的新手,我正在他们的网站上关注角度教程。
我有一个角度应用程序,我希望能够有两个单独的页面。现在主页是localhost:8870/dr3-interface
,我想在localhost:8870/dr3-interface/manage_cycles_instances
我的问题是当我点击我的链接Manage cycles instances
时,它会显示我的所有app.component
组件,而不仅仅是我决定在/manage_cycles_instances
上显示的组件。我试图使用*ngIf
隐藏它们但没有结果。有什么想法吗?
app.component.html:
<div style="text-align:left">
<h1>{{ title }}</h1>
</div>
<nav>
<a routerLink="/manage_cycles_instances" routerLinkActive="active"> Manage cycles instances</a>
</nav>
<router-outlet></router-outlet>
<div *ngIf="router = '/dr3-interface'">
<h2><d-table></d-table></h2>
</div>
<br/>
<form-upload></form-upload>
<br/>
<list-upload></list-upload>
app-routing.module.ts:
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { DataTableComponent } from './datatable/data-table.component';
const appRoutes: Routes = [
{ path: 'manage_cycles_instances', component: DataTableComponent },
/* TO DO : page error
{ path: '**', component: ... }
*/
];
@NgModule({
imports: [
RouterModule.forRoot(
appRoutes,
{ enableTracing: true } // <-- debugging purposes only
)
],
exports: [
RouterModule
]
})
export class AppRoutingModule {}
app.module.ts:
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { NgModule } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import {
//all material modules
} from '@angular/material';
import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
import { DataTableComponent } from './datatable/data-table.component';
import { DetailsUploadComponent } from './upload/details-upload/details-upload.component';
import { FormUploadComponent } from './upload/form-upload/form-upload.component';
import { ListUploadComponent } from './upload/list-upload/list-upload.component';
@NgModule({
imports: [
BrowserModule,
AppRoutingModule,
BrowserAnimationsModule,
HttpClientModule,
//material modules
],
declarations: [
AppComponent,
DataTableComponent,
DetailsUploadComponent,
FormUploadComponent,
ListUploadComponent
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {
}
答案 0 :(得分:5)
您的路线应为:
const appRoutes: Routes = [
{ path: 'dr3-interface', component: DrThreeComponent, children: [ // I don't know which component to use for this route
{ path: 'manage_cycles_instances', component: DataTableComponent },
]},
];
因为您希望拥有嵌套路由,所以应该创建嵌套路由。请注意,DrThreeComponent
应该有router-outlet
,因为它有子女。
您不需要在代码中使用条件,因为路由器将处理组件的显示。
首先要有一个 index.html 文件。它只包含一个标签,通常是app-root
。此标记将由您的引导组件替换,通常为AppComponent
。
如果您要路由您的应用程序,则需要使用路由器。需要几个步骤:
1 - 在路由其他人的组件中放置 router-outlet 标记(此处为app组件)
2 - 创建你的路线(你做到了,我在答案中纠正了它)。
3 - 如果是子路由(由斜线分隔的路由,与您的路由一样),请在每个父组件中放置一个路由器插座标签,并将children
属性放入相应的路由中。
在你的情况下,如果我们要做一棵树,这将是这样的:
index.html
|
|--app component (with router outlet)
|
|--DrThree component (with router outlet)
|
|--ManageCycles component
基本上,索引会显示应用,然后应用会显示DrThree ,然后 DrThree将显示ManageCycles 。