在打字稿中导入JavaScript库时出错 [英] Error import javascript library in typescript

查看:114
本文介绍了在打字稿中导入JavaScript库时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要导入javascript库.那是阿卡拉塔图书馆.我像这样在互联网上的建议中导入:

I want import javascript library. that is akarata library. I import like in suggestion in internet like this:

import * as akarata from 'akarata/dist';

import * as akarata from 'akarata';

仍然收到这样的错误

尝试npm install @ types/akarata(如果存在)或添加新的 包含声明模块'akarata'的声明(.d.ts)文件;

Try npm install @types/akarata if it exists or add a new declaration (.d.ts) file containing declare module 'akarata';

但是我尝试了没事.

,很奇怪.刚开始我键入ng serve,因为我使用angular,出现错误.然后,我对项目进行了一些更改,然后将其保存.我仍然遇到错误,但是我的项目可行.图书馆也很好.

and the weird is. just first I type ng serve , because I use angular, the error appear. and after I make a bit change of my project and then I save it. I still get error but my project works. The library works well too.

有人知道为什么会这样吗?

Is anyone know why it like that?

推荐答案

TypeScript是JavaScript的类型化超集,可编译为纯JavaScript. TypeScript具有自己的语法,函数,并且变量可以具有定义的类型,如果要使用外部库(例如akarata),则需要声明TypeScript的类型定义.有些库包含键入文件,您无需为其安装TypeScript的类型目标.但是如果库没有.d.ts文件,则需要安装它. 类型搜索

TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. TypeScript has its own syntax, function, and variables can have defined types, if you want to use an external library such as akarata you need to declare type definitions for TypeScript. Some libraries include typing file and you don’t need to install TypeScript’s type destination for them. But in case a library does not have .d.ts file, you need to install it.Type Search

因为您的库在TypeScript和Angular中没有类型定义(* .d.ts)

since your library does not have type definition (*.d.ts) in TypeScript and Angular

解决方案:
如果src/typings.d.ts不存在,请创建,否则,请打开它,然后将包添加到其中:

Solution:
Create if the src/typings.d.ts does not exist, otherwise, open it, and add your package to it:

declare module 'akarata'

并导入

import * as akarata from 'akarata';

这篇关于在打字稿中导入JavaScript库时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆