简体   繁体   English

如何在Typescript中实现$ http?

[英]How can I implement $http in Typescript?

I am having problems defining my $http call in Typescript. 我在Typescript中定义$ http调用时遇到问题。 Previously I was using .success and .error like this: 以前我使用.success和.error像这样:

this.$http({
            method: "GET",
            url: self.ac.dataServer + url
        })
            .success((data: any, status: number, headers: (headerName: string) => string, config: ng.IRequestConfig): void => {
                self.topics = angular.copy(data);
                self.topicsBase = angular.copy(data);
                this.$state.transitionTo('home.subjects.subject.admin.topics', {
                    subjectId: this.sus.subject.id
                });
            })
            .error((data, status, headers, config): void => {
                self.topics = null;
                self.topicsBase = null;
            })
            .finally((): void => {
            });

Now I changed to use .then: 现在我改为使用.then:

 this.$http({
            method: "GET",
            url: self.ac.dataServer + url
        })
            .then(
            (response): any => {
                self.topics = angular.copy(response.data);
                self.topicsBase = angular.copy(response.data);
                this.$state.transitionTo('home.subjects.subject.admin.topics', {
                    subjectId: this.sus.subject.id
                });},
            (response): any => {
                self.topics = null;
                self.topicsBase = null;
            }
            );

But this gives me the following errors: 但这给了我以下错误:

Severity Code Description Project File Line Error TS2322 Type '{}' is not assignable to type 'ITopic[]'. 严重级代码说明项目文件行错误TS2322类型“{}”不能分配给“ITopic []”类型。 Property 'length' is missing in type '{}'. 类型“{}”中缺少属性“长度”。 admin C:\\H\\admin\\admin\\app\\services\\topicservice.ts 214 Severity Code Description Project File Line Error TS2322 Type '{}' is not assignable to type 'ITopic[]'. admin C:\\ H \\ admin \\ admin \\ app \\ services \\ topicservice.ts 214严重级代码说明项目文件行错误TS2322类型“{}”不能分配给“ITopic []”类型。 admin C:\\H\\admin\\admin\\app\\services\\topicservice.ts 215 admin C:\\ H \\ admin \\ admin \\ app \\ services \\ topicservice.ts 215

The errors come up on the lines where I am assigning an angular.copy(data) to self.topics and self.topicsBase 错误出现在我为self.topics和self.topicsBase分配angular.copy(数据)的行上。

Argument of type '(data: IHttpPromiseCallbackArg<{}>, status: any, headers: any, config: any) => any' is not assignable to parameter of type '(response: IHttpPromiseCallbackArg<{}>) => any' 参数类型'(data: IHttpPromiseCallbackArg<{}>, status: any, headers: any, config: any) => any'不能赋值给'(response: IHttpPromiseCallbackArg<{}>) => any'参数'(response: IHttpPromiseCallbackArg<{}>) => any'

You are passing a function that takes more arguments than what is returned by $http() . 您传递的函数需要的参数多于$http()返回的参数。

Fixed : 修正:

$http({
    method: "GET",
    url: myURL
})
    .then((response): any => { },(): any => {});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM