【问题标题】:Angular Routing: Replacing components角度路由:替换组件
【发布时间】: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 中使用 &lt;router-outlet&gt;&lt;/router-outlet&gt; 和相应地定义您的路线have a look at this, this will help

标签: javascript angular angular-routing


【解决方案1】:

定义一个路由模块,您可以在其中定义 url 及其子 url。在模块中,为 registercomponent 创建一个父组件。假设您的主页 url 是 /home,它由 Root 组件控制,它显示页眉和页脚以及中间的一些内容。因此根组件的 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,将显示注册组件。

希望这会有所帮助。

【讨论】:

  • 谢谢!当我深入阅读路由时,我认为您的建议可能是正确的。我明天会试试这个,如果它有效,我会报告。
  • 你好,最后需要展示的Registration-Form组件在哪里? myRouteModule 是哪一个?
  • Hi Anish,myRouteModule 是自定义应用路由模块的名称。通过导出它,其他 Angular 模块可以导入路由模块。详情请参考angular.io/guide/ngmodules
猜你喜欢
  • 2018-05-17
  • 1970-01-01
  • 1970-01-01
  • 2019-04-18
  • 1970-01-01
  • 2019-02-14
  • 1970-01-01
  • 2020-10-09
  • 1970-01-01
相关资源
最近更新 更多