将JS IIFE库导出到React Component

时间:2019-02-23 07:57:29

标签: javascript html reactjs import export

我才刚刚开始学习React JS。所以我有这2个JS文件:

  1. Polyfill.js-> github
  2. 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一样工作,但是我不知道该如何做。请帮忙!

1 个答案:

答案 0 :(得分:0)

import "./customnavbar.js"

对于模块导入而言不是有效的语法。

type元素中的<script>设置为"module"

<script src="./polyfill.js" type="module">

return来自IIFE的classy对象

“ script.js”

export default (() => { const classy = {fn() { return 1 }}; return classy })();

import导出的`经典对象

“ polyfill.js”

import classy from "./script.js";
// do stuff

plnkr http://plnkr.co/edit/Q0orq8Bvk6nOT8tX0qLg?p=preview