angular - 在 Angular 7 路由中将查询字符串放在哈希之前
问题描述
我有一个更大的应用程序,其中包含一个 Angular 页面。外部应用程序使用查询字符串来存储它的一些状态(不是我的决定)。Angular 页面使用路由。
我尝试了几种方法,使用 PathLocationStrategy 并为 APP_BASE_HREF 提供我自己的提供程序,但最终得到的 url 看起来像这样:
/my-page/#/?foo=bar/my-route
当我想去:
/my-page?foo=bar#/my-route
有没有办法做到这一点?请注意,我只知道 foo=bar 在运行时。对该页面的后续访问可能会产生:
/my-page?foo=baz#/my-route 或 /my-page?foo=qux#/my-route 或 /my-page?foo=12345#/my-route
我已经尝试过,在 app.module.ts 中:
export function getBaseLocation(): string {
return location.toString()
.replace(`${location.protocol}//`, '')
.replace(location.host, '');
}
@NgModule({
// ...
providers: [{
provide: APP_BASE_HREF,
useFactory: getBaseLocation,
},
//...
});
在 app.component.ts 中:
constructor(
private pathLocationStrategy: PathLocationStrategy,
private router: Router) {
if (this.pathLocationStrategy.getBaseHref() !== this.pathLocationStrategy.path()) {
this.router.navigateByUrl(this.pathLocationStrategy.path());
}
}
解决方案
推荐阅读
- node.js - 使用证书中的公钥将引发错误
- machine-learning - h2o: Flow UI, ROC with drf without fold
- java - 如何修复“创建文档实例时出错。原因:org.xml.sax.SAXParseException;必须为元素类型“mapper”声明属性“class”。
- python - 从 url 获取数据并将其放入 DataFrame
- ruby-on-rails - 是否可以在不使用密钥 ID 和密钥(Ruby on Rails)的情况下在 S3 存储桶上创建文件?
- c++ - 如何从用户那里获取方程的输入并在c ++中对其进行评估
- fullcalendar - 控制 FullCalendar 4 时间网格槽高度
- konvajs - vue-konva 将自定义样式传递给 div.konvajs-content & canvas
- node.js - 如何在突变 graphQL Playground 中传递变量?
- tortoisegit - TortoiseGit:如果选中“为此存储库运行”,则未执行启动提交钩子