Skip to content

Instantly share code, notes, and snippets.

@DevEarley
Last active April 3, 2019 18:30
Show Gist options
  • Save DevEarley/a9d31cf20c0484c754821a593f163801 to your computer and use it in GitHub Desktop.
Save DevEarley/a9d31cf20c0484c754821a593f163801 to your computer and use it in GitHub Desktop.
Angular Routing Tips

Get values from ActivatedRoute

class MyComponent {
  constructor(route: ActivatedRoute) {
    const id: string = route.snapshot.params.id;
    const user = route.snapshot.data.user;
    const parentId = route.snapshot.parent.paramMap.get("id")
  }
}

Subscribe to route changes

  private subscribeToRouteChanges() {
    this.route.params.subscribe(val => {
      this.loadResources();
    });
  }

Navigate to route relative to current route

this.router.navigate(['/mypath']);

Navigate to parent route (go back)

this.router.navigate(['../'], { relativeTo: this.route });

Navigate and resolve some data

Your route -

{
	path: 'mypath',
	component: MyAppComponent,
	data: {
		someData: "Hello World"
	}
},

Your Component -

this.route.snapshot.data['someData']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment