我正在使用angular 5应用程序(Angular CLI:1.6.1)添加jsplumb社区js库版本 .
使用没有任何配置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.
使用“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.
根据tsconfig FAQ
Why am I getting the error TS5055: Cannot write file 'xxx.js' because it would overwrite input file.
使用outDir(“outDir”:“ . / dist / out-tsc”)此问题应该得到解决 . 这已在我的tsconfig中设置 .
如果我们将noEmit添加到true它只是构建应用程序,不包括任何js我们得到一个空白的白色屏幕 .
让我知道是否有人试图用新的角度cli包含外部js并面临同样的错误,同样的解决方案是什么 .
没有任何附加选项添加到tsconfig.json,如果我尝试修改任何ts文件,应用程序将成功编译,我能够工作 . 但是,在运行ng build时,这无助于创建可部署的二进制文件 .
Update Workaround: 直到CLI中的修复程序可用 . 对于开发(ng serve),从tsconfig.json中删除allowJs,当你因添加allowJs而出错时只需修改一个ts文件来重新编译应用程序,这次将成功编译 . 对于 生产环境 或分发,将allowJs添加回tsconfig.json运行应用程序运行 ng build --prod --watch=auto
你应该看到有关覆盖node_module中的JS文件的错误,简单修改它应该重建的ts文件,因为--watch = auto在命令中但是这次是成功的 .
2 回答
如果您尝试将库导入Angular 2,则应在
angular-cli.json
中进行 .你有一个
scripts
键,你应该配置所需的导入 . 他们通常喜欢:您也可以使用
<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/stories-global-scripts
Angular Cli Webpack, How to add or bundle external js files?
你试过升级到角6吗?也许他们在新版本中解决了这个问题 .