[英]Routing angular 4/5 hiding components
new to routing with angular 4/5, I am following the angular tutorial on their website. 角4/5布线的新手,我正在他们网站上关注角教程。 I have an angular application which I want to be able to have two separate pages.
我有一个有角度的应用程序,我希望能够有两个单独的页面。 Right now main page is
localhost:8870/dr3-interface
and I want a new page with new components on localhost:8870/dr3-interface/manage_cycles_instances
现在,主页是
localhost:8870/dr3-interface
,我想要一个新页面,其中包含localhost:8870/dr3-interface/manage_cycles_instances
上的新组件
My issue is when I click on my link Manage cycles instances
it shows all my app.component
components and not only the components I decided to show on my /manage_cycles_instances
. 我的问题是,当我单击“
Manage cycles instances
链接时,它显示了我所有的app.component
组件,而不仅仅是我决定显示在/manage_cycles_instances
上的组件。 I tried to hide them using *ngIf
but without results. 我试图使用
*ngIf
隐藏它们,但没有结果。 Any ideas? 有任何想法吗?
app.component.html : 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 : 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 : 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 {
}
Your routes should be this : 您的路线应为:
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 },
]},
];
Because you want to have nested routes, you should make nested routes. 因为您要具有嵌套的路由,所以应该创建嵌套的路由。 Note that your
DrThreeComponent
should have a router-outlet
, since it has children. 请注意,您的
DrThreeComponent
应该有一个router-outlet
,因为它有孩子。
You won't need to use conditions in your code, because the router will handle the display of your components. 您不需要在代码中使用条件,因为路由器将处理组件的显示。
You start by having an index.html file. 您首先要拥有一个index.html文件。 It only contains a tag in its body, usually
app-root
. 它的主体中仅包含一个标签,通常是
app-root
。 This tag will be replaced by your bootstraped component, which is usually AppComponent
. 这个标签会被你的bootstraped组件,它通常是被替换
AppComponent
。
If you want to route your application, you will need to use the router . 如果要路由应用程序,则需要使用router 。 Several steps are required :
需要执行几个步骤:
1 - Put a router-outlet tag in the component that will route others (here, app component) 1-在要路由其他组件的组件(此处为应用程序组件)中放置一个路由器出口标签。
2 - Create your routes (you did it, and I corrected it in my answer). 2-创建您的路线(您做了,我在回答中更正了)。
3 - in case of child routes (routes seprated by slashes, like yours), put a router outlet tag in every parent component, and a children
property into the corresponding routes. 3-如果是子级路由(像您一样用斜线分隔的路由),则在每个父级组件中放置一个路由器出口标签,并将
children
属性放入相应的路由中。
In your case, if we were to make a tree, this would look like this : 在您的情况下,如果我们要制作一棵树,则如下所示:
index.html
|
|--app component (with router outlet)
|
|--DrThree component (with router outlet)
|
|--ManageCycles component
So basically, index will show app , then app will show DrThree , then DrThree will show ManageCycles . 所以基本上, 索引将显示app ,然后app将显示DrThree ,然后DrThree将显示ManageCycles 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.