TypeError:Ajv不是构造函数 [英] TypeError: Ajv is not a constructor

查看:218
本文介绍了TypeError:Ajv不是构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在此类中尝试使用new关键字实例化



此代码过去一直有效,这就是Ajv的使用方式。来自Ajv文档:


最快的验证呼叫:




  var Ajv = require('ajv'); 
var ajv = new Ajv(); //可以传递选项,例如{allErrors:true}
var validate = ajv.compile(schema);
var valid = validate(data);
if(!valid)console.log(validate.errors);

我怎么会收到此错误?



有关如何导入Ajv库-systemjs.config.js的信息,请参见此文章的底部。

 (function(global ){
System.config({
path:{
//路径用作别名
'npm:':'lib / js /'
},
//地图告诉系统加载程序在哪里寻找东西
地图:{
app:'app',
//角形包
'@ angular / core' :'npm:@ angular / core / bundles / core.umd.js',
'@ angular / common':'npm:@ angular / common / bundles / common.umd.js',
'@ angular / compiler':'npm:@ angular / compiler / bundles / compiler.umd.js',
'@ angular / platform-b​​rowser':'npm:@ angular / platform-b​​rowser / bundles / platform -browser.umd.js',
'@ angular / platform-b​​rowser-dynamic':'npm:@ angular / platform-b​​rowser-dynamic / bundles / platform-b​​rowser-dynamic.umd.js',
'@ angular / http':'npm:@ angular / http / bundles / http.umd.js',
'@ angular / router':'npm:@ angular / router / bundles / router.umd.js ',
'@ angular / forms':'npm:@ angular / forms / bundles / forms.umd.js',
//其他库
'rxjs':'npm:rxjs ',
'angular2-in-memory-web-api':'npm:angular2-in-memory-web-api',
'angular2-google-maps / core':'npm:angular2 -google-maps / core / core.umd.js',
'ajv':'npm:ajv / dist / ajv.min.js',
'primeng':'npm:primeng'


解决方案

我看到Ajv具有默认功能,所以我更改了代码:

  let ajv = new Ajv.default({allErrors:true}); 

不是100%知道那里发生了什么,但是它可以工作。


I have this class where I try to instantiate Ajv with the new keyword and I get this error:

TypeError: Ajv is not a constructor

Code:

import * as Ajv from "ajv";

    export class ValidateJsonService {
        validateJson(json, schema) {
            console.log(Ajv);
            let ajv = new Ajv({ allErrors: true });
            if (!ajv.validate(schema, json)) {
                throw new Error("JSON does not conform to schema: " + ajv.errorsText())
            }
        }
    }

The console log:

This code used to be working and it is how Ajv is used. From the Ajv docs:

The fastest validation call:

var Ajv = require('ajv');
var ajv = new Ajv(); // options can be passed, e.g. {allErrors: true}
var validate = ajv.compile(schema);
var valid = validate(data);
if (!valid) console.log(validate.errors);

How come I'm getting this error?

See the bottom of this for how I import the Ajv library - systemjs.config.js:

(function (global) {
    System.config({
        paths: {
            // paths serve as alias
            'npm:': 'lib/js/'
        },
        // map tells the System loader where to look for things
        map: {
            app: 'app', 
            // angular bundles
            '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
            '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
            '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
            '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
            '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
            '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
            '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
            '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
            // other libraries
            'rxjs': 'npm:rxjs',
            'angular2-in-memory-web-api': 'npm:angular2-in-memory-web-api',
            'angular2-google-maps/core': 'npm:angular2-google-maps/core/core.umd.js',
            'ajv': 'npm:ajv/dist/ajv.min.js',
            'primeng': 'npm:primeng'

解决方案

I saw that Ajv has a default function so I changed my code to this:

let ajv = new Ajv.default({ allErrors: true });

Not 100% sure what is going on there but it works.

这篇关于TypeError:Ajv不是构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆