0

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

1 Answer 1

1
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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks!, I got it , I should use queryParams rather than params

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.