Justin
Justin

Reputation: 1120

Angular can't get query parameter

I have an angular route application, one page has a query parameter. But I can't get this query parameter

My main code:

    <div class="nav-item" [routerLinkActive]="['active']">
         <a class="nav-link" [routerLink]="['nested']" [queryParams]="{ initType: 'testE'}" >Nested</a> 
    </div>

    ngOnInit(): void {
        const urlString = window.location.href;
        console.log('urlString=' + urlString);
        const url = new URL(urlString);
        const initType = url.searchParams.get('initType');
        console.log('initType=' + initType);

        this.route.params.subscribe(params => {
            console.log('initType=' + params.initType);
        });
    }

The both output are undefined or null.

You can debug the code in this url: http://www.justa999.com/angular/#/nested?initType=testE

My angular version is 9.1

Upvotes: 0

Views: 577

Answers (1)

Talg123
Talg123

Reputation: 1506

this.activatedRoute.queryParams.subscribe(values => {
  console.log(values);//Which will print the properties you have passed
});

this should help you: https://angular.io/api/router/ActivatedRoute#queryParams

Upvotes: 1

Related Questions