简体   繁体   English

如何在 TypeScript 中导入 Vanilla JS ES6 模块?

[英]How to import Vanilla JS ES6 modules in TypeScript?

I'm trying to import this library: spotlight , which is plain JS.我正在尝试导入这个库: spotlight ,它是纯 JS。

In my project I created a fake d.ts :在我的项目中,我创建了一个假d.ts

declare module 'spotlight.js'

as suggested by VSCode , then I tried every single import combination:按照VSCode 的建议,然后我尝试了每个导入组合:

import * as spotlight from 'spotlight';
import spotlight from 'spotlight';
import { spotlight } from 'spotlight';

but inside my code the spotlight expression is always an empty object.但在我的代码中, spotlight表达式始终是一个空对象。

What am I missing?我错过了什么?

有一个悬而未决的问题: https : //github.com/nextapps-de/spotlight/issues/6

As @Shinigami pointed out - there is an open issue.正如@Shinigami 指出的那样 - 有一个未解决的问题。 As I see it, right now, there is no elegant way to import the Spotlight object without modifications to the library (it also seems that npm run build doesn't help much).正如我所看到的,现在,没有不修改库的情况下import Spotlight对象的优雅方法(似乎npm run build也没有多大帮助)。 But, judging by the code, the Spotlight is accessible through window .但是,从代码来看, Spotlight可以通过window访问。 So it should be possible to use it like so所以应该可以像这样使用它

import "spotlight"
window.Spotlight.show(...)

声明语句最初必须在一个单独的文件中,还要确保为导入语句提供了正确的路径。

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

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