【发布时间】:2018-11-28 19:42:27
【问题描述】:
我正在为自己的学习做一个项目,并坚持这个简单的问题。 我有第一页/主页作为登录/注册页面。现在我想使用路由将页面替换为其他组件。当我单击注册时,我只想查看注册表单组件。我有模块 ENTRY 和它自己的 entry.routing,有三个组件登录组件,注册组件和注册表单组件。在第一页上,我正在查看登录和注册组件,它们的指令位于应用组件的 html 中以查看它们。如何在视图中将表单 app.component 更改为 register-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 组件,其中路由器插座是。
【问题讨论】:
-
我在 app.module.ts
imports中看不到你的EntryRouting -
@Avinash 是的。我有另一个模块 entry.module,其中 entry.routing 被导入。因此,当我将 EntryModule 导入 AppModule 时,我认为它应该可以工作。它有效,只是没有做我需要它做的事情。 :)
-
如果我没记错,您正在登录,在主路径中注册组件并尝试用 registerForm 替换整个视图,那么您需要在 app.component.html 中使用
<router-outlet></router-outlet>和相应地定义您的路线have a look at this, this will help
标签: javascript angular angular-routing