Javascript Angularjs+;Typescript,如何将$routeParams与IRouteParamsService一起使用
我使用$routeParams从URI中提取属性,并为它们设置本地变量 当我使用typescripts键入设置$routeParams的类型时,我无法让日志记录器访问$routeParams 如何访问$routeParams中的属性Javascript Angularjs+;Typescript,如何将$routeParams与IRouteParamsService一起使用,javascript,angularjs,typescript,angularjs-routing,Javascript,Angularjs,Typescript,Angularjs Routing,我使用$routeParams从URI中提取属性,并为它们设置本地变量 当我使用typescripts键入设置$routeParams的类型时,我无法让日志记录器访问$routeParams 如何访问$routeParams中的属性 class Controller{ constructor($routeParams: ng.route.IRouteParamsService, private $location: ng.ILocationService) this.pr
class Controller{
constructor($routeParams: ng.route.IRouteParamsService, private $location: ng.ILocationService)
this.propetry1 = this.getProperty1($routeParams.property1);
}
property1: string;
getProperty1(input: string):string{
return (input || "Not present");
}
}
ng.route.IRouteParamsService代码为:
interface IRouteParamsService {
[key: string]: any;
}
这有一个错误:ng.route.IRouteParamsService类型上不存在属性“property1”
如果我将$routeParams的类型更改为:any,那么它将正确设置property1。
如何在仍然访问$routeParams中的属性的情况下保持Typescript的严格键入?解决了这个问题。您需要声明一个使用IRoutParamsService的接口,并指定要使用的属性
class Controller{
constructor($routeParams: ng.route.IRouteParamsService, private $location: ng.ILocationService)
this.propetry1 = this.getProperty1($routeParams.property1);
}
property1: string;
getProperty1(input: string):string{
return (input || "Not present");
}
interface IRouteParams extends ng.route.IRouteParamsService {
property1:string;
}
class Controller{
constructor($routeParams: IRouteParams, private $location: ng.ILocationService)
this.propetry1 = this.getProperty1($routeParams.property1);
}
property1: string;
getProperty1(input: string):string{
return (input || "Not present");
}
}
请注意控制器构造函数中的更改。您可以使用
$routeParams['property1']
而不是$routeParams.property1