angular - 为什么延迟加载路由在 angular12 中不起作用?
问题描述
我有想要延迟加载的模块。但是当我定义他们的延迟加载路由时,这些路由不起作用并且没有加载到浏览器中。当我尝试通过浏览器导航到他们的路线时,每次都会带我到仪表板。
app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { RouterModule } from '@angular/router';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { CoreModule } from './core/core.module';
import { ClaimModule } from './modules/claim/claim.module';
import { DependentModule } from './modules/dependent/dependent.module';
import { ProfileModule } from './modules/profile/profile.module';
import { ModalModule } from 'ngx-bootstrap/modal';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule,
BrowserAnimationsModule,
CoreModule,
ClaimModule,
DependentModule,
ProfileModule,
FormsModule,
RouterModule,
ModalModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
应用模块.routing.ts
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { LoginComponent } from './core/components/login/login.component';
import { AuthGuard } from './core/guard/auth.guard';
const routes: Routes = [
{ path: 'login', component: LoginComponent },
{
path: '',
canActivate: [AuthGuard],
loadChildren: () => import('./core/core.module').then(m => m.CoreModule)
},
{
path: 'dependent',
canActivate: [AuthGuard],
loadChildren: () => import('./modules/dependent/dependent.module').then(m => m.DependentModule)
},
{
path: 'profile',
loadChildren: () => import('./modules/profile/profile.module').then(m => m.ProfileModule)
},
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
配置文件-module.routing.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { ProfileComponent } from './profile.component';
const routes: Routes = [
{ path: '', component: ProfileComponent },
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class ProfileRoutingModule { }
profile.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ProfileComponent } from './profile.component';
import { ProfileRoutingModule } from './profile-routing.module';
@NgModule({
declarations: [
ProfileComponent
],
imports: [
CommonModule,
ProfileRoutingModule
],
exports: [ProfileComponent]
})
export class ProfileModule { }
核心路由.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { LayoutComponent } from './layout/layout.component';
const routes: Routes = [
{
path: '',
component: LayoutComponent,
children: [
{ path: '', redirectTo: '/dashboard', pathMatch: 'full' },
{ path: 'dashboard', loadChildren: () => import('./../modules/dashboard/dashboard.module').then(m => m.DashboardModule) },
]
},
{
path: '**',
redirectTo: '/dashboard',
pathMatch: 'full'
}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class CoreRoutingModule { }
layout.component.html
<div class="wrapper w-100">
<!-- Navbar -->
<app-header></app-header>
<!-- /.navbar -->
<!-- Main Sidebar Container -->
<app-sidebar></app-sidebar>
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
<router-outlet></router-outlet>
</div>
<!-- /.content-wrapper -->
<!-- Control Sidebar -->
<app-control-sidebar></app-control-sidebar>
<!-- /.control-sidebar -->
<!-- Main Footer -->
<app-footer></app-footer>
</div>
<!-- ./wrapper -->
这是我的所有route
配置。但是每当我尝试导航到时/profile
route
,它总是带我到仪表板。我不知道为什么它没有加载module
和它的component
.
解决方案
应用模块.routing.ts
空路径应该有 pathMatch: 'full' 否则所有 url 都将转到 coreRoutingModule
{
path: '',
canActivate: [AuthGuard],
loadChildren: () => import('./core/core.module').then(m => m.CoreModule),
pathMatch: 'full'
},
推荐阅读
- php - 如何为 10 天后过期的 php 文件生成密钥。密钥过期后其他用户无法执行api
- javascript - Ajax 调用返回 viewmodel 但视图不显示结果
- mysql - MySQL:如何通过转换 VARCHAR 列中的所有值来设置 DATE 列?
- ios - iOS中谷歌翻译API的文本解码问题
- doctrine-orm - 如何在 dql where 语句中使用学说自定义类型?
- ubuntu - 在 ubuntu 中使用 ccm 时出现 YAMLLOADWARNING
- microsoft-graph-api - MS-Graph 事件中的组织者和 isOrganizer 属性是否有效?
- r - 重新格式化矢量
- elasticsearch - 如何聚合直到在 ElasticSearch 中达到某个值?
- android - 如何将图像放置在 android 工具栏中?