我有一个服务,它用返回延迟对象的函数包装 $http。
我的界面:
export interface MyServiceScope {
get: ng.IPromise<{}>;
}
我的课:
export class MyService implements MyServiceScope {
static $inject = ['$http', '$log'];
constructor(private $http: ng.IHttpService,
private $log: ng.ILogService,
private $q: ng.IQService) {
this.$http = $http;
this.$log = $log;
this.$q = $q;
}
get(): ng.IPromise<{}> {
var self = this;
var deferred = this.$q.defer();
this.$http.get('http://localhost:8000/tags').then(
function(response) {
deferred.resolve(response.data);
},
function(errors) {
self.$log.debug(errors);
deferred.reject(errors.data);
}
);
return deferred.promise;
}
}
编译失败并出现以下错误:
myservice.ts(10,18): error TS2420: Class 'MyService' incorrectly implements interface 'MyServiceScope'.
Types of property 'get' are incompatible.
Type '() => IPromise<{}>' is not assignable to type 'IPromise<{}>'.
Property 'then' is missing in type '() => IPromise<{}>'.
以供参考,这是IPromise https://github.com/DefinitelyTyped/DefinitelyTyped/blob/af81415504b4edce8dac8446d96857f8018c6e9f/angularjs/angular.d.ts#L1046来自DefinitelyTyped 的定义。这IQService.defer()
调用返回一个IDeferred
对象,然后deferred.promise
返回 IPromise 对象。
我不确定我是否在接口中使用了错误的定义,或者没有以相同的方式返回延迟对象。任何投入将不胜感激!
在您的界面中您定义了一个属性get
在服务实现中它是一个函数get()
。您可能想要的是一个函数,因此接口应该是:
export interface MyServiceScope {
get(): ng.IPromise<{}>;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)