延迟加载不加载模块-Angular 8

时间:2019-10-04 18:21:31

标签: angular routing lazy-loading angular-routing angular8

我正在尝试延迟加载我的主页,其中包括一个PrimeNG模块(MenubarModule),但是它从未加载过,所以我一直遇到错误:

错误:模板解析错误: “ p-menubar”不是已知元素:

app.module.ts

@NgModule({
  declarations: [
    AppComponent,
    HomeComponent,
    LoginComponent
  ],
  imports: [
    AppRoutingModule,
    BrowserModule,
    FormsModule,
    ReactiveFormsModule,
    BrowserAnimationsModule,

    PanelModule,
    InputTextModule,
    ButtonModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

我的app-routing.module.ts

const routes: Routes = [
  {
    path: '',
    component: LoginComponent
  },
  {
    path: 'login',
    component: LoginComponent
  },
  {
    path: 'home',
    component: HomeComponent,
    loadChildren: () => import('./home/home.module').then(m => m.HomeModule) 
  }
];

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

我的home.module.ts

@NgModule({
  declarations: [],
  imports: [
    CommonModule,
    MenubarModule
  ]
})
export class HomeModule { }

继续获取错误:模板解析错误: “ p-menubar”不是已知元素

@edit

如果我将MenubarModule放入我的app.module中,它将可以工作,但是我希望将其懒加载到home.module中

3 个答案:

答案 0 :(得分:1)

您需要在堆栈闪电中做一些事情来修复代码

index.html应该是

<body><app-root></app-root></body>

而您的输入标签不同于app-root

此外,您还需要从应用程序模块和应用程序路由模块中删除对HomeComponent的所有引用,并在Home Module中将forChild路由器定义添加为

RouterModule.forChild([{ path: "", component: HomeComponent }])

正在{@ {3}}

工作

答案 1 :(得分:0)

app.module.ts:

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

app-routing.module.ts:

const routes: Routes = [
  { path: 'home', loadChildren: () => import('./home/home.module').then(module => module.HomeModule)},
  { path: 'login', loadChildren: () => import('./login/login.module').then(login => login.LoginModule)}
];

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

app.component.html:

<a routerLink="/home">Home</a>
<br>
<a routerLink="/login">Login</a>

<router-outlet></router-outlet>

home.module.ts:

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

home-routing.module.ts

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

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

login.module.ts:

@NgModule({
  declarations: [LoginComponent],
  imports: [
    CommonModule
  ]
})
export class LoginModule { }

login-routing.module.ts

const routes: Routes = [
  { path: '', component: LoginComponent, children: [
     { path: 'another', component: AnotherComponent }
  ]}
];

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

现在它必须工作。因为children:,您可以在LoginComponentHomeComponent中使用<router-outlet></router-outlet> dom元素。

答案 2 :(得分:0)

如果您要延迟加载,则应在home.module.ts中声明子组件,并在home-routing.module.ts中定义路由,然后它将起作用。

enter image description here

从上图admin.module.ts来看,它具有两个组成部分,分别为homesettings,现在我们可以看到admin-routing.module.ts

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


export const AdminRoutes: Routes = [{
  path: '',
  children: [
    { path: 'home', component: HomeComponent },
    { path: 'settings', component: SettingsComponent }]
}];

并将那些AdminRoutes导入到admin.module.ts中,并通过以下方式声明组件。

import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { CommonModule } from '@angular/common';
import { AdminRoutes } from './admin-routing.module';
import { HomeComponent } from './home/home.component';
import { SettingsComponent } from './settings/settings.component';
import { RouterModule } from '@angular/router';


@NgModule({
  declarations: [HomeComponent, SettingsComponent],
  imports: [
    CommonModule, RouterModule.forChild(AdminRoutes)
  ],
  schemas: [
    CUSTOM_ELEMENTS_SCHEMA
  ]
})
export class AdminModule { }

,然后在app-routing.module.ts中定义它的延迟加载,它将永远有效。