![](/img/trans.png)
[英]Angular 2 include dependencies from node_modules into index.html
[英]Js file inside node_modules is not accessible to index.html
我正在使用模板 ,到目前為止已經完成了什么:
當我添加
<script src="/node_modules/stencil-poc-ng/dist/mycomponent.js"></script>
然后瀏覽器控制台出現以下錯誤:
GET http:// localhost:4200 / node_modules / stencil-poc-ng / dist / mycomponent.js 404(未找到)localhost /:1拒絕從' http:// localhost:4200 / node_modules / stencil-poc執行腳本-ng / dist / mycomponent.js ,因為其MIME類型('text / html')不可執行,並且啟用了嚴格的MIME類型檢查。
您正在構建Angular應用,在瀏覽器上運行該應用時將無法訪問計算機上的本地路徑。 Angular ng serve
是:
僅提供“內存中”文件,因此您的http://localhost:4200/node_modules/stencil-poc-ng/dist/mycomponent.js
將始終無效
您只剩下兩個選擇:
<script src='https://unpkg.com/stencil-poc-ng@0.0.1/dist/mycomponent.js'></script>
在Angular項目上有兩種使用模版組件的方法,它們是:
添加您的js文件的遠程路徑,例如: https ://unpkg.com/ {module-name} @ {module-version} / dist / {relevant-js-
將您的js文件添加到Assets文件夾,原因是angular找到了angular.json或angular.cli.json文件的資產數組中提到的資產。
感謝@YoukouleleY提出了第一個解決方案,並給出了我無法訪問第二個解決方案的正確原因
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.