来自控制器的 Angular2 UI 路由器 -ng2 路由

Angular2 UI router -ng2 routing from the controller

本文关键字:路由器 -ng2 路由 UI Angular2 控制器      更新时间:2023-09-26

所以我正在使用UI-Router ng2,并且我正在尝试在函数发生后更改路由,我的代码如下所示:

SomeFuncion() {
    if(something){
        router.goto('/newRouteName');
    }
}

使用 uiSref 从 HTML 使用路由器很简单,但是我不知道如何使用新的 ng2 路由器从组件内部路由,他们的文档没有多大帮助。

在组件中注入StateService

import { StateService } from "ui-router-ng2";
@Component({})
class MyComponent {
  constructor(public stateService: StateService) {}
  goSomewhere() {
    this.stateService.go('somewhere');
  }
}

你可以这样使用它。我的例子在打字稿中

class SomeComponent {
   constructor(router: Router) {
      if(something) router.navigate(['/SomeRoute']);
   }
}