簡體   English   中英

可以將 crossorigin 屬性添加到由 angular cli 生成的腳本標簽嗎?

[英]possible to add crossorigin attribute to script tags generated by angular cli?

是否可以將 crossorigin 屬性添加到由 angular cli 生成的腳本標簽?

運行我的 angular 應用程序時,腳本標簽被添加到我的 index.html 的末尾:

<script src="runtime-es2015.3d05cbd29d24231258bf.js" type="module"></script>
<script src="polyfills-es2015.28da6787754ec8436843.js" type="module"></script>
<script src="main-es2015.4106b7f4d43a05cb792d.js" type="module"></script>

是否可以配置 angular-cli,以便在構建 index.html 中包含這些標簽時,將 crossorigin 屬性添加到腳本聲明中:

<script src="runtime-es2015.3d05cbd29d24231258bf.js" type="module" crossorigin="use-credentials"></script>

我為什么要問這個? 我的應用程序部署到使用基本身份驗證的 apache 服務器。 使用 Firefox 或 Edge(chrome 很好)時,對 javascript 模塊的請求會收到 401 錯誤,因為未設置 Authorization 標頭。 如果添加了 crossorigin 標記,則設置 Authorization 標頭。 因此,如果我希望我的用戶能夠使用 FF 或 Edge,我需要能夠添加該 crossorigin 屬性。

感謝您閱讀我的問題。

從 angular/cli 8.1 ( PR ) 開始,可以設置一個標志來更改 index.html 中的腳本標簽(以及與此相關的鏈接標簽)

--crossOrigin=none|匿名|使用憑證
定義提供 CORS 支持的元素的 crossorigin 屬性設置。 默認值:無https://angular.io/cli/build

另一種選擇是使用 post build npm 步驟來解包 index.html 並根據各種需要對其進行更改

對於服務器端的這些操作, cheeriojs想到了(查看此示例以獲取更多詳細信息)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM