[英]require("http2") in angular 7
我想在我的 angular 項目中將 http2 用於客戶端。
const http2 = require("http2");
const client = http2.connect("http://localhost:8443");
const req = client.request({
":path": "/"
});
當我為 http2 請求編寫此代碼塊時,出現錯誤Module not found: Error: Can't resolve 'http2' in '/path' 。
有 http2 包(“npm install http2”命令),但該包顯示“此包已被棄用”和“改為使用節點 9.0.0 或更新版本中的內置模塊”。 因此,我不能使用這個包。
因此,我無法使用像 nodejs 這樣的 http2 客戶端從服務器獲取數據。 我該如何解決這些問題?
您需要使用內置的HttpClient從服務器獲取數據。
1- 制作服務並在您的服務中導入 httpclient,如下所示。
import { HttpClient, HttpHeaders } from '@angular/common/http';
getData() {
return this.http.get(serverUrl, {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
});
}
2- 在您的組件中導入服務。 依賴注入//fetcher
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);
})
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.