Ben*_*ins 5 javascript typescript systemjs ajv angular
我有这个类,我尝试使用 new 关键字实例化Ajv,但出现此错误:
类型错误:Ajv 不是构造函数
代码:
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())
}
}
}
Run Code Online (Sandbox Code Playgroud)
控制台日志:
这段代码曾经有效,这就是 Ajv 的使用方式。从 Ajv 文档:
最快的验证调用:
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);
Run Code Online (Sandbox Code Playgroud)
我怎么会收到这个错误?
有关我如何导入 Ajv 库的信息,请参阅本文底部 - 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'
Run Code Online (Sandbox Code Playgroud)
Ben*_*ins 11
我看到 Ajv 有一个默认功能,所以我将代码更改为:
let ajv = new Ajv.default({ allErrors: true });
Run Code Online (Sandbox Code Playgroud)
不是 100% 确定那里发生了什么,但它有效。
| 归档时间: |
|
| 查看次数: |
4445 次 |
| 最近记录: |