[英]Angular Routing: Replacing components
我正在为自己的学习做一个项目,并坚持解决这个简单的问题。 我有第一个/主页作为登录/注册页面。 现在我想使用路由用其他组件替换页面。 当我单击注册时,我只想看到注册表单组件。 我有自己的 entry.routing 模块 ENTRY,有登录组件、注册组件和注册表单组件三个组件。 在第一页上,我正在查看登录和注册组件,它们的指令位于应用程序组件的 html 中以查看它们。 如何在视图中将表单 app.component 更改为 register-form.component?
这是我的一些代码:
应用程序组件.html
<div class="flex-row">
<div class="flex-grow-one logo">
<h1>Something</h1>
</div>
<div class="flex-column flex-grow-one entry-section">
<app-login></app-login>
<div class="separation"></div>
<app-register></app-register>
</div>
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { FormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';
import { EntryModule } from './modules/entry/entry.module';
import { HeaderComponent } from './shared/components/header/header.component';
@NgModule({
declarations: [
AppComponent,
HeaderComponent
],
imports: [
BrowserModule,
BrowserAnimationsModule,
FormsModule,
HttpClientModule,
EntryModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
entry.routing.ts
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { RegisterFormComponent } from './components/register-form/register-form.component';
const entryRoutes: Routes = [
{ path: 'register-form', component: RegisterFormComponent }
];
@NgModule({
imports: [
RouterModule.forRoot(
entryRoutes
)
],
exports: [
RouterModule
]
})
export class EntryRouting {}
export const EntryRoutingComponents = [
RegisterFormComponent
];
注册.component.html
<div class="card">
<div class="card-title">
<h2>REGISTER</h2>
</div>
<div class="card-content">
<div class="card-form">
<div class="card-input">
<input type="Email" name="Email" placeholder="Email"/>
</div>
<div class="card-input">
<input type="password" name="password" placeholder="Password"/>
</div>
<div class="card-actions">
<button routerLink="/register-form" class="button-register">Register</button>
</div>
</div>
</div>
</div>
我知道必须有一个路由器插座,但我真的不知道把它放在哪里,因为如果我把它放在 register.component.html 中,它会在同一个视图中显示 register-form 组件,路由器插座在哪里是。
定义一个路由模块,您可以在其中定义 url 及其子 url。 在模块中,为 register 组件创建一个父组件。 假设您的主页 url 是 /home,它由 Rootcomponent 控制,它显示页眉和页脚以及中间的一些内容。 所以根组件的 html 可能看起来像:
<div>some header info</div> <router-outlet></router-outlet> <div>some footer info</div>
路由模块应如下所示:
const appRoute : Routes = [{ path: '/home', component: RootComponent, children: [ { path: '/login', component: AppComponent, }, { path: '/registration', component: RegisterComponent, } ] } @NgModule({ imports: [RouterModule.forRoot(appRoute)], exports: [RouterModule], }) export class myRouteModule {};
将模块注入根模块。
@NgModule({ declarations: [ RootComponent, AppComponent, registerComponent, ], imports: [ myRoutModule ] })
页面中间内容由当前路由动态控制。 如果 url 是 /home/login,则将显示应用程序视图。 对于 /home/registration,将显示注册组件。
希望这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.