HashLocationStrategy and query parameters in angular 4

前端 未结 1 452
死守一世寂寞
死守一世寂寞 2020-12-19 21:21

I am migrating an existing angular 4 app from PathLocationStrategy to HashLocationStrategy and need to keep entry point url working. It looks something like

1条回答
  •  离开以前
    2020-12-19 21:48

    Since HashLocationStrategy is already used as default, PathLocationStrategy should be additionally injected in order to get to real browser location:

      providers: [
        PathLocationStrategy,
        {provide: LocationStrategy, useClass: HashLocationStrategy},
        ...
      ]
    
    ...
    
    class AppComponent {
      constructor(router: Router, pathLocationStrategy: PathLocationStrategy) {
        const basePath = pathLocationStrategy.getBaseHref();
        const absolutePathWithParams = pathLocationStrategy.path();
    
        if (basePath !== absolutePathWithParams) {
          router.navigateByUrl(absolutePathWithParams);
        }
      }
    }
    

    If there is base url, it should be additionally taken away from the path.

    0 讨论(0)
提交回复
热议问题