简体   繁体   English

在 Angular 7 中工作但不在 8 中的外部 JS 文件

[英]External JS Files Working in Angular 7 But Not in 8

I'm trying to run these viewstl javascript files in an Angular 8 application.我正在尝试在 Angular 8 应用程序中运行这些 viewstl javascript 文件。 I've run it successfully in an Angular 6 and 7 application by doing the following basics:通过执行以下基础操作,我已经在 Angular 6 和 7 应用程序中成功运行了它:

  • Copied the .js files to the application, keeping the web worker files ( load_stl.min.js and parser.min.js ) in the root directory and the rest of the files in assets/scripts .将 .js 文件复制到应用程序,将 web worker 文件( load_stl.min.jsparser.min.js )保存在根目录中,其余文件保存在assets/scripts

  • Add the .js files to my scripts array in angular.json in this working order按此工作顺序将 .js 文件添加到 angular.json 中的我的脚本数组中

    "scripts": [ "src/assets/scripts/three.min.js", "src/assets/scripts/webgl_detector.js", "src/assets/scripts/Projector.js", "src/assets/scripts/CanvasRenderer.js", "src/assets/scripts/OrbitControls.js", "src/assets/scripts/stl_viewer.min.js" ]
  • Added the necessary code to the template:在模板中添加了必要的代码:

    html <div id="stl_container"></div>

  • Lastly referencing stl_viewer.min.js from the component:最后从组件中引用stl_viewer.min.js

     declare var StlViewer:any; var stl_viewer = new StlViewer(document.getElementById('stl_container'), { models: [ {id:0, filename:"assets/_somefile.stl"} ] });

As I mentioned, it works fine in Angular 6 but when running from Angular 8 I get the error as soon as the javascript is initialized from the component - and I don't understand why.正如我所提到的,它在 Angular 6 中运行良好,但是从 Angular 8 运行时,一旦从组件初始化 javascript,我就会收到错误 - 我不明白为什么。

GET http://localhost:4300/load_stl.min.js 404 (Not Found) GET http://localhost:4300/load_stl.min.js 404(未找到)

I'm not necessarily sure if the version difference could be the problem我不确定版本差异是否可能是问题所在

It turns out there is an extra property on the StlViewer class called load_three_files which takes the path to all the javascript files as argument.事实证明,StlViewer 类中有一个名为load_three_files的额外属性,它将所有 javascript 文件的路径作为参数。

Moving parser.min and load_stl.min to the assets/script folder and adding load_three_files: "assets/scripts/" to StlViewer fixed it.parser.minload_stl.min移动到 assets/script 文件夹并将load_three_files: "assets/scripts/"到 StlViewer 修复了它。

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

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