Angular 4路由-基本href在hashPath前面加上前缀

Angular 4路由-基本href在hashPath前面加上前缀,angular,lazy-loading,angular4-router,Angular,Lazy Loading,Angular4 Router,当angular应用程序的构建版本放在根目录中时,它会生成url 但是当它被放置在根目录中更深的某个位置,并且我使用base href时,路由器在加载页面时仍然可以正常工作,但是一旦加载,base href就会被预先添加到hashpath中,如下所示: 预期Url: 我发现问题与APP.module.ts导入部分的APP_BASE_HREF条目的注入有关 providers: [ AuthenticationService, KaribaService, GlobalService, No

当angular应用程序的构建版本放在根目录中时,它会生成url

但是当它被放置在根目录中更深的某个位置,并且我使用base href时,路由器在加载页面时仍然可以正常工作,但是一旦加载,base href就会被预先添加到hashpath中,如下所示:

预期Url:

我发现问题与APP.module.ts导入部分的APP_BASE_HREF条目的注入有关

providers: [
AuthenticationService,
KaribaService,
GlobalService,
NotificationService,
/*
{
  provide: APP_BASE_HREF,
  useFactory: getBaseHref,
  deps: [PlatformLocation]
},
*/
CustomizationService
]

对此进行评论可以解决问题,但我需要它,以便在我的服务中注入BASE_HREF,从资产文件夹中提取内容。 帮忙


另外,我正在使用延迟加载。

我通过扩展HashLocationStrategy创建CustomLocationStrategy来解决这个问题,因为这似乎是唯一的解决方案

import {Injectable} from '@angular/core';
import {HashLocationStrategy} from "@angular/common";

@Injectable()    
export class CustomLocationStrategy extends HashLocationStrategy {

  prepareExternalUrl(internal: string): string {
    const url = this.getBaseHref() + '#' + internal;
    return url;
  }
}
在APP.module.ts中导入了自定义类以及APP_BASE_HREF和LocationStrategy

import { APP_BASE_HREF, LocationStrategy } from "@angular/common";
import { CustomLocationStrategy } from './common/services/customLocationStrategy.service';
并在提供者部分添加了以下内容

providers: [
  {
    provide: APP_BASE_HREF, 
    useValue: window.location.pathname
  }, 
  {
    provide: LocationStrategy, 
    useClass: CustomLocationStrategy
  }
]

你能发布app.routing的其余部分吗?主要是有路线的部分。@Cruril谢谢,但我能够通过解决方法解决它。我已经把答案贴在下面了。