[英]How to roughly import .js libraries without type declaration in typescript?
I'm using Vue.js with cli and starting project on Typescript. 我将Vue.js与cli一起使用,并在Typescript上启动项目。 I didn't have any problem with it except of cases when I have to use js libraries.
除了必须使用js库的情况外,我没有任何问题。 I know about this https://github.com/DefinitelyTyped/DefinitelyTyped and I use it when possible.
我知道这个https://github.com/DefinitelyTyped/DefinitelyTyped,并在可能的情况下使用它。 But there is one big problem when js library is new, old or unpopular and has no type definitions.
但是,当js库是新的,旧的或不受欢迎且没有类型定义时,存在一个大问题。 I spend a lot of time, looking for solution and it seems like there is only few of options:
我花费大量时间寻找解决方案,似乎只有很少的选择:
In my case first option is unavailable. 在我的情况下,第一种选择不可用。 Second - very depends on library, which may have difficult structure to declare.
第二-非常依赖于库,库可能难以声明。 So, what I can do if I want just import js library as is?
因此,如果我只想按原样导入js库,该怎么办? I tried these ways:
我尝试了以下方法:
import { } from 'velocity-animate';
import Velocity from 'velocity-animate';
import { Velocity } from 'velocity-animate';
import Velocity from '../../node_modules/velocity-animate';
import * as Velocity from 'velocity-animate';
const Velocity = require('velocity-animate');
const Velocity: any = require('velocity-animate');
const Velocity = require('../../node_modules/velocity-animate/velocity.js');
Also, tried add different variations and combinations with imports above like: 另外,尝试使用上面的导入添加不同的变体和组合,例如:
declare var Velocity: any;
declare module 'velocity-animate';
etc. And nothing, I just have errors in console like: 等。什么都没有,我只是在控制台中有错误,例如:
Uncaught ReferenceError: Velocity is not defined
未被捕获的ReferenceError:未定义速度
It's not my typo in code, in regular js same code works fine ie https://codepen.io/anon/pen/YLBZMd 这不是我的错字,在常规js中,相同的代码也可以正常工作,即https://codepen.io/anon/pen/YLBZMd
Another my suspicion is tscompiler and his rules/settings. 另一个我怀疑的是tscompiler及其规则/设置。 Here is mine:
这是我的:
{
"compilerOptions": {
"target": "es2015",
"module": "es2015",
"strict": true,
"allowJs": true,
"jsx": "preserve",
"moduleResolution": "node",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"baseUrl": ".",
"noImplicitAny": false,
"types": [
"node",
"googlemaps"
],
"paths": {
"@/*": [
"src/*"
]
}
},
"include": [
"src/**/*.ts",
"src/**/*.vue",
"tests/**/*.ts"
],
"exclude": [
"node_modules"
]
}
Can someone explain me what I'm doing wrong? 有人可以解释我在做什么吗? Why so simple thing causes so big problems?
为什么这么简单的事情会引起如此大的问题?
Uncaught ReferenceError: Velocity is not defined
未被捕获的ReferenceError:未定义速度
This is a runtime error not a compile time typescript error . 这是运行时错误,而不是编译时打字稿错误 。
Make sure Velocity
global is available. 确保全局
Velocity
可用。 eg add : https://raw.githubusercontent.com/julianshapiro/velocity/master/velocity.min.js as a script
tag before your application code runs. 例如, 在运行应用程序代码之前,将https://raw.githubusercontent.com/julianshapiro/velocity/master/velocity.min.js添加为
script
标记。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.