Installation & Setup
Installation
npm install ngx-url-state-- or --
yarn add ngx-url-stateSetup Steps
- Define a TypeScript interface representing which parameters you need and their types.
export interface ProductsPageParams { page: number; pageSize: number; redText: boolean;}- Inject
ActivatedRouteandUrlStateServicein the component's constructor
constructor(private activatedRoute: ActivatedRoute, private urlStateService: UrlStateService) { }- In
ngOnInit, define the parameters you wish to track and obtain an instance ofUrlState
export class YourComponent implements OnInit {
public urlState: UrlState<ProductsPageParams>;
...
ngOnInit(): void { this.urlState = this.urlStateService.listen<ProductsPageParams>({ activatedRoute: this.activatedRoute, componentDestroyed$: this.componentDestroyed$, paramDefinitions: { name: { mapper: StringMapper } } });
}- You're all set up and ready to rock. The following sections describe your new super powers.