How to handle query parameters in angular 2

RouteParams are now deprecated , So here is how to do it in the new router. this.router.navigate([‘/login’],{ queryParams: { token:’1234′}}) And then in the login component you can take the parameter, constructor(private route: ActivatedRoute) {} ngOnInit() { // Capture the token if available this.sessionId = this.route.queryParams[‘token’] } Here is the documentation

Angular 2 router.navigate

If the first segment doesn’t start with / it is a relative route. router.navigate needs a relativeTo parameter for relative navigation Either you make the route absolute: this.router.navigate([‘/foo-content’, ‘bar-contents’, ‘baz-content’, ‘page’], this.params.queryParams) or you pass relativeTo this.router.navigate([‘../foo-content’, ‘bar-contents’, ‘baz-content’, ‘page’], {queryParams: this.params.queryParams, relativeTo: this.currentActivatedRoute}) See also https://github.com/angular/angular.io/blob/c61d8195f3b63c3e03bf2a3c12ef2596796c741d/public/docs/_examples/router/ts/app/crisis-center/crisis-detail.component.1.ts#L108 https://github.com/angular/angular/issues/9476

Angular2 Routerlink: add query parameters

If u need something as /search?q=asdf than you can simply use: @RouteConfig { {path: “https://stackoverflow.com/search”, name: ‘Search’, component: SearchCmp} } //And to generate router Links use: <a [routerLink]=”[‘/Search’]” [queryParams]=”{q:’asdf’}”></a> This will generate the href tag as <a href=”https://stackoverflow.com/search” but on clicking the anchor tag will lead you to url /search?q=asdf. [queryParams] will let you add … Read more

EmptyError: no elements in sequence

In my case I’ve got those errors when I used first() in an Observable that had a takeUntil() emitted before first(). Example: let test$ = new ReplaySubject(1); let testAux$ = new ReplaySubject(1); let test2$ = test$.asObservable().takeUntil(testAux$.asObservable()); test2$.first().subscribe(() => console.log(‘first!’)); testAux$.next(null); It was hard to discover the problem because I returned an observable with takeUntil() in … Read more

Angular 2 unit testing components with routerLink

You need to configure all the routing. For testing, rather than using the RouterModule, you can use the RouterTestingModule from @angular/router/testing, where you can set up some mock routes. You will also need to import the CommonModule from @angular/common for your *ngFor. Below is a complete passing test import { Component } from ‘@angular/core’; import … Read more

How to route to a Module as a child of a Module – Angular 2 RC 5

Okay, after fiddling around with this for the better part of the weekend I got it running on my end. What worked for me in the end was to do the following: Export all Routes for every module you want to route. Do not import any of the RouterModule.forChild() in the child modules. Export every … Read more

How do I navigate to a sibling route?

If you are using the new router (3.0.0-beta2), you can use the ActivatedRoute to navigate to relative path as follow: constructor(private router: Router, private r:ActivatedRoute) {} /// // DOES NOT WORK SEE UPDATE goToContact() { this.router.navigate([“../contacts”], { relativeTo: this.r }); } Update 08/02/2019 Angular 7.1.0 current route: /department/7/employees/45/sales the old version will do: /department/7/employees/45/sales/contacts As … Read more

CanActivate vs. CanActivateChild with component-less routes

From the docs: As we learned about guarding routes with CanActivate, we can also protect child routes with the CanActivateChild guard. The CanActivateChild guard works similarly to the CanActivate guard, but the difference is its run before each child route is activated. We protected our admin feature module from unauthorized access, but we could also … Read more

Angular 2 router event listener

new router constructor(router:Router) { router.events.subscribe(event:Event => { if(event instanceof NavigationStart) { } // NavigationEnd // NavigationCancel // NavigationError // RoutesRecognized }); } old Inject the Router and subscribe to route change events import {Router} from ‘angular2/router’; class MyComponent { constructor(router:Router) { router.subscribe(…) } } NOTE For the new router, don’t forget to import NavigationStart from … Read more