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

Method subscribeToNavigationEvents

packages/router/src/router.ts:193–263  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

191
192 private eventsSubscription = new Subscription();
193 private subscribeToNavigationEvents() {
194 const subscription = this.navigationTransitions.events.subscribe((e) => {
195 try {
196 const currentTransition = this.navigationTransitions.currentTransition;
197 const currentNavigation = untracked(this.navigationTransitions.currentNavigation);
198
199 if (currentTransition !== null && currentNavigation !== null) {
200 this.stateManager.handleRouterEvent(e, currentNavigation);
201 if (
202 e instanceof NavigationCancel &&
203 e.code !== NavigationCancellationCode.Redirect &&
204 e.code !== NavigationCancellationCode.SupersededByNewNavigation
205 ) {
206 // It seems weird that `navigated` is set to `true` when the navigation is rejected,
207 // however it's how things were written initially. Investigation would need to be done
208 // to determine if this can be removed.
209 this.navigated = true;
210 } else if (e instanceof NavigationEnd) {
211 this.navigated = true;
212 this.injectorCleanup?.(this.routeReuseStrategy, this.routerState, this.config);
213 } else if (e instanceof RedirectRequest) {
214 const opts = e.navigationBehaviorOptions;
215 const mergedTree = this.urlHandlingStrategy.merge(
216 e.url,
217 currentTransition.currentRawUrl,
218 );
219 const extras = {
220 scroll: currentTransition.extras.scroll,
221 focusReset: currentTransition.extras.focusReset,
222 browserUrl: currentTransition.extras.browserUrl,
223 info: currentTransition.extras.info,
224 skipLocationChange: currentTransition.extras.skipLocationChange,
225 // The URL is already updated at this point if we have 'eager' URL
226 // updates or if the navigation was triggered by the browser (back
227 // button, URL bar, etc). We want to replace that item in history
228 // if the navigation is rejected.
229 replaceUrl:
230 currentTransition.extras.replaceUrl ||
231 this.urlUpdateStrategy === 'eager' ||
232 isBrowserTriggeredNavigation(currentTransition.source),
233 // allow developer to override default options with RedirectCommand
234 ...opts,
235 };
236
237 this.scheduleNavigation(
238 mergedTree,
239 IMPERATIVE_NAVIGATION,
240 null,
241 extras,
242 currentTransition.hasUAVisualTransition,
243 {
244 resolve: currentTransition.resolve,
245 reject: currentTransition.reject,
246 promise: currentTransition.promise,
247 },
248 );
249 }
250 }

Callers 1

constructorMethod · 0.95

Calls 9

scheduleNavigationMethod · 0.95
untrackedFunction · 0.90
isPublicRouterEventFunction · 0.90
subscribeMethod · 0.65
addMethod · 0.65
handleRouterEventMethod · 0.45
mergeMethod · 0.45
nextMethod · 0.45

Tested by

no test coverage detected