javascript - 在 JS 库中调用的 AppComponent 中声明的函数中未定义 Http
问题描述
试图调用一个函数,该函数从另一个 javascript 库 (AJV) 中的 http 响应返回一个承诺,但未定义的 httpClient 模块
问题是我们可以在任何其他节点模块中使用 appcomponent 导入的模块吗?
应用组件文件:
import { Component, OnInit } from '@angular/core';
import * as Ajv from 'ajv';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-root',
templateUrl: './app.component.html'
})
export class AppComponent implements OnInit{
title = 'testajv';
schema: any = {
//Some Json Schema
};
ajv: any = new Ajv({loadSchema: this.loadSchema});
validate: any = null;
valid: any ;
data: any = {
"foo": ""
};
uri: string = "assets/schemas/defs.json";
constructor(private http: HttpClient) {}
ngOnInit(){
this.ajv.compileAsync(this.schema).then(
(validate) => {
this.valid = validate(this.data);
console.log(validate.schema);
});
}
loadSchema(uri){
return this.http.get(uri).toPromise().then(
res => {
return res;
}
)
}
}
ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'get' of undefined
TypeError: Cannot read property 'get' of undefined
at Object.push../src/app/app.component.ts.AppComponent.loadSchema (app.component.ts:79)
at loadMissingSchema (async.js:67)
at _compileAsync (async.js:56)
at async.js:32
解决方案
您是否在 AppModule 中导入了 HttpClientModule ?
推荐阅读
- time-complexity - 下面几何级数的时间复杂度是多少?
- python - 在 10.7.5 上构建 Nikola 时出现 Ascii 错误
- salesforce - 在 Salesforce 中更新潜在客户时触发第三方 API 请求
- autodesk-forge - Forge Viewer Markups 最新版本
- javascript - 带有 firebase.add 未定义错误的 ReactJS
- python - 包含 django_migrations 表的 Django 数据库在哪里?
- c# - 如何动态地更改 Grid 孩子的属性?
- netlogo - 我希望只有当电池能量达到一定值时,需求才会增加一倍
- rgraph - 使用鼠标动态平移和缩放 rGraph 折线图
- javascript - 严格动画需要 D3.js API 的哪些部分(即没有图表、没有绘图、没有图表等)