程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了在angular 2中使用HTTP Rest API大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决在angular 2中使用http Rest API?

开发过程中遇到在angular 2中使用http Rest API的问题如何解决?下面主要结合日常开发的经验,给出你关于在angular 2中使用http Rest API的解决方法建议,希望对你解决在angular 2中使用http Rest API有所启发或帮助;

提供的很好的答案,但是我想补充一点,所以发布作为答案。

首先,要使用Rest API,我们需要导入httphttp_PROVIDERS模块。当我们谈论http时,第一步显然是。

<script src="node_modules/angular2/bundles/http.dev.Js"></script>

但是,是的http_PROVIDERS,在bootstrap文件中提供一个好习惯,因为通过这种方式,它是在全局级别提供的,并且可以像这样在整个项目中使用。

bootstrap(App, [
    http_PROVIDERS, some_more_dependencIEs
]);

和要包括的进口是…

import {http, Response, requestoptions, headers, request, requestMethoD} from 'angular2/http';

有时我们需要headers在消耗API的同时提供发送access_token信息,并通过这种方式完成更多工作:

this.headers = new headers();
this.headers.append("Content-Type", 'application/Json');
this.headers.append("Authorization", 'Bearer ' + localstorage.getItem('ID_token'))

现在到requestMethods:基本上,我们使用GET,POST,但是您可以在此处引用更多选项…

我们可以使用requestmethods作为 requestMethod.method_name

API还有更多选项,但现在我发布了一个POST请求示例,该示例将通过一些重要方法为您提供帮助:

Postrequest(url,data) {
        this.headers = new headers();
        this.headers.append("Content-Type", 'application/Json');
        this.headers.append("Authorization", 'Bearer ' + localstorage.getItem('ID_token'))

        this.requestoptions = new requestoptions({
            method: requestMethod.Post,
            url: url,
            headers: this.headers,
            body: JsON.Stringify(data)
        })

        return this.http.request(new request(this.requestoptions))
            .map((res: ResponsE) => {
                if (res) {
                    return [{ status: res.status, Json: res.Json() }]
                }
            });
    }

您也可以在这里参以获取更多信息。

也可以看看 -

  • 在Angular 2中如何处理200以外的http状态代码。

更新资料

导入已从更改为

import {http, Response, requestoptions, headers, request, requestMethoD} from 'angular2/http';

import {http, Response, requestoptions, headers, request, requestMethoD} from '@angular/http';

解决方法

因此,我正在通过打字稿在其网站上遵循angular2指南,并停留在httpapi集成中。我正在尝试制作一个可以通过soundcloudapi搜索一首歌曲的简单应用程序,但是我很难实现和理解如何入门,在线资源却以多种不同方式做到这一点(我相信可以快速进行角度2语法更改早些时候)。

所以目前我的项目看起来像这样

app
  components
    home
      home.component.ts
      ...
    search
      search.component.ts
      ...
    app.ts
    ...
  services
    soundcloud.ts
  bootstrap.ts
index.html

在该示例中没有任何花哨的内容,主文件将是

应用程序

import {Component,View} from 'angular2/core';
import {RouteConfig,ROUTER_DIRECTIVES} from 'angular2/router';

import {HomeComponent} from './home/home.component';
import {SearchComponent} from './search/search.component';

@Component({
    SELEctor: 'app',templateUrl: 'app/components/app.html',directives: [ROUTER_DIRECTIVES]
})

@RouteConfig([
  {path: '/home',name: 'Home',component: HomeComponent,useAsDefault: truE},{path: '/search',name: 'Search',component: SearchComponent}
])

export class App { }

bootstrap.ts

    import {App}     from './components/app';
import {Bootstrap}        from 'angular2/platform/browser';
import {ROUTER_PROVIDERS} from 'angular2/router';

bootstrap(App,[
  ROUTER_PROVIDERS
]);

而且我试图找出soundcloud.ts,但是无法执行,并且以下方法存在错误,即@Inject找不到(我假设我在这里使用过时的语法)。本质上,我想在我的应用程序表单搜索组件中将soundcloud服务用于api调用。

import {InjectablE} from 'angular2/core'
import {http} from 'angular2/http'

@Injectable()
export class Soundcloudservice {
 http : http

 constructor(@Inject(http) http) {
   this.http = http;
 }
}

这里不包括soundcloud api,因为我无法首先掌握基础知识。

大佬总结

以上是大佬教程为你收集整理的在angular 2中使用HTTP Rest API全部内容,希望文章能够帮你解决在angular 2中使用HTTP Rest API所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。