未捕获的ReferenceError:未定义http_1

时间:2018-12-12 16:22:22

标签: c# asp.net angular typescript

我知道其中有很多,因为这是一个常见的问题,可能相差很大。

我收到了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;
}

1 个答案:

答案 0 :(得分:0)

// 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;
}