npm - UI5 Web 组件的 jsfiddle 代码示例,使用 unpkg.com 避免对 npm 的依赖
问题描述
unpkg.com书呆子的问题。
我想在jsfiddle上为UI5 Web Components创建一个代码示例。目标是在不强制读者安装相应的 NPM 模块及其依赖项的情况下共享代码示例。
对应的node包@ui5/webcomponents可以用npm安装:
npm install @ui5/webcomponents
然后我可以导入所需的 Web 组件:
import "@ui5/webcomponents/dist/Button"; // loads ui5-button
并在 HTML 页面中实例化自定义元素:
<ui5-button>Hello world!</ui5-button>
如何在 jsfiddle 上托管此代码示例,以便在那里显示“Hello world”按钮?
解决方案
您可以使用?module
参数(来自unpkg docs)实现此目的:
?module
Expands all “bare” import specifiers in JavaScript modules to unpkg URLs. This feature is *very experimental*
所以至少:
<script src="https://unpkg.com/@ui5/webcomponents/dist/Button?module" type="module"></script>
<ui5-button>Hello world!</ui5-button>
这是在jsfiddle上。
或者,您可以直接将其作为模块导入:
<ui5-button>Hello world!</ui5-button>
<script type="module">
import 'https://unpkg.com/@ui5/webcomponents/dist/Button?module';
</script>
请注意文档中的“非常实验性”限定词,所以我还不会完全依赖它!
推荐阅读
- jquery - 如何修复未捕获的类型错误:$image.cropper 不是函数
- python - 如何从数据库中的自定义表单中保存用户
- javascript - 数组中对象的属性
- docker - docker容器启动时是否可以将丢失的文件添加到挂载卷?
- c - 是否首选在 C 项目中使用全局 pthread_mutex_t?
- dataframe - 复制 stargazer 输出,如具有空间模型的线性模型
- performance-testing - Jmeter 对于每个控制器
- qliksense - 如何在 Qlik Sense 表达式中将数字转换为日期
- java - 它们之间有链接的实体列表
- reactjs - 避免 React 组件之间的共享自定义挂钩中的重复 API 调用