无法在Angular中使用“ http”发送请求

时间:2020-07-28 11:28:02

标签: angular api http

我正在尝试向WebApi发送请求,但“ http”字样给出了一个错误,提示“类型'ProductComponent'上不存在属性'http'”

这是我的代码

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from "@angular/router";
import { HttpClient } from "@angular/common/http";
import { Observable } from 'rxjs';

interface Product {
  description: string;
  courseListIcon: string;
  iconUrl: string;
  longDescription: string;
  url: string;
}

@Component({
  selector: 'app-product',
  templateUrl: './product.component.html',
  styleUrls: ['./product.component.css']
})
export class ProductComponent implements OnInit {

  public urlParameter: string;
  public product: Object; //Product

  constructor(private route: ActivatedRoute) { }

  ngOnInit() {
    this.urlParameter = this.route.snapshot.paramMap.get("urun");
    console.log(this.urlParameter);

    this.product = this.http.get<Product>("url");      // this line gives the error ***********
  }

}

我在这里缺少进口商品吗?请帮助...

1 个答案:

答案 0 :(得分:2)

您需要将HTTPClient注入您的组件类。

constructor(private route: ActivatedRoute, private: http: HTTPClient) { 

}