在Angular 7路由中将查询字符串放在散列之前

时间:2019-06-19 19:27:59

标签: angular typescript

我有一个更大的应用程序,其中包含一个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());
    }
  }

0 个答案:

没有答案