首页 > 解决方案 > 未捕获的 ReferenceError:未定义 http_1

问题描述

我知道这种情况很多,因为这是一个常见问题,可能会有很大差异。

我得到了 http_1 错误,但我已经定义了它。

这是我得到错误的行

  constructor(private http: HttpClient, @Inject('BASE_URL') private baseUrl: 
   string, private activatedRoute: ActivatedRoute) {

 }

而且我一开始就导入了HttpClient

import { HttpClient } from '@angular/common/http';

它曾经在其他服务中工作,但突然间它就停止了,即使代码几乎完全一样。

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

export class MakeService
{
 CurrentId: number;
 CurrentName: string;
 CurrentAbrv: string;
 public makes: Makes[];


 constructor(private http: HttpClient, @Inject('BASE_URL') private baseUrl: 
 string, private activatedRoute: ActivatedRoute) {

 }

 GetId() {

this.activatedRoute.paramMap.subscribe(paramMap => {
  const id = parseInt(paramMap.get('id'), 10) || -1;
  this.GetMakeObjectById(id);
});
 }

GetMakeObjectById(id) {
  this.http.get<Makes[]>(this.baseUrl + "api/SampleData/DetailsMake/" + 
 id).subscribe(result => {
  this.makes = result;

 }, error => console.error(error));
}
}


interface Makes {
Id: number;
 Name: string;
 Abrv: string;
}

标签: c#asp.netangulartypescript

解决方案


// You were missing @Injectable()

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

@Injectable()
export class MakeService{
 CurrentId: number;
 CurrentName: string;
 CurrentAbrv: string;
 public makes: Makes[];


 constructor(private http: HttpClient, @Inject('BASE_URL') private baseUrl: 
 string, private activatedRoute: ActivatedRoute) {

 }

 GetId() {

this.activatedRoute.paramMap.subscribe(paramMap => {
  const id = parseInt(paramMap.get('id'), 10) || -1;
  this.GetMakeObjectById(id);
});
 }

GetMakeObjectById(id) {
  this.http.get<Makes[]>(this.baseUrl + "api/SampleData/DetailsMake/" + 
 id).subscribe(result => {
  this.makes = result;

 }, error => console.error(error));
}
}


interface Makes {
Id: number;
 Name: string;
 Abrv: string;
}

推荐阅读