简体   繁体   English

Angular 不显示带有延迟加载的组件

[英]Angular is not displaying the components with lazyload

I'm trying to create an app with Angular, i'm using lazyload.我正在尝试使用 Angular 创建一个应用程序,我正在使用延迟加载。 When I try to access localhost:4200 is not showing anything.当我尝试访问localhost:4200时没有显示任何内容。 Here is the code这是代码

app.routing.ts app.routing.ts

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

const routes: Routes = [
    { path: '', loadChildren: './pages/lander/lander.module#LanderModule' },
];

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

This is my app.module.ts这是我的 app.module.ts

import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { AppRoutingModule } from './app.routing';
import { FormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';
import { RouterModule } from '@angular/router';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { BrowserModule } from '@angular/platform-browser';

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

This is the app.component.html这是app.component.html

<router-outlet></router-outlet>

As you see, I'm trying to call a module called lander this will be the handler of the rest of components.如您所见,我正在尝试调用一个名为lander的模块,这将是 rest 组件的处理程序。

**This is the lander-routing.module.ts**
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { LanderComponent } from './lander.component';

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

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

The lander.module.ts登陆器.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

// Material
import { MatGridListModule } from '@angular/material';
import {MatSidenavModule} from '@angular/material/sidenav';
import {MatMenuModule} from '@angular/material/menu';

import { LanderRoutingModule } from './lander-routing.module';
import { LanderComponent } from './lander.component';
import { RouterModule } from '@angular/router';



@NgModule({
  declarations: [LanderComponent],
  imports: [
    CommonModule,
    RouterModule,
    LanderRoutingModule,
    MatGridListModule,
    MatSidenavModule,
    MatMenuModule,
  ]
})
export class LanderModule { }

And at the end is coming the lander.component.html最后是lander.component.html

<div class="container-fluid h-100 px-0">
  <mat-drawer-container class="drawer" [hasBackdrop]="true">
    <mat-drawer class="menubar" #drawer mode="over">
      <img class="avatar" src="https://www.teslarati.com/wp-content/uploads/2019/04/elon-musk-smiling-1.jpg">
      <h4 class="text-center mt-2">
        ELON MUSK<br>
        <small class="text-secondary">
          Chairman
        </small>
      </h4>
      <ul class="list-group mt-4">
        <li class="list-group-item active" routerLinkActive="active">
          <a [routerLink]="[ '/' ]">Dashboard</a>

        </li>
        <li class="list-group-item">Second item</li>
        <li class="list-group-item">Third item</li>
      </ul>
    </mat-drawer>

    <mat-drawer-content>
      <button mat-raised-button (click)="drawer.toggle()">Toggle drawer</button>
      <router-outlet></router-outlet>
    </mat-drawer-content>
  </mat-drawer-container>
</div>

What is wrong with the structure?结构有什么问题?

Well, the first error that I see is the order of the imports in the modules.好吧,我看到的第一个错误是模块中导入的顺序。 the Angular modules should be first (CommonModule, BrowserModule, FormsModule). Angular 模块应该是第一个(CommonModule、BrowserModule、FormsModule)。 Then if you have the "moduleA" that uses the "moduleB", so the order should be imported: [moduleB, moduleA].然后如果你有使用“moduleB”的“moduleA”,那么应该导入顺序:[moduleB,moduleA]。

Second, you are importing the Router Module twice in each module.其次,您在每个模块中导入了两次路由器模块。 You are already importing the RouterModule with the AppRoutingModule, so you don't have to import in the AppModule.您已经使用 AppRoutingModule 导入 RouterModule,因此您不必在 AppModule 中导入。 Also, you are making the same mistake in the LanderModule.此外,您在 LanderModule 中犯了同样的错误。

Here you have the corrections:在这里你有更正:

app.routing.ts it's fine. app.routing.ts没关系。

** app.module.ts** ** app.module.ts**

import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { AppRoutingModule } from './app.routing';
import { FormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { BrowserModule } from '@angular/platform-browser';

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

LanderRoutingModule it's fine LanderRoutingModule 没问题

The lander.module.ts登陆器.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

// Material
import { MatGridListModule } from '@angular/material';
import {MatSidenavModule} from '@angular/material/sidenav';
import {MatMenuModule} from '@angular/material/menu';

import { LanderRoutingModule } from './lander-routing.module';
import { LanderComponent } from './lander.component';
import { RouterModule } from '@angular/router';



@NgModule({
  declarations: [LanderComponent],
  imports: [
    CommonModule,
    MatGridListModule,
    MatSidenavModule,
    MatMenuModule,
    LanderRoutingModule
  ]
})
export class LanderModule { }

Try now if it run good.如果运行良好,请立即尝试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM