Javascript 使用AngularJS 2和ES6的HTTP GET请求
如何使用JavaScript(ES6)在AngularJS 2中执行HTTP GET请求?文档仅显示TypeScript。您可以使用以下内容:Javascript 使用AngularJS 2和ES6的HTTP GET请求,javascript,angularjs,angular,http-get,Javascript,Angularjs,Angular,Http Get,如何使用JavaScript(ES6)在AngularJS 2中执行HTTP GET请求?文档仅显示TypeScript。您可以使用以下内容: import {Http, URLSearchParams} from 'angular2/http'; @Injectable() export class SomeHttpService { constructor(http) { this.http = http; } getSomething() { URLSear
import {Http, URLSearchParams} from 'angular2/http';
@Injectable()
export class SomeHttpService {
constructor(http) {
this.http = http;
}
getSomething() {
URLSearchParams params = new URLSearchParams();
params.set('id', '1');
return this.http.get('http://...', { search: params }).map(res => res.map());
/*
or if you need to subscribe in the service
this.http.get('http://...').map(res => res.map())
.subscribe(
(data) => {
// do something with data
}
);
*/
}
static get parameters() {
return [[Http]];
}
}
不要忘记导入Angular2 Http模块文件:
<script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>
<script src="node_modules/rxjs/bundles/Rx.js"></script>
<script src="node_modules/angular2/bundles/angular2.dev.js"></script>
<script src="node_modules/angular2/bundles/http.dev.js"></script> <---
事实上,ES6唯一特有的是使用静态getter配置依赖注入的方法…我如何传递参数,例如使用
URLSearchParams
传递“id=1”,但这不是ES6特有的。。。我更新了我的回答感谢我们的帮助,我将tr并发布结果什么是HTTP_提供者?HTTP_提供者
包含使用HTTP的提供者列表。它包含Http
一个类,但也包含Http
类所依赖的其他类。您需要定义它们,以便能够在服务或组件中插入Http
实例。
import {HTTP_PROVIDERS} from 'angular2/http';
(...)
bootstrap(AppComponent, [ HTTP_PROVIDERS ]);