简体   繁体   English

在 angular nx 项目中导入 JavaScript 模块

[英]import JavaScript module in angular nx project

I have an angular project built with nx I want to import standard JavaScript libraries which do not have typescript typings for example I want to install and use cytoscape and cytoscape-context-menus我有一个用nx构建的 angular 项目我想导入没有打字稿类型的标准 JavaScript 库,例如我想安装和使用cytoscapecytoscape-context-menus

I get compilation errors such as我收到编译错误,例如

error TS7016: Could not find a declaration file for module 'cytoscape-context-menus'.错误 TS7016:找不到模块“cytoscape-context-menus”的声明文件。 'C:/dev/armo/armo-ng-app/node_modules/cytoscape-context-menus/cytoscape-context-menus.js' implicitly has an 'any' type. 'C:/dev/armo/armo-ng-app/node_modules/cytoscape-context-menus/cytoscape-context-menus.js' 隐含地有一个 'any' 类型。 Try npm i --save-dev @types/cytoscape-context-menus if it exists or add a new declaration (.d.ts) file containing declare module 'cytoscape-context-menus';尝试npm i --save-dev @types/cytoscape-context-menus如果它存在或添加一个包含declare module 'cytoscape-context-menus';的新声明 (.d.ts) 文件declare module 'cytoscape-context-menus';

How can I solve them?我该如何解决它们? I tried to put declaration files but I think my setup doesn't see them.我试图放置声明文件,但我认为我的设置没有看到它们。

since there's no available type definition npm package, you have to write your own.由于没有可用的类型定义 npm 包,您必须自己编写。 start with the ones you need and continue to build on top of it as needed.从您需要的开始,并根据需要继续在其基础上构建。

if you like you can share it too as an npm package.如果您愿意,也可以将其作为 npm 包共享。

found this article for writing TypeScript type definition files by @dubtypescript: http://blog.wolksoftware.com/contributing-to-definitelytyped通过@dubtypescript 找到了这篇关于编写 TypeScript 类型定义文件的文章: http ://blog.wolksoftware.com/contributing-to-definelytyped

also take a look at how cytoscope is typed from here, borrow their ways: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/cytoscape/index.d.ts也看看细胞镜是如何从这里输入的,借用他们的方式: https : //github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/cytoscape/index.d.ts

Added a stackblitz to checkout:添加了一个 stackblitz 到结帐:

https://stackblitz.com/edit/cytoscape-6cyuem?file=src%2Fapp%2Fcytoscape-context-menus.d.ts https://stackblitz.com/edit/cytoscape-6cyuem?file=src%2Fapp%2Fcytoscape-context-menus.d.ts

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

相关问题 Angular v13 带 nx 测试的玩笑 - 语法错误:无法在 Runtime.createScriptFromCode 的模块外使用导入语句 - Angular v13 Jest with nx test - SyntaxError: Cannot use import statement outside a module at Runtime.createScriptFromCode 如何将Javascript文件导入Ionic / Angular项目 - How to import Javascript file into Ionic/Angular project 在另一个 javascript 项目中导入 2 个 Angular 项目 - Import 2 Angular projects in another javascript project 在Angular项目中导入2个JavaScript同名函数 - Import 2 JavaScript functions with the same name in Angular project 在现有 Nx 项目上创建 React 应用程序时找不到模块“nx/src/config/workspaces” - Cannot find module 'nx/src/config/workspaces' on creating a React app on existing Nx project 使用Handsontable的Javascript项目在导入时找不到模块 - Javascript project using Handsontable cannot find module on import 如何在 JavaScript/NPM 的 monorepo 项目中导入单个模块 - How do I import an individual module in a monorepo project in JavaScript/NPM 如何将第 3 方 javascript 模块“导入”到我的反应项目中? - How to “import” a 3rd party javascript module to my react project? Angular 5-将模块导入动态模块 - Angular 5 - Import Module into Dynamic Module 如何在Angular中导入模块? - How to import module in Angular?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM