Angular对某些路由移除了base-href。
创始人
2024-10-24 15:31:41
0

在使用 Angular 路由时需要注意,如果在根模块中设置了 base-href,在使用 loadChildren 懒加载模块时会移除这个 base-href,导致路由无法正确匹配。因此需要在懒加载模块中重新设置 base-href。

示例代码:

// app.module.ts @NgModule({ imports: [ CommonModule, RouterModule.forRoot(routes, { initialNavigation: 'enabled', relativeLinkResolution: 'legacy', }), ], }) export class AppModule {}

// lazy.module.ts @NgModule({ imports: [ CommonModule, RouterModule.forChild([ { path: '', component: LazyComponent, children: [ { path: 'child', component: LazyChildComponent, data: { baseHref: '/lazy', }, }, ], }, ]), ], }) export class LazyModule { constructor( private router: Router, private activatedRoute: ActivatedRoute ) { this.router.events .pipe(filter((e) => e instanceof NavigationEnd)) .subscribe(() => { const { root } = this.activatedRoute; const { baseHref } = root.firstChild.snapshot.data; if (baseHref) { const base = document.createElement('base'); base.href = baseHref; const head = document.getElementsByTagName('head')[0]; head.insertBefore(base, head.firstChild); } }); } }

上述代码中,我们在路由配置中加入了一个 data 属性,用来存储每个子路由需要设置的 base-href。在 LazyModule 的 constructor 中通过订阅路由事件,在路由变化时手动插入新增的 base 标签来设置 base-href。这个方法可以避免 Angular 移除 base-href 导致的路由匹配问题。

相关内容

热门资讯

我来分享!pokemmo脚本手... 我来分享!pokemmo脚本手机版,智星菠萝辅助,切实是真的有挂(有挂教程)1、任何透视是真的假的的...
更值得关注的是!wpk德州局透... 更值得关注的是!wpk德州局透视,德普之星透视辅助软件是真的吗,竟然真的有挂(真实有挂)1、德普之星...
程序员教你!wpk辅助是什么,... 程序员教你!wpk辅助是什么,aapoker辅助器怎么用,果然有挂(今日头条)1、任何透视是真的假的...
连日来!wepoker透视挂底... 连日来!wepoker透视挂底牌,德扑之星透视,一直是有挂(有挂助手)1、德扑之星透视免费脚本咨询教...
我来教教大家!wepoker数... 我来教教大家!wepoker数据分析,wpk免费辅助,本来是真的有挂(有挂方式)1、进入到是否有挂之...
据了解!红龙poker作弊指令... 您好,德普之星辅助器app这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很...
一分钟揭秘!德普之星透视辅助软... 一分钟揭秘!德普之星透视辅助软件,wejoker内置辅助,竟然有挂(的确有挂)1、破解器简单,机器人...
出乎意料的是!wejoker手... 出乎意料的是!wejoker手机版透视脚本,智星菠萝透视,切实确实有挂(有挂教学)1、智星菠萝透视透...
必看攻略!wepoker辅助透... 必看攻略!wepoker辅助透视,wepoker游戏下载,本来是真的有挂(真的有挂)暗藏猫腻,小编详...
技巧辅助挂!大菠萝免费辅助,德... 技巧辅助挂!大菠萝免费辅助,德普之星辅助软件,其实真的有挂(有挂方略)1、该软件可以轻松地帮助玩家将...