Angular4 - state change from a component, not a template

In AngularJS, I used ui-router to redirect inside my application (state change).

He has 2 possible redirection options

  • In the template ui-sref='stateName'
  • In the controller $state.go()

I am just starting to play with Angular (4), and I only found a way to change the route from the template with something like:

  • Template routerLink="routePath"

Is there a way that was in u-router to change the route from the component?

+4
source share
2 answers
constructor(private router:Router) {}

changeRoute() {
  this.router.navigate(...)
  // this.router.navigateByUrl(...)
}

See also https://angular.io/docs/ts/latest/api/router/index/Router-class.html

+6
source

you can navigate the router as follows

router.navigate([URL])

or how is it

router.navigateByUrl('whole_string_containing_full_path')

- , , ,

, url .

+1

Source: https://habr.com/ru/post/1673306/


All Articles