Ben*_*ins 3 import require typescript
我正在尝试使用Ajv(我正在使用打字稿).
import { Ajv } from "ajv";
let ajv = new Ajv({allErrors: true});
Run Code Online (Sandbox Code Playgroud)
只是想知道它为什么会出现这个错误:
[ts]'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)
但我使用import而不是require,所以我没有设置变量,所以我猜这是导致错误.使用时如何定义变量用于import代替require?
所有代码:
import { Injectable } from "@angular/core";
import { Http, Headers, Response } from "@angular/http";
import { Configuration } from "../shared/configuration.model";
import { Observable } from "rxjs/Rx";
import { Ajv } from "ajv";
@Injectable()
export class ValidateJSONSchemaService {
constructor(private http: Http) { }
public valJson(json, schemaFile: string) {
return new Promise((resolve, reject) => {
this.http.get(schemaFile)
.toPromise()
.then(fileContents => fileContents.json())
.then((schema) => {
let ajv = new Ajv({allErrors: true});
ajv.validate(schema, json) ?
resolve() :
reject(new Error("JSON does not conform to schema: " + ajv.errorsText()));
}, err => reject(
new Error("Unable to get schema file contents:" + err))
);
});
};
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3231 次 |
| 最近记录: |