8

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:

enter image description here

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'
4

2 回答 2

13

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.

于 2017-02-08T23:42:43.193 回答
0

旧问题和旧答案,但由于我认为接受的答案并不理想并且还留下未回答的问题,我仍然添加我的 $.02:

造成这种情况的原因是ajv使用export defaultorexport = 语法,而您正在使用import * aswhich 从模块中导入一个包含所有导出成员的对象ajv,其中默认导出是一个名为default.

导入默认构造函数最合理的方法是使用:

import Ajv from 'ajv';
const ajv = new Ajv(...);

而不是

import * as Ajv from 'ajv';
const ajv = new Ajv.default(...); // Ajv is an object containing _all_ exports from the ajv module

如果你绝对觉得你必须使用import *,那么这至少会更干净,Ajv而不是Ajv.default构造函数:

import * as AjvModule from 'ajv';
const {default: Ajv} = AjvModule;

如果使用require而不是import从使用 的模块访问导出的成员export default,它将表现得像import * as Ajv,即,您将获得一个具有default属性的对象。

所以以下是等价的:

// Pre-ES6 require
const Ajv = require('ajv').default;
const ajv = new Ajv(...);

// Import default
import Ajv from 'ajv';
const ajv = new Ajv(...);

// Import entire module and use default property
import * as Ajv from 'ajv';
const ajv = new Ajv.default(...); // just ugly!

// Import entire module as AjvModule and assign constructor function to Ajv
import * as AjvModule from 'ajv';
const {default: Ajv} = AjvModule;
const ajv = new Ajv(...);

如果您确实需要导入默认导出其他导出成员,则可以这样做而无需诉诸import * as

import Ajv, {EnumParams} from 'ajv';
const ajv = new Ajv(...);

我个人认为接受的答案有些缺陷,因为如果您只对导入 ajv 构造函数感兴趣,则将其分配给Ajv变量而不是包含构造函数作为称为属性的对象是有意义 的default- 然后创建类使用new Ajv.default语法 - 看起来很奇怪。

于 2021-02-25T10:33:54.373 回答