Angular路由:替换组件
Angular Routing: Replacing components
为了自己的学习做一个项目,卡在了这个简单的问题上。
我有 first/main 页和 login/register 页。现在我想使用路由将页面替换为其他组件。当我点击注册时,我只想看到注册表格组件。我有自己的 entry.routing 模块 ENTRY,它包含三个组件:登录组件、注册组件和注册表单组件。在第一页上,我正在查看登录和注册组件,它们的指令在应用程序组件的 html 中以查看它们。如何将表单 app.component 更改为在视图中注册-form.component?
这是我的一些代码:
app.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
];
register.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>
我知道肯定有一个router-outlet,但我真的不知道放在哪里,因为如果我把它放在里面register.component.html,它会在同一个视图上显示register-form组件,路由器插座在哪里。
定义一个路由模块,在其中定义 url 及其子 url。在模块中,为 registercomponent 创建一个父组件。假设你的home url 是/home,它由Root 组件控制,它显示页眉和页脚以及中间的一些内容。所以根组件的 html 可能看起来像:
</p>
<pre><code><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 {};
将模块注入根模块。
</p>
<pre><code>@NgModule({
declarations: [
RootComponent,
AppComponent,
registerComponent,
],
imports: [
myRoutModule
]
})
页面中间内容由当前路由动态控制。如果 url 是 /home/login,则将显示应用程序视图。对于 /home/registration,将改为显示寄存器组件。
希望这会有所帮助。
为了自己的学习做一个项目,卡在了这个简单的问题上。 我有 first/main 页和 login/register 页。现在我想使用路由将页面替换为其他组件。当我点击注册时,我只想看到注册表格组件。我有自己的 entry.routing 模块 ENTRY,它包含三个组件:登录组件、注册组件和注册表单组件。在第一页上,我正在查看登录和注册组件,它们的指令在应用程序组件的 html 中以查看它们。如何将表单 app.component 更改为在视图中注册-form.component?
这是我的一些代码:
app.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
];
register.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>
我知道肯定有一个router-outlet,但我真的不知道放在哪里,因为如果我把它放在里面register.component.html,它会在同一个视图上显示register-form组件,路由器插座在哪里。
定义一个路由模块,在其中定义 url 及其子 url。在模块中,为 registercomponent 创建一个父组件。假设你的home url 是/home,它由Root 组件控制,它显示页眉和页脚以及中间的一些内容。所以根组件的 html 可能看起来像:
</p>
<pre><code><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 {};
将模块注入根模块。
</p>
<pre><code>@NgModule({
declarations: [
RootComponent,
AppComponent,
registerComponent,
],
imports: [
myRoutModule
]
})
页面中间内容由当前路由动态控制。如果 url 是 /home/login,则将显示应用程序视图。对于 /home/registration,将改为显示寄存器组件。
希望这会有所帮助。