【问题标题】:Angular 10 lazy Loading and routing doesn't functionAngular 10 延迟加载和路由不起作用
【发布时间】:2020-11-03 15:53:42
【问题描述】:

我在 Angular 10.1.6 中尝试路由和延迟加载,但由于某种原因我无法发现,模块的路由和延迟加载似乎根本不起作用。我曾尝试将其与 Angular 网站上的 stock example 代码进行比较,但即使在多次尝试解决问题后,我也找不到问题。

我的 App.module:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { EventsComponent } from './events/events.component';

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

这是我的应用路由模块:

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

const routes: Routes = [
  { path: 'events', component: EventsComponent },
  {
    path: 'user',
    loadChildren: () =>
      import('./User/user-profile/user-profile.module').then(
        (m) => m.UserProfileModule
      ),
  },
  { path: '**', redirectTo: '', pathMatch: 'full' },
];

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

app.component 模板只有 2 个按钮,用于根据需要将路由器链接设置为 URL。 app.component.html 看起来像这样:

<p>{{ title }}</p>
<button [routerLink]="['events']">Events</button>
<button [routerLink]="['user/profile']">User Profile</button>

我正在尝试延迟加载的模块,用户配置文件模块如下所示:

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

import { UserProfileRoutingModule } from './user-profile-routing.module';
import { UserProfileComponent } from './user-profile.component';

@NgModule({
  declarations: [UserProfileComponent],
  imports: [CommonModule, UserProfileRoutingModule],
})
export class UserProfileModule {}

并且有自己的路由模块,像这里:

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

import { UserProfileComponent } from './user-profile.component';

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

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

我做错了什么/错过了什么,路由和延迟加载不能简单地工作?

按钮点击的结果是这样的:

默认值:

事件按钮:

用户配置文件按钮:

完整的项目代码在这里:https://bitbucket.org/Anirudh_Nandavar/lazyload_angular/src/master/

提前感谢您的帮助!

【问题讨论】:

    标签: angular typescript routes lazy-loading angular2-routing


    【解决方案1】:

    你忘了把&lt;router-outlet&gt;标签放在你的app.component.html中:

    <p>{{ title }}</p>
    <button [routerLink]="['events']">Events</button>
    <button [routerLink]="['user/profile']">User Profile</button>
    <router-outlet></router-outlet>  <================================= this one
    

    【讨论】:

      猜你喜欢
      • 2017-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多