在JS库中调用的AppComponent中声明的Function中未定义Http

时间:2018-12-08 16:20:50

标签: javascript angular angular-httpclient ajv

试图在另一个javascript库(AJV)中调用从http响应返回承诺的函数,但httpClient模块未定义

问题是我们是否可以在任何其他节点模块中按appcomponent使用导入的模块。

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

1 个答案:

答案 0 :(得分:0)

您是否已在AppModule中导入HttpClientModule?