[英]require("http2") in angular 7
I want to use http2 for client side in my angular project.我想在我的 angular 项目中将 http2 用于客户端。
const http2 = require("http2");
const client = http2.connect("http://localhost:8443");
const req = client.request({
":path": "/"
});
When I wrote this code block for http2 request, I get error Module not found: Error: Can't resolve 'http2' in '/path' .当我为 http2 请求编写此代码块时,出现错误Module not found: Error: Can't resolve 'http2' in '/path' 。
There was http2 package ("npm install http2" command), but this package showing "This package has been deprecated" and "Use the built-in module in node 9.0.0 or newer, instead".有 http2 包(“npm install http2”命令),但该包显示“此包已被弃用”和“改为使用节点 9.0.0 或更新版本中的内置模块”。 Thus, I cannot use this package.因此,我不能使用这个包。
Thus, I cannot get data from server using http2 client like nodejs.因此,我无法使用像 nodejs 这样的 http2 客户端从服务器获取数据。 How can I fix these problem?我该如何解决这些问题?
You need to use the built in HttpClient to get the data from server.您需要使用内置的HttpClient从服务器获取数据。
1- Make service and import httpclient in your service like below. 1- 制作服务并在您的服务中导入 httpclient,如下所示。
import { HttpClient, HttpHeaders } from '@angular/common/http';
getData() {
return this.http.get(serverUrl, {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
});
}
2- import service in your component. 2- 在您的组件中导入服务。 Dependency Injection //fetcher依赖注入//fetcher
import { FetcherService } from 'relative path';
// inject it in constructor
constructor(public fetcherService: FetcherService) {}
3- subscribe service observable to you component. 3-订阅服务可观察到您的组件。
saveUpdatedGeojson() {
this.fetcherService.getData().subscribe((response: any) => {
console.log(response)
}, error => {
console.log(error);
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.