【发布时间】:2018-08-20 07:19:31
【问题描述】:
我们使用 angular-ui-router.js v 0.2.18 + angular-ui-router.d.ts v 1.4.6(typescript v 1.8.36.0 )。
所以我们使用IStateProvider 来配置从一种状态到另一种状态的导航。
通常我们需要将一些状态参数从父状态传递到子状态。
配置:
.state(States.MeasureDetail, {
parent: States.OperatorView,
templateUrl: '/App/Business/Kanban/BuildAndCureKanban/Views/MeasureDetail.html',
controller: Controllers.MeasureDetailController,
controllerAs: 'measureDetailCtrl',
params:
{
machine: null,
material: null
}
})
在父控制器中:
this.$state.go(States.MeasureDetail, {
machine: this.currentMachine,
material: this.currentMaterial
});
在子控制器中:
constructor(
private buildAndCureService: Services.IBuildAndCureService,
private $state: ng.ui.IStateService,
private eventManager: _Common.Services.IEventManager){
this.machine = $state.params["machine"];
this.material = $state.params["material"];
}
问题
params 属性和 IStateParameterService 接口都不是强类型的:您通过键访问并获得任何类型。
params?: any;
interface IStateParamsService {
[key: string]: any;
}
这是一个弱点,在有许多开发人员的大型项目中,已经带来了运行时问题。
问题
我知道可以像the example in this post to add a typed field 一样扩展 IStateParameterService,但解决方案是一次适用于所有状态,而我需要一个解决方案来逐个状态添加类型化字段。
如何将 IStateParameterService 扩展为类似:
interface IStateParamsService {
CustomParam<T>?: T;
}
?
谢谢。
【问题讨论】:
-
只需为每个状态定义 ICustomStateParams 并使用它来代替 IStateParameterService。您对这种方法有疑问吗?
-
@estus 您能否通过添加一些代码示例使您的解决方案更清晰?如果您可以修改我的用例,那就更好了。谢谢。
标签: angularjs typescript generics angular-ui-router