File

libs/ngx-pfe/hash-only-location-strategy.ts

Description

Provides the possibility to keep search url parameters and to only change the hash.

Alternative, to set the base href to a specific value: {provide: APP_BASE_HREF_HASH_ONLY, useValue: '/' + window.location.pathname.split('/')[1] + window.location.search},

Extends

HashLocationStrategy

Index

Methods

Constructor

constructor()

Methods

getBaseHref
getBaseHref()
Returns : string
prepareExternalUrl
prepareExternalUrl(internal: string)
Parameters :
Name Type Optional
internal string No
Returns : string
import { APP_BASE_HREF, HashLocationStrategy, PlatformLocation } from '@angular/common';
import { Injectable, InjectionToken, inject } from '@angular/core';

export const APP_BASE_HREF_HASH_ONLY = new InjectionToken<string>('appBaseHrefHashOnly');

/**
 *
 * Provides the possibility to keep search url parameters and to only change the hash.
 *
 * Alternative, to set the base href to a specific value:
 * {provide: APP_BASE_HREF_HASH_ONLY, useValue: '/' + window.location.pathname.split('/')[1] + window.location.search},
 *
 */
@Injectable()
export class HashOnlyLocationStrategy extends HashLocationStrategy {
  private _baseHashHref = '';
  // eslint-disable-next-line @typescript-eslint/naming-convention
  private __platformLocation!: PlatformLocation;

  constructor() {
    const _platformLocation = inject(PlatformLocation);
    const _baseHref = inject(APP_BASE_HREF, { optional: true }) ?? undefined;
    const _baseHashHref = inject(APP_BASE_HREF_HASH_ONLY, { optional: true });

    super(_platformLocation, _baseHref);

    if (_baseHashHref != null) {
      this._baseHashHref = _baseHashHref;
    } else if (_platformLocation) {
      this.__platformLocation = _platformLocation;
    }
  }

  prepareExternalUrl(internal: string): string {
    return this._baseHashHref + '#' + internal;
  }

  getBaseHref(): string {
    if (this.__platformLocation) {
      this._baseHashHref = '/' + this.__platformLocation.pathname + this.__platformLocation.search;
      if (this._baseHashHref.startsWith('//')) {
        this._baseHashHref = this._baseHashHref.slice(1);
      }
      return this._baseHashHref;
    } else {
      return this._baseHashHref;
    }
  }
}

results matching ""

    No results matching ""