javascript - 在 html 文件中包含 Reactjs Bundled 组件
问题描述
我有一个reactjs npm 包,我想将它包含在 html 文件中,我在这里得到了捆绑的文件,但我遇到了一个require
不是函数的问题,因为我不在 nodejs 项目中。所以我无法导入其他依赖项,有人可以进一步帮助我吗?
console.log(window.React, 'AAB');
window.ReactDOM.render(window.React.createElement(window.ListItem, {
labelOption: 'name',
typeOption: 'type',
valueOption: 'k',
onRemoveItem: (e) => {console.log('1')},
item: {name: 'Zeyad', type: 'person'}
}, 'Hi'), document.getElementById("app"));
<!DOCTYPE html>
<html>
<head>
<title>Parcel Sandbox</title>
<meta charset="UTF-8" />
<link href="https://unpkg.com/react-multiple-selector@1.0.7/dist/styles.css">
</head>
<body>
Name:
<div id="app"></div>
<script>
var exports = {};
</script>
<script
crossorigin
src="https://unpkg.com/react@16/umd/react.production.min.js"></script>
<script
crossorigin
src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"
></script>
<script src="https://unpkg.com/react-multiple-selector@1.0.7/dist/index.js"></script>
<script src="https://unpkg.com/react-multiple-selector@1.0.7/dist/ListItem.js"></script>
<script src="https://unpkg.com/react-multiple-selector@1.0.7/dist/data.js"></script>
<script src="https://gist.githubusercontent.com/gaearon/0b180827c190fe4fd98b4c7f570ea4a8/raw/b9157ce933c79a4559d2aa9ff3372668cce48de7/LikeButton.js"></script>
</body>
</html>
解决方案
If you want to create a simple reactjs application, you can use umd and an example is available from the react website itself - here
However, not all modules that are built for reactjs support UMD, and you will have to use react in a bundled fashion. Read the official documentation and more. react-multiple-selector looks like it does not support UMD yet.
The inspector console shows that the error is from https://unpkg.com/react-multiple-selector@1.0.7/dist/index.js
and https://unpkg.com/react-multiple-selector@1.0.7/dist/ListItem.js
While searching for an option for require
in a browser, I came across this SO. However, know that you are trying to hack your way into doing this, and is only a hack not a solution.
推荐阅读
- ruby-on-rails - 定义一个嵌套的 has_many 和 belongs_to
- amazon-web-services - 无法创建 aws ec2 证书。正文不得包含私钥
- vba - 功能区 onLoad 子中的 ActiveDocument 错误 4248
- azure - 以特定用户身份在系统启动时运行批处理脚本,即使该用户未登录
- javascript - 调整窗口大小时如何触发功能?
- excel - 如何为B列中的56个单元格着色
- concurrency - C++Amp 在构建/执行之前是否需要 GPU 硬件?
- google-chrome-extension - 如何在我的 chrome 浏览器中获取当前 TAB 的 URL 和标题,并在用户单击我的扩展程序图标时显示它
- osgi - SCR 到 OSGi R6 注释问题
- r - 插值数据:如何在不同组的一个数据集中插入数据,并将其应用于具有相同组的另一个数据集?