How to get query params from URL in Angular 2+?
Hi everyone! I just started learning Angular (version 2, I think, or 4, now I'm writing on version 17, but the essence is the same). I encountered a seemingly simple task: I need to read parameters from the browser URL string.
I have a page with a list of products, and I want the user to be able to filter them via query parameters. For example, the URL looks like this: `/products?category=electronics&sort=price`.
I tried to get these parameters inside the component, but nothing works. I tried using `ActivatedRoute`, but it returns an empty object or `undefined`.
Here is my component code (simplified):
```typescript
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
@Component({
selector: 'app-products',
template: '
Product list
'No answers yet.
Log in to leave an answer
Blogs
GraphQL API with JavaScript: Step-by-Step Tutorial for Node.js
WebSocket in Java: Building Real-Time Applications from Scratch
BDD in Swift: How to Write Tests That Business Understands
Async/await in forEach: How to Make Asynchronous Loops Work Together