我正在尝试在 JSBin 中使用 ES6 创建一个 React 组件,但我一直收到以下错误。有人可以解释一下吗?
JSBin 错误:“ReferenceError:未定义要求 在 mudaza.js:17:14"
最佳答案
您不能在 JSBin 中使用导入或要求。您需要在 html 文件的 head
中包含要在您的应用程序中使用的库的捆绑 .js 文件,然后使用稍微不同的语法从添加的库中访问组件或函数。
例如,要在笔中使用 react-router
,您应该将其添加到托管应用程序的 HTML 页面的 head
中:
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-router/2.0.0/ReactRouter.min.js"></script>
然后,在您应用的源代码中:
var { Router, Route, IndexRoute, hashHistory, Link } = ReactRouter;
代替
import { Router, Route, IndexRoute, hashHistory, Link } from "react-router";
我已经克隆并更新了你的 JSBin with these changes .
大多数模块 .js 文件的一个很好的来源是 https://cdnjs.com/ .
关于javascript - "ReferenceError: require is not defined",在使用 ES6/Babel 创建 React 组件时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35871311/