简体   繁体   English

从CDN加载的ES6模块导入TS类型用于JS

[英]Importing TS typings with ES6 modules for JS loaded from CDN

Using: 使用:

Typescript with ES6 output (target and modules) 带ES6输出的打字稿(目标和模块)

Vue loaded from CDN 从CDN加载的Vue

TS typings of Vue installed by NPM NPM安装的Vue的TS类型

I'm trying to get types working in source .ts files and still use Vue loaded by the CDN. 我正在尝试使类型在源.ts文件中工作,并且仍然使用CDN加载的Vue。 Vue from the CDN isn't a module (and that wouldn't help either way; as I understand it, it has to be hosted by me to be importable in ES6). CDN中的Vue并不是一个模块(这两种方式都无济于事;据我所知,它必须由我托管才能在ES6中导入)。

The types as installed by NPM need to be imported import Vue from 'vue' , which comes up in the generated Javascript (that's in addition to the difference in Vue being global in the generated js as a non-module) and can't be resolved because it's not local on the server. 由NPM安装的类型需要import Vue from 'vue' ,这是在生成的Javascript中出现的(这是Vue在生成的js中作为非模块是全局的之外的区别),并且不能已解决,因为它不在服务器上本地。

I think you just need to add Vue as external lib in webpack config - your typings will work correctly and webpack will resolve dependency from global variable(window). 我认为您只需要在Webpack配置中将Vue添加为外部库-您的键入将正确运行,并且Webpack将解决全局变量(窗口)的依赖性。

//...
externals: {
  vue: "Vue"
}
//...

So you can use import in your code for external library - import Vue from 'vue' . 因此,您可以在代码中使用import外部库- import Vue from 'vue'

See Webpack Externals . 请参阅外部Webpack

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM