B
B
babagay2018-11-12 12:37:34
Angular
babagay, 2018-11-12 12:37:34

How to change URL without reload in Angular 6?

Change the route (url) to /?userId=123 or /userId/123 so that the page is not reloaded - how to do it with regular angular tools or using hacks?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mrhat24, 2018-11-13
@mrhat24

You need to add a RouterModule to app.module.ts, like here , and you can add links in the template:

<a routerLink="/"
   [queryParams]="{ userId: '123' }">
  Users
</a>

or
<a routerLink="/userId/123"
   queryParamsHandling="merge">
  Users
</a>

or
in a component
import {Router} from "@angular/router";
@Component()
export class AppComponent implements OnInit {
   constructor(
        private router: Router
    ){}
    ngOnInit() {
        this.router.navigateByUrl("/userId/123");
        // или
        this.router.navigateByUrl({queryParams: {userId: 123}});
    }
}

I'm not sure if the latter works, I wrote it in one breath, but judging by the documentation it should work.
Here is the documentation: https://angular.io/guide/router

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question