在 angular 7 中要求("http2")
require("http2") in angular 7
我想在我的 angular 项目中为客户端使用 http2。
const http2 = require("http2");
const client = http2.connect("http://localhost:8443");
const req = client.request({
":path": "/"
});
当我为 http2 请求编写此代码块时,出现错误 找不到模块:错误:无法解析 '/path' 中的 'http2'。
有 http2 包("npm install http2" 命令),但此包显示 "This package has been deprecated" 和 "Use the built-in module in node 9.0.0 or newer, instead"。因此,我不能使用这个包。
因此,我无法使用像 nodejs 这样的 http2 客户端从服务器获取数据。我该如何解决这些问题?
- 节点版本:v13.3.0
- npm 版本:6.13.1
- @angular/cli: "~7.3.7"
您需要使用内置的 HttpClient 从服务器获取数据。
1- 创建服务并在您的服务中导入 httpclient,如下所示。
import { HttpClient, HttpHeaders } from '@angular/common/http';
getData() {
return this.http.get(serverUrl, {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
});
}
2- 在您的组件中导入服务。 Dependency Injection //获取器
import { FetcherService } from 'relative path';
// inject it in constructor
constructor(public fetcherService: FetcherService) {}
3- 订阅 service 可观察组件。
saveUpdatedGeojson() {
this.fetcherService.getData().subscribe((response: any) => {
console.log(response)
}, error => {
console.log(error);
})
}
我想在我的 angular 项目中为客户端使用 http2。
const http2 = require("http2");
const client = http2.connect("http://localhost:8443");
const req = client.request({
":path": "/"
});
当我为 http2 请求编写此代码块时,出现错误 找不到模块:错误:无法解析 '/path' 中的 'http2'。
有 http2 包("npm install http2" 命令),但此包显示 "This package has been deprecated" 和 "Use the built-in module in node 9.0.0 or newer, instead"。因此,我不能使用这个包。
因此,我无法使用像 nodejs 这样的 http2 客户端从服务器获取数据。我该如何解决这些问题?
- 节点版本:v13.3.0
- npm 版本:6.13.1
- @angular/cli: "~7.3.7"
您需要使用内置的 HttpClient 从服务器获取数据。
1- 创建服务并在您的服务中导入 httpclient,如下所示。
import { HttpClient, HttpHeaders } from '@angular/common/http';
getData() {
return this.http.get(serverUrl, {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
});
}
2- 在您的组件中导入服务。 Dependency Injection //获取器
import { FetcherService } from 'relative path';
// inject it in constructor
constructor(public fetcherService: FetcherService) {}
3- 订阅 service 可观察组件。
saveUpdatedGeojson() {
this.fetcherService.getData().subscribe((response: any) => {
console.log(response)
}, error => {
console.log(error);
})
}