1
如何使用JavaScript(ES6)在AngularJS 2中执行HTTP GET请求?该文档仅用TypeScript显示。使用AngularJS 2和ES6进行HTTP GET请求
如何使用JavaScript(ES6)在AngularJS 2中执行HTTP GET请求?该文档仅用TypeScript显示。使用AngularJS 2和ES6进行HTTP GET请求
你可以使用类似的东西:
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> <---
引导您的应用程序时设置HTTP_PROVIDERS
提供商:
import {HTTP_PROVIDERS} from 'angular2/http';
(...)
bootstrap(AppComponent, [ HTTP_PROVIDERS ]);
实际上,唯一特定于ES6的是w唉配置依赖注入与静态获取...
我如何通过参数,例如'id = 1' –
使用'URLSearchParams',但它不是特定于ES6的东西...我更新了我的答案 –
谢谢对于我们的帮助,我将tr并发布结果 –