Angular4 - Change state from component not template

会有一股神秘感。 提交于 2020-01-11 09:33:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!