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

时间:2019-10-18 23:39:26

标签: angular

我正在尝试使用Angular创建一个应用,而我正在使用lazyload。当我尝试访问localhost:4200时没有显示任何内容。这是代码

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 { }

这是我的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 { }

这是app.component.html

<router-outlet></router-outlet>

如您所见,我正在尝试调用名为 lander 的模块,该模块将是其余组件的处理程序。

**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 { }

lander.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 { }

最后是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>

结构有什么问题?

1 个答案:

答案 0 :(得分:0)

好吧,我看到的第一个错误是模块中导入的顺序。 Angular模块应该是第一个(CommonModule,BrowserModule,FormsModule)。然后,如果您具有使用“ moduleB”的“ moduleA”,则应导入顺序:[moduleB,moduleA]。

第二,在每个模块中两次导入路由器模块。您已经使用AppRoutingModule导入了RouterModule,因此您不必导入AppModule。另外,您在LanderModule中犯了同样的错误。

在这里您有更正:

app.routing.ts 很好。

** app.module.ts **

        for (int i = 0; i < 4;)
        {

            // Total Delay aka delay1+delay2
            if (totaldelay < 50)
            {
                i++;
            }

            // Should make totaldelay > 50
            if (i == 3)
            {
                delay1 = RandomInt(75, 105);
            }

            // Reset to 0 so it checks from 0 again
            if (total > 50)
            {
                i = 0;
            }





        }

LanderRoutingModule 很好

lander.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 { }

如果运行良好,请立即尝试。