且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将jQuery插件导入Angular 2+拒绝执行脚本,因为其MIME类型('text / html')不可执行

更新时间:2021-07-24 21:41:33

尝试将js脚本放入 src / assets 文件夹。然后它应该由开发服务器提供正确的mime类型

Try putting your js script in the src/assets folder. Then it should be served with correct mime type by the development server

然后像下面引用它

<script src="/assets/scripts/test.js" type="text/javascript"></script>

您可以将文件添加到脚本中,而不是在index.html中包含jquery和您的脚本的属性angular-cli.json (或 angular.json 如果使用角度6)

Rather than including jquery and your script in index.html, you could add the files to the "scripts" properties of angular-cli.json (or angular.json if using angular 6)

"scripts":
 [
      //include jQuery here, e.g. if installed with npm
      "../node_modules/jquery/dist/jquery.min.js" ,
      "assets/scripts/test.js"
 ]

注意:别忘了重启 ng服务修改 .angular-cli.json angular.json files

Note: Don't forget to restart ng serve after modifying the .angular-cli.json or angular.json files