问题
In AngularJS I used ui-router for redirecting inside of my app(changing state).
It has 2 possible options to redirect
- In template
ui-sref='stateName'
- In controller
$state.go()
I just start play with Angular (4) and I found only way how to change route from template with something like:
- Template
routerLink="routePath"
Is there some way as there was in ui-router to change route from component?
回答1:
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
回答2:
you can navigate via router like this
router.navigate([URL])
or like this
router.navigateByUrl('whole_string_containing_full_path')
Here router is an instance created in constructor, which is imported from router firstly, basically there is no difference between these two
First one accept array where as second one accept url in the form of string.
来源:https://stackoverflow.com/questions/43045736/angular4-change-state-from-component-not-template