我想使用http2作为客户端在我的角度项目。
const http2 = require("http2");
const client = http2.connect("http://localhost:8443");
const req = client.request({
":path": "/"
});当我为http2请求编写这个代码块时,没有找到错误模块:错误:无法解析'/path‘中的'http2’.
有http2包("npm http2“命令),但是这个包显示”这个包已被废弃“和”使用节点9.0.0或更高版本中的内置模块“。因此,我不能使用这个包。
因此,我无法使用像nodejs这样的http2客户端从服务器获取数据。我怎样才能解决这些问题?
发布于 2020-01-13 13:01:33
您需要使用内置的HttpClient从服务器获取数据。
1-在服务中创建服务并导入httpclient,如下所示。
import { HttpClient, HttpHeaders } from '@angular/common/http';
getData() {
return this.http.get(serverUrl, {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
});
}2-在组件中导入服务。依赖注入 //取器
import { FetcherService } from 'relative path';
// inject it in constructor
constructor(public fetcherService: FetcherService) {}3-订阅可观察到的服务组件。
saveUpdatedGeojson() {
this.fetcherService.getData().subscribe((response: any) => {
console.log(response)
}, error => {
console.log(error);
})
}https://stackoverflow.com/questions/59716730
复制相似问题