javascript - 将 JS IIFE 库导出到 React 组件
问题描述
我刚开始学习 React JS。所以我有这2个JS文件:
- Polyfill.js -> github
- CustomNavbar.js -> 我的
polyfill.js 结构
export default (function(window){
...
var classy = {
...
}
...
})(window);
!window.addEventListener && window.Element && (function () {
...
---code---
...
})();
这是 customnavbar.js
import { cs } from "./polyfill";
(function(){
...
function openNav(){
...
cs.classy.add(overlay, 'on-overlay');
...
}
...
})();
这是我的 component.jsx
import "./customnavbar.js"
...
...
它没有用,错误说cs没有定义。也许我在导出语法上做错了?
<script src="./polyfill.js">
<script src="./customnavbar.js">
^ 我希望它像纯 HTML 一样工作,但我不知道如何做出反应。请帮忙!
解决方案
import "./customnavbar.js"
不是模块导入的有效语法。
元素集type
_<script>
"module"
<script src="./polyfill.js" type="module">
return
classy
来自 IIFE的对象
“脚本.js”
export default (() => { const classy = {fn() { return 1 }}; return classy })();
import
导出的 `classy 对象
“polyfill.js”
import classy from "./script.js";
// do stuff
推荐阅读
- java - 程序似乎忽略了 synchronized() 行
- php - 加入足球/足球网站的表格和数据
- graphql-js - 不能使用重复的“graphql”模块
- ubuntu-18.04 - Ubuntu 磁盘空间
- javascript - 在数据表中的图像下方添加页面标题
- asp.net-core - web api asp core : 未声明纯文本文档的字符编码
- python - 如何检查输入是否在矩阵的第一列内
- sockets - 我需要 2 个插座来进行此设置还是一个就可以了?
- jetty - 如何在 kairosdb 中使用 Jetty 配置拒绝来自客户端的请求
- mingw - 使用 CMake 启用对 CLion 的 openmp 支持