问题描述
我正在开发Angular / AngularJS混合应用程序。 主要的AppModule是Angular模块,该模块使用使用给定状态的AppRoutingModule。像这样的东西:
AppModule:
imports: [AModule,BModule,AppRoutingModule,...]
AppRoutingModule:
imports: [UIRouterUpgradeModule.forRoot(states: some_state)]
状态:
export const some_state: [
{
name: 'a',url: '/a'
views: {
main: {component: AComponent}
}
},{
name: 'b',url: '/b'
views: {
main: {component: BComponent}
}
}
]
我想做的-说我在BComponent中,我想导航到AComponent。 在纯角度组件中,我将使用RouterModule中的Router(AppModule中的forRoot和子模块中的forChild)。 如router.navigatetoURL('...')
现在,我正在使用UIRouterUpgradeModule,但找不到如何执行此操作。 任何帮助将非常感谢!
解决方法
在您的组件中,首先在构造函数中声明router
,如下所示:
import { Component } from '@angular/core';
import { Router } from '@angular/router';
@Component({
// ...
})
export class AppComponent {
constructor(private router: Router) {}
// ...
}
然后创建一个将在需要更改路线时使用的功能,您可以将此功能绑定到任何事件,例如单击按钮以查看所需的更改:
changeRoute() {
this.router.navigateByUrl('/b');
}
,
所以对我有用的是: 像这样从构造函数中的@ uirouter / core导入StateService:
constructor(private stateService: StateService)
在我的方法中执行了以下操作:
stateService.go('URL_TO_GO_TO');
这对我有用,我被按需重定向。