Is there any way to access Angular router information from the LocationStrategy?

Discussão em 'Angular' iniciado por anthonyb, Fevereiro 12, 2025.

  1. anthonyb

    anthonyb Guest

    I'm currently trying to extend the functionality of Angular's PathLocationStrategy to set the navigation state of the browser based on the state passed during a Router.navigate() event.

    I keep getting a circular dependency error that forwardRef has not seemed to fix.

    How can I get access to the current navigation from the location strategy?

    import {
    APP_BASE_HREF,
    PathLocationStrategy,
    PlatformLocation,
    } from '@angular/common';
    import {
    forwardRef,
    inject,
    Inject,
    Injectable,
    Optional,
    } from '@angular/core';
    import { ActivatedRoute, Router } from '@angular/router';

    @Injectable({ providedIn: 'root' })
    export class NavStateLocationStrategy extends PathLocationStrategy {
    router?: Router

    constructor(
    platformLocation: PlatformLocation,
    @Optional() @Inject(APP_BASE_HREF) _baseHref?: string,
    @Optional() @Inject(forwardRef(()=>Router)) router?: Router
    ) {
    super(platformLocation, _baseHref);
    this.router = router
    }

    override pushState(
    state: any,
    title: string,
    url: string,
    queryParams: string,
    ): void {
    const navState = this.router?.getCurrentNavigation()?.extras.state;
    super.pushState({...state, ...navState}, title, url, queryParams);
    }
    }

    Continue reading...

Compartilhe esta Página