大佬教程收集整理的这篇文章主要介绍了在angular 2中使用HTTP Rest API,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
提供的很好的答案,但是我想补充一点,所以发布作为答案。
首先,要使用Rest API,我们需要导入http
和http_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() }]
}
});
}
您也可以在这里参考以获取更多信息。
也可以看看 -
导入已从更改为
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,请注明来意。