【问题标题】:Angular2 UI router -ng2 routing from the controllerAngular2 UI 路由器 -ng2 来自控制器的路由
【发布时间】:2016-11-10 21:29:03
【问题描述】:

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

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

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

【问题讨论】:

    标签: javascript angular routing angular-ui-router


    【解决方案1】:

    在你的组件中注入StateService

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

    【讨论】:

    • 似乎最新的语法是 import {StateService} from '@uirouter/core';
    【解决方案2】:

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

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

    【讨论】:

    • 我应该导入什么才能使用它?对于 uiSref 使用我导入 UIRouterDirectives。我应该从构造函数中为操作导入什么
    • 这是 angular 2 示例,我将编辑答案并添加 angular 1 示例
    • 我不认为你明白我的意思:P 我在 Angular 2 中工作,只是搜索我需要的导入以使用 ng2-uirouter state.go
    猜你喜欢
    • 2016-11-16
    • 1970-01-01
    • 2016-04-08
    • 2016-06-15
    • 2016-07-12
    • 1970-01-01
    • 1970-01-01
    • 2018-05-13
    • 2017-01-30
    相关资源
    最近更新 更多