路由:无法显示组件

时间:2019-11-16 18:03:27

标签: angular

我是Angular的新手,我想用URL localhost:4200 / 注册创建一个页面。

这是我尝试过的:

src / app / app-routing-modules.ts

import { NgModule } from '@angular/core';
import {RouterModule, Routes} from '@angular/router';
import {RegisterComponent} from './register/register.component';

const routes: Routes = [
  { path: 'register', component: RegisterComponent }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

src / app / register / register.component.html

<p>register works!</p>
<router-outlet></router-outlet>

我还已经在app.modules.ts中导入了AppRoutingModule

如果URL为localhost:4200 / register,则该页面包含该应用程序组件,但无法显示我的组件“ register”。

我在做什么错了?

1 个答案:

答案 0 :(得分:1)

<router-outlet></router-outlet>应该添加到已引导的组件中(默认为AppComponent)

尝试这样:

const routes: Routes = [
  { path: '', component: LandingComponent }
  { path: 'register', component: RegisterComponent }
];

register.component.html

<p>register works!</p>

app.componet.html

<router-outlet></router-outlet>

注意:LandingComponent只是您要默认显示的组件,例如主页,登录页面等