Angular 5 子模块的路由不工作

Angular 5 routing for child module is not working

我正在使用 Angular 开发 Web 应用程序 5. 我是 Angular 使用 Typescript 的新手。现在我正在为我的应用程序模块配置路由。但它不工作并抛出错误。

这是我的 index.html 文件

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Web</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
  <app-root></app-root>
  <router-outlet></router-outlet>
</body>
</html>

我在应用程序文件夹下创建了一个 app-routing.module.ts 文件。

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


    const routes: Routes = [ 
        { path: '', loadChildren: './web/web.module#WebModule' },
    ];

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


Import that routing class in the app.module.ts file

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { RouterModule, Route } from '@angular/router';
import { AppRoutingModule } from './app-routing.module'

import { AppComponent } from './app.component';


@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    AppRoutingModule,
    BrowserModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

然后创建了另一个模块,web/web.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { HomeComponent } from './home/home.component';
import { WebRoutingModule  } from './web-routing.module';

@NgModule({
  imports: [
    CommonModule,
    WebRoutingModule
  ],
  declarations: [HomeComponent]
})
export class WebModule { }

这是我的web-routing.module.ts

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

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

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

如您所见,我正在渲染 HomeComponent。我创建了主组件用户 web/home/ 文件夹。

根据我的代码,如果我像这样访问我的网站,localhost:4200,它应该在 index.html 文件的路由器输出中呈现主页组件 HTML。现在它不渲染主页组件 HTML。它正在渲染 index.html 但没有家,组件在路由器插座中被替换。那么,我的代码有什么问题,我该如何解决?

<router-outlet></router-outlet> 必须在 app-home 的标记中而不是在 index.html 文件中才能工作

我刚刚在我的一个项目中设置了路由。

我的app.component.ts看起来像这样

import { Component } from '@angular/core';
import { Router } from '@angular/router';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})

export class AppComponent {
}

我的app.component.html看起来像这样

<router-outlet></router-outlet>

我的app-routing.module.ts长得像这样

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

// Layouts
import { MasterComponent } from './components/layouts/master/master.component';
// Main Pages
import { HomeComponent } from './components/pages/home/home.component';
import { PageNotFoundComponent } from './components/pages/page-not-found/page-not-found.component';

const routes: Routes = [
  { path: '', component: MasterComponent, children: [
    { path: '', component: HomeComponent },
  ] },
  { path: '**', component: PageNotFoundComponent },
];

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

我的 index.html 文件也是这样的

<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Office</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
  <app-root></app-root>
</body>
</html>

尝试在 app.module 的导入数组中提供您的 web.module