Ilj*_*lja 32 javascript http angular
所以,我正在通过打字稿在他们的网站上关注角度2指南,并且我陷入了http api集成.我正在尝试创建一个简单的应用程序,可以通过soundcloud api搜索一首歌,但是我很难实现和理解如何开始,而且在线资源以很多不同的方式完成它(我相信快速的角度2语法更改)早些时候).
所以目前我的项目看起来像这样
app
components
home
home.component.ts
...
search
search.component.ts
...
app.ts
...
services
soundcloud.ts
bootstrap.ts
index.html
Run Code Online (Sandbox Code Playgroud)
在示例中没有任何花哨的东西,主要文件将是
app.ts
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 { }
Run Code Online (Sandbox Code Playgroud)
bootstrap.ts
import {App} from './components/app';
import {bootstrap} from 'angular2/platform/browser';
import {ROUTER_PROVIDERS} from 'angular2/router';
bootstrap(App, [
ROUTER_PROVIDERS
]);
Run Code Online (Sandbox Code Playgroud)
我试图找出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;
}
}
Run Code Online (Sandbox Code Playgroud)
soundcloud api不包括在这里,因为我不能先得到基础知识.
Par*_*ain 36
@langley提供了很好的答案,但我想补充一些积分,以便发布作为答案.
首先,为了使用Rest API,我们需要导入Http和HTTP_PROVIDERS模块.正如我们所说的Http,显然是第一步.
<script src="node_modules/angular2/bundles/http.dev.js"></script>
Run Code Online (Sandbox Code Playgroud)
但是
HTTP_PROVIDERS,在bootstrap文件中提供是一个很好的做法,因为通过使用这种方式,它在全局级别提供,并且可以像这样在整个项目中使用.
bootstrap(App, [
HTTP_PROVIDERS, some_more_dependencies
]);
Run Code Online (Sandbox Code Playgroud)
而且要包括的进口是....
import {Http, Response, RequestOptions, Headers, Request, RequestMethod} from 'angular2/http';
Run Code Online (Sandbox Code Playgroud)
有时我们需要提供Headers消费API以便发送access_token以及更多以这种方式完成的事情:
this.headers = new Headers();
this.headers.append("Content-Type", 'application/json');
this.headers.append("Authorization", 'Bearer ' + localStorage.getItem('id_token'))
Run Code Online (Sandbox Code Playgroud)
现在到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() }]
}
});
}
Run Code Online (Sandbox Code Playgroud)
你也可以在这里参考更多信息.
也可以看看 -
导入已更改
import {Http, Response, RequestOptions, Headers, Request, RequestMethod} from 'angular2/http';
Run Code Online (Sandbox Code Playgroud)
至
import {Http, Response, RequestOptions, Headers, Request, RequestMethod} from '@angular/http';
Run Code Online (Sandbox Code Playgroud)
你需要添加这一行:
<script src="node_modules/angular2/bundles/http.dev.js"></script>
在index.html文件中.
你需要添加HTTP_PROVIDERS:
bootstrap(App, [
ROUTER_PROVIDERS,
HTTP_PROVIDERS
]);
Run Code Online (Sandbox Code Playgroud)
在你的boot.ts/bootstrap.ts文件中,然后导入它们.
您需要导入@Inject您的DojoService类文件:
import {Injectable, Inject} from 'angular2/core'
Run Code Online (Sandbox Code Playgroud)
就像你导入一样@Injectable.
| 归档时间: |
|
| 查看次数: |
52681 次 |
| 最近记录: |