2016-07-08 35 views
1

路由所以我使用的UI,路由器NG2,我試圖改變路由功能發生後Angular2 UI路由器,我的代碼看起來是這樣的:-ng2從控制器

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

使用從路由器HTML使用uiSref很簡單,但是,我不知道如何使用新的ng2路由器從組件內部進行路由,他們的文檔沒有多大幫助。

回答

0

你可以像這樣使用它。我的例子是在Typescript中

class SomeComponent { 

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

我應該輸入什麼才能使用它?用於uiSref的 使用我導入的UIRouterDirectives。 我應該從構造函數 –

+0

導入動作這是角2示例我將編輯答案並添加角1示例 – Kliment

+0

我不認爲你明白我的意思:P 我在角2中工作, 只是搜索爲了使用ng2-uirouter state.go我需要導入 –

2

在組件中注入StateService

import { StateService } from "ui-router-ng2"; 

@Component({}) 
class MyComponent { 
    constructor(public stateService: StateService) {} 

    goSomewhere() { 
    this.stateService.go('somewhere'); 
    } 
}