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 导致的路由匹配问题。

相关内容

热门资讯

透视模拟器!wpk软件是真的吗... 透视模拟器!wpk软件是真的吗,陕西微乐挖坑小程序脚本,有挂透视(揭秘有挂)-哔哩哔哩进入游戏-大厅...
透视工具!uupoker有透视... 透视工具!uupoker有透视吗,微信小程序微乐破解器2024,推荐透视(有挂头条)-哔哩哔哩1、玩...
透视阶段!wejoker辅助软... 透视阶段!wejoker辅助软件价格,WePoKer咨询好像存在有安装,第八分钟教程(发现有挂)1、...
脚本普及!wepoker辅助器... 脚本普及!wepoker辅助器官方,wepoker怎么挂飞机(透视)竟然是真的技巧(哔哩哔哩)透视方...
透视脚本!wpk私人辅助,微信... 透视脚本!wpk私人辅助,微信微乐游戏辅助脚本,详细脚本(有挂功能)-哔哩哔哩1、wpk私人辅助辅助...
透视脚本!hhpoker可以开... 透视脚本!hhpoker可以开挂吗,微乐自建房辅助工具2025在哪,辅助透视(真的有挂)-哔哩哔哩1...
脚本教你!wpk模拟器是什么,... 脚本教你!wpk模拟器是什么,wpk真的有透视嘛(透视)切实是有挂(哔哩哔哩)一、可以开透视的定义与...
透视秘籍!wpk软件是真的吗,... 透视秘籍!wpk软件是真的吗,WPK开挂总是存在有透视,八分钟教程(有挂工具)1、wpk软件是真的吗...
透视有挂!德州局hhpoker... 透视有挂!德州局hhpoker,hhpoker有没有作必弊挂(透视)本来真的有挂(哔哩哔哩)一、游戏...
相较于以往!wepoker究竟... 相较于以往!wepoker究竟有没有透视,微乐家乡自建房辅助app,关于脚本(果真有挂)-哔哩哔哩w...