MCPcopy Create free account
hub / github.com/angular/angular / Location

Class Location

packages/common/src/location/location.ts:57–310  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

55 useFactory: createLocation,
56})
57export class Location implements OnDestroy {
58 /** @internal */
59 _subject = new Subject<PopStateEvent>();
60 /** @internal */
61 _basePath: string;
62 /** @internal */
63 _locationStrategy: LocationStrategy;
64 /** @internal */
65 _urlChangeListeners: ((url: string, state: unknown) => void)[] = [];
66 /** @internal */
67 _urlChangeSubscription: SubscriptionLike | null = null;
68
69 constructor(locationStrategy: LocationStrategy) {
70 this._locationStrategy = locationStrategy;
71 const baseHref = this._locationStrategy.getBaseHref();
72 // Note: This class's interaction with base HREF does not fully follow the rules
73 // outlined in the spec https://www.freesoft.org/CIE/RFC/1808/18.htm.
74 // Instead of trying to fix individual bugs with more and more code, we should
75 // investigate using the URL constructor and providing the base as a second
76 // argument.
77 // https://developer.mozilla.org/en-US/docs/Web/API/URL/URL#parameters
78 this._basePath = _stripOrigin(stripTrailingSlash(_stripIndexHtml(baseHref)));
79 this._locationStrategy.onPopState((ev) => {
80 const popStateEvent: PopStateEvent = {
81 'url': this.path(true),
82 'pop': true,
83 'state': ev.state,
84 'type': ev.type,
85 };
86 if (ev.hasUAVisualTransition) {
87 popStateEvent.hasUAVisualTransition = true;
88 }
89 this._subject.next(popStateEvent);
90 });
91 }
92
93 /** @docs-private */
94 ngOnDestroy(): void {
95 this._urlChangeSubscription?.unsubscribe();
96 this._urlChangeListeners = [];
97 }
98
99 /**
100 * Normalizes the URL path for this location.
101 *
102 * @param includeHash True to include an anchor fragment in the path.
103 *
104 * @returns The normalized URL path.
105 */
106 // TODO: vsavkin. Remove the boolean flag and always include hash once the deprecated router is
107 // removed.
108 path(includeHash: boolean = false): string {
109 return this.normalize(this._locationStrategy.path(includeHash));
110 }
111
112 /**
113 * Reports the current state of the location history.
114 * @returns The current value of the `history.state` object.

Callers

nothing calls this directly

Implementers 1

SpyLocationpackages/common/testing/src/location_m

Calls

no outgoing calls

Tested by

no test coverage detected