Xgo*_*oya 12 javascript angularjs typescript angularjs-routing
我使用$ routeParams从URI中提取属性并将本地变量设置为它们.
当我使用typescripts键入来设置$ routeParams的类型时,我无法访问$ routeParams.
如何访问$ routeParams中的属性?
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");
}
Run Code Online (Sandbox Code Playgroud)
}
ng.route.IRouteParamsService代码是:
interface IRouteParamsService {
[key: string]: any;
}
Run Code Online (Sandbox Code Playgroud)
这有一个错误:属性'property1在ng.route.IRouteParamsService的类型上不存在'
如果我将$ routeParams的类型更改为:any,则它会正确设置property1.如何在仍然访问$ routeParams中的属性的同时保持Typescript的严格输入?
Xgo*_*oya 25
弄清楚了.您需要声明一个使用IRouteParamsService的接口并指定要使用的属性.
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");
}
}
Run Code Online (Sandbox Code Playgroud)
注意控制器构造函数的更改.
| 归档时间: |
|
| 查看次数: |
5518 次 |
| 最近记录: |