简体   繁体   中英

require("http2") in angular 7

I want to use http2 for client side in my angular project.

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' .

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". Thus, I cannot use this package.

Thus, I cannot get data from server using http2 client like nodejs. How can I fix these problem?

  • Node version: v13.3.0
  • npm version: 6.13.1
  • @angular/cli: "~7.3.7"

You need to use the built in HttpClient to get the data from server.

1- Make service and import httpclient in your service like below.

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. Dependency Injection //fetcher

import { FetcherService } from 'relative path';
// inject it in constructor
constructor(public fetcherService: FetcherService) {}

3- subscribe service observable to you component.

saveUpdatedGeojson() {
    this.fetcherService.getData().subscribe((response: any) => {
       console.log(response)
    }, error => {
       console.log(error);
    })
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM