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(_platformLocation: PlatformLocation, _baseHref?: string, _baseHashHref?: string | undefined)
Parameters :
Name Type Optional
_platformLocation PlatformLocation No
_baseHref string Yes
_baseHashHref string | undefined Yes

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 { Inject, Injectable, InjectionToken, Optional } 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(
    _platformLocation: PlatformLocation,
    @Optional()
    @Inject(APP_BASE_HREF)
    _baseHref?: string,
    @Optional()
    @Inject(APP_BASE_HREF_HASH_ONLY)
    _baseHashHref?: string | undefined
  ) {
    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 ""