c# - 未捕获的 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;
}
解决方案
// 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;
}
推荐阅读
- sql-server - 如何访问 IIS 中的 HTTP 请求日志或查询从实体框架发送到 SQL Server 的日志?
- tree - 叶子相似的树
- javascript - 从 Stripe 显示货币金额(任何货币的最小单位)的最佳方式是什么?
- google-sheets - google sheet countif 如果值为 lotto,则过滤掉其他列
- postgresql - 查询挂起时如何解释 PostgreSQL EXPLAIN 结果
- c# - 如何使用 System.Windows.Forms 键模拟“_”或“=”的按键?
- android - Android AsyncTask 不调用 onProgressUpdate
- shell - Unix cat 命令、管道和标准输入/标准输出
- javascript - 从具有特定值的现有对象创建新对象的最佳方法
- c++ - 如何在 Visual Studio C++ 中集成 CryptoPP (2019)