简体   繁体   English

在angular5中添加第三方(外部)js lib的问题

[英]Issue adding third-party (external ) js lib in angular5

I am working on to add jsplumb community js library version with the angular 5 application (Angular CLI: 1.6.1). 我正在使用angular 5应用程序(Angular CLI:1.6.1)添加jsplumb社区js库版本。

With the first build without any configuration to tsconfig.json I get the following error. 使用没有任何配置tsconfig.json的第一个生成我得到以下错误。

ERROR in src/app/jsplumb/jsplumb.component.ts(4,25): error TS6143: Module '../../../node_modules/jsplumb/dist/js/jsplumb.js' was resolved to 'D:/myproj/angular5/myapp/node_modules/jsplumb/dist/js/jsplumb.js', but '--allowJs' is not set.

With "allowJs":true in the tsconfig.json get the following error 使用“allowJs”:tsconfig.json中的true会出现以下错误

ERROR in error TS5055: Cannot write file 'D:/myproj/angular5/myapp/node_modules/jsplumb/dist/js/jsplumb.js' because it would overwrite input file.
  Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig.

As per tsconfig FAQ 根据tsconfig FAQ

Why am I getting the error TS5055: Cannot write file 'xxx.js' because it would overwrite input file. 为什么我收到错误TS5055:无法写入文件'xxx.js',因为它会覆盖输入文件。

with an outDir ("outDir": "./dist/out-tsc") this issue should be resolved. 使用outDir(“outDir”:“。/ dist / out-tsc”)此问题应该得到解决。 This is already set in my tsconfig. 这已在我的tsconfig中设置。

If we add noEmit to true it simply builds the application, not including any js we get a blank white screen. 如果我们将noEmit添加到true它只是构建应用程序,不包括任何js我们得到一个空白的白色屏幕。

Let me know if anyone has tried to include external js with new angular cli and face the same kind of error and what is the solution for the same. 让我知道是否有人试图用新的角度cli包含外部js并面临同样的错误,同样的解决方案是什么。

Without any additional option added to tsconfig.json if I try to modify any ts file the application will compile with success and I am able to work. 没有任何附加选项添加到tsconfig.json,如果我尝试修改任何ts文件,应用程序将成功编译,我能够工作。 But this does not help while running ng build, to create a deployable binary. 但是,在运行ng build时,这无助于创建可部署的二进制文件。

Update Workaround: until the fix in CLI is available. 更新解决方法:直到CLI中的修复程序可用。 For development (ng serve) remove allowJs from tsconfig.json, when ever you get an error with adding allowJs simply modify a ts file to recompile the applicaiton, this time will compile with success. 对于开发(ng serve),从tsconfig.json中删除allowJs,当你因添加allowJs而出错时只需修改一个ts文件来重新编译应用程序,这次将成功编译。 For production or distribution add back the allowJs to tsconfig.json run the application run with ng build --prod --watch=auto you should see the error about overriding the JS file in node_module, simple modify a ts file it should rebuild as --watch=auto is there in command but this time with sucess. 对于生产或分发,将allowJs添加回tsconfig.json运行应用程序运行ng build --prod --watch=auto你应该看到有关覆盖node_module中的JS文件的错误,简单修改它应该重建的ts文件 - -watch = auto在命令中,但这次是成功的。

If you're trying to import a library to Angular 2+, you should do it inside angular-cli.json . 如果您尝试将库导入Angular 2+,则应在angular-cli.json

There you have a scripts key where you should configure the desired imports. 你有一个scripts键,你应该配置所需的导入。 They are generally like: 他们通常喜欢:

{
    ...
    "scripts": [
        "../node_modules/MODULE/file.js"
    ],
    ...
}

Also you can import the library (not recommended) inside your main index.html , using <script src="/path_relative_to_root/file.js"> 您也可以使用<script src="/path_relative_to_root/file.js">在主index.html导入库(不推荐)

https://github.com/angular/angular-cli/wiki/angular-cli https://github.com/angular/angular-cli/wiki/angular-cli

https://github.com/angular/angular-cli/wiki/stories-global-scripts https://github.com/angular/angular-cli/wiki/stories-global-scripts

Angular Cli Webpack, How to add or bundle external js files? Angular Cli Webpack,如何添加或捆绑外部js文件?

have you tried upgrading it to angular 6? 你试过升级到角6吗? maybe they fixed this in the new version. 也许他们在新版本中解决了这个问题。

ng update @angular/cli

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

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