MCPcopy
hub / github.com/angular/angular / Location

Class Location

packages/common/src/location/location.ts:55–304  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Implementers 1

SpyLocationpackages/common/testing/src/location_m

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…