使用CDN时打字工作

时间:2017-01-17 23:51:02

标签: typescript cdn typescript-typings

我想使用cdn访问json验证库,因为它应该更快一点,因为它从cdn中最近的可用服务器获取文件。

这是json验证库:

https://github.com/epoberezkin/ajv#using-in-browser

它指引我这个cnd:

https://cdnjs.com/libraries/ajv

所以我把它包含在我的html中:

<html>
<head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/ajv/4.10.4/ajv.min.js" integrity="sha256-LtA3VfycAam30/5e2Fq1f2tg8eIiFMOVWp1NDd6jmUU=" crossorigin="anonymous"></script>
</head>

现在的打字......我跑了这个:npm install --save-dev @ types / ajv并安装了

node_modules/@types/ajv/package.json:

{
  "_args": [
    [
      {
        "raw": "@types/ajv",
        "scope": "@types",
        "escapedName": "@types%2fajv",
        "name": "@types/ajv",
        "rawSpec": "",
        "spec": "latest",
        "type": "tag"
      },
      "C:\\Users\\si556577\\Documents\\SSWebApp\\app\\Iag.DI.Web.SupplierApp"
    ]
  ],
  "_from": "@types/ajv@latest",
  "_id": "@types/ajv@1.0.0",
  "_inCache": true,
  "_installable": true,
  "_location": "/@types/ajv",
  "_npmOperationalInternal": {
    "host": "packages-12-west.internal.npmjs.com",
    "tmp": "tmp/ajv-1.0.0.tgz_1482502603556_0.6872997884638608"
  },
  "_npmUser": {
    "name": "types",
    "email": "ts-npm-types@microsoft.com"
  },
  "_phantomChildren": {},
  "_requested": {
    "raw": "@types/ajv",
    "scope": "@types",
    "escapedName": "@types%2fajv",
    "name": "@types/ajv",
    "rawSpec": "",
    "spec": "latest",
    "type": "tag"
  },
  "_requiredBy": [
    "#DEV:/",
    "#USER"
  ],
  "_resolved": "https://registry.npmjs.org/@types/ajv/-/ajv-1.0.0.tgz",
  "_shasum": "4fb2440742f2f6c30e7fb0797b839fc6f696682a",
  "_shrinkwrap": null,
  "_spec": "@types/ajv",
  "_where": "C:\\Users\\si556577\\Documents\\SSWebApp\\app\\Iag.DI.Web.SupplierApp",
  "author": "",
  "bugs": {
    "url": "https://github.com/epoberezkin/ajv/issues"
  },
  "dependencies": {
    "ajv": "*"
  },
  "deprecated": "This is a stub types definition for ajv (https://github.com/epoberezkin/ajv). ajv provides its own type definitions, so you don't need @types/ajv installed!",
  "description": "Stub TypeScript definitions entry for ajv, which provides its own types definitions",
  "devDependencies": {},
  "directories": {},
  "dist": {
    "shasum": "4fb2440742f2f6c30e7fb0797b839fc6f696682a",
    "tarball": "https://registry.npmjs.org/@types/ajv/-/ajv-1.0.0.tgz"
  },
  "homepage": "https://github.com/epoberezkin/ajv#readme",
  "license": "MIT",
  "main": "",
  "maintainers": [
    {
      "name": "types",
      "email": "ts-npm-types@microsoft.com"
    }
  ],
  "name": "@types/ajv",
  "optionalDependencies": {},
  "readme": "ERROR: No README data found!",
  "repository": {
    "type": "git",
    "url": "git+https://github.com/epoberezkin/ajv.git"
  },
  "scripts": {},
  "typings": null,
  "version": "1.0.0"
}

它还将它添加到package.json:

"devDependencies": {
    "@types/ajv": "^1.0.0",

在代码中我使用它:

validateJSONSchema(json) {
    var ajv = new Ajv();
    var valid = ajv.validate(this.schema, json);
    if (!valid) {
        console.log(ajv.errors);
        return false;
    } else {
        return true;
    }
}

代码有效,但在vs代码中我得到了编译时错误:找不到名字&#34; Ajv&#34;

如何让我的打字工作?当我在本地安装软件包而不是使用cdn时,我只使用过类型。使用cdn时,打字机甚至可以工作吗?

1 个答案:

答案 0 :(得分:2)

ajv的{​​{1}}文件中注明package.json现已弃用,并且包含的​​内容包含自己:

  

这是ajv(https://github.com/epoberezkin/ajv)的存根类型定义。 ajv提供了自己的类型定义,因此您不需要安装@ types / ajv!

但是,包装本身包含的打字应该像这样使用:

@types/ajv

因此,除非使用import * as Ajv from "ajv"; let ajv = new Ajv(...); 语句,否则TypeScript对打字不满意。

这意味着如果要使用CDN,则必须配置模块捆绑器才能使用CDN。仅仅包括CDN import并且<script>可以作为全球使用,这还不够。

如何操作取决于您使用的捆绑包。