首页 > 解决方案 > Angular 12 将外部脚本加载为类型模块

问题描述

是否可以将 Angular 设置为加载angular.json下面architect/build/options/scripts定义的外部脚本type="module"

运行 Angular 时,我可以看到自动注入的脚本标签,如下所示:

<script src="runtime.js" type="module"></script>
<script src="polyfills.js" type="module"></script>
<script src="styles.js" type="module"></script>
<script src="scripts.js" defer></script>
<script src="vendor.js" type="module"></script>
<script src="main.js" type="module"></script>

scripts.js一个 defer 属性,但没有type="module"。我正在尝试将通过 npm 安装的 Stencil JS Web 组件加载到我的应用程序中。Stencil 组件脚本需要作为模块加载,否则其他导入将不起作用。

标签: javascriptangular

解决方案


我还可以确认目前无法向该字段添加type属性。scripts可能的值是:带有inject|lazy|bundleName|input属性的对象或直接是string

polyfills.ts为了在 Angular 中使用 WebComponent,我最终在文件中添加了一个导入语句

import 'node_modules/my-web-components/dist/example.js';

我能够看到正确导入的内容,并且我的 Web 组件在我的 Angular 组件 html 文件中处于活动状态。 此处添加的内容


推荐阅读