libs/ngx-pfe/hash-only-location-strategy.ts
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},
Methods |
constructor(_platformLocation: PlatformLocation, _baseHref?: string, _baseHashHref?: string | undefined)
|
||||||||||||
Parameters :
|
getBaseHref |
getBaseHref()
|
Returns :
string
|
prepareExternalUrl | ||||||
prepareExternalUrl(internal: string)
|
||||||
Parameters :
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;
}
}
}