angularjs – 使用Typescript时如何用$http定义回调?
发布时间:2020-12-17 07:31:04 所属栏目:安全 来源:网络整理
导读:我的$http调用看起来像这样,我想知道最灵活的方法来处理.success和.error中返回的所有参数? this.$http({ url: "/api/x,method: "GET" }) .success((??) : void = { }) .error((??) : void = { }) Angular文档告诉我返回以下内容: data – {string|Object}
我的$http调用看起来像这样,我想知道最灵活的方法来处理.success和.error中返回的所有参数?
this.$http({ url: "/api/x,method: "GET" }) .success((??) : void => { }) .error((??) : void => { }) Angular文档告诉我返回以下内容: data – {string|Object} – The response body transformed with the transform functions. status – {number} – HTTP status code of the response. headers – {function([headerName])} – Header getter function. config – {Object} – The configuration object that was used to generate the request. statusText – {string} – HTTP status text of the response. angular.d.ts告诉我: interface IHttpPromiseCallback<T> { (data: T,status: number,headers: (headerName: string) => string,config: IRequestConfig): void; } interface IHttpPromiseCallbackArg<T> { data?: T; status?: number; headers?: (headerName: string) => string; config?: IRequestConfig; statusText?: string; } 但我仍然感到困惑.有没有人使用上面的两个接口来定义回调,他们是如何做到的? 理想情况下,我希望有这样的东西: .success((data: any,config: any) : void => { 但是使用接口. 任何人都可以告诉我,如果我在正确的轨道上,如果我可以使用接口,而不是必须在参数后指定:any:number等.
以下是GET的示例:
private getInternal<T>(url) { var deferred = this.$q.defer(); this.$http({ url: url,method: "GET" }) .success((data: T,config: ng.IRequestConfig): void => { if (status == 200 && headers('location') == null && config.timeout > 200) { //do something with data } return deferred.resolve(data); }) .error((data: T,config: ng.IRequestConfig): void => { if (status == 500 && headers('myAuth') != null && config.method == 'GET') { // write to log } return deferred.reject(data); }); } 现在让我们说你有一个服务,你知道你得到一个特定的对象: this.getInternal<mySpecObject>('service/abc').then(res => doSomethingWithData(res)); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |