Angular 6: 获取基础 href

Angular 6: Get base href

我想加入 angular 6 个组件。 我通过以下方式管理它:PlatformLocation#getBaseHrefFromDOM

但是这个api不应该被开发者使用。还有其他方法可以在运行时获取 /CTX-ROOT/assets/tiny-editor/langs/cs.js 吗?

  constructor(
    private zone: NgZone,
    private platformLocation: PlatformLocation) {
  }

  public ngAfterViewInit() {
    var baseHref = this.platformLocation.getBaseHrefFromDOM();
    Observable.fromPromise(tinymce.init({
      selector: '#' + this.elementId,
      entity_encoding: "raw",
      menubar: false,
      branding: false,
      elementpath: true,
      language_url: baseHref + '/assets/tiny-editor/langs/cs.js',

你用原生JS怎么样? window.location 将给出 Location 对象。 window.location.origin 将为您提供基本 href。

您可以使用 Location 服务中的 prepareExternalUrl 方法 (doc)

import {Location} from "@angular/common";

//...
constructor(
    private zone: NgZone,
    private location: Location) {
}


public ngAfterViewInit() {
    Observable.fromPromise(tinymce.init({
      selector: '#' + this.elementId,
      entity_encoding: "raw",
      menubar: false,
      branding: false,
      elementpath: true,
      language_url: this.location.prepareExternalUrl('assets/tiny-editor/langs/cs.js')

编辑:我认为这仅在使用 PathLocationStrategy

时有效

This method will also add a hash if HashLocationStrategy is used, or the APP_BASE_HREF if the PathLocationStrategy is in use.