Javascript 如何将ReactJS应用程序导出为包,以便在其他应用程序中使用?
我正在使用webpack和babel构建我的应用程序,应用程序正在bundle.js文件中成功构建 我有一个index.jsx文件,其中包含以下代码Javascript 如何将ReactJS应用程序导出为包,以便在其他应用程序中使用?,javascript,reactjs,webpack,babeljs,Javascript,Reactjs,Webpack,Babeljs,我正在使用webpack和babel构建我的应用程序,应用程序正在bundle.js文件中成功构建 我有一个index.jsx文件,其中包含以下代码 import React from 'react'; import { render } from 'react-dom'; import App from './App'; render(<App />, document.getElementById('app')); 从“React”导入React; 从'react dom'导
import React from 'react';
import { render } from 'react-dom';
import App from './App';
render(<App />, document.getElementById('app'));
从“React”导入React;
从'react dom'导入{render};
从“./App”导入应用程序;
render(显示他如何使用它的链接)
他使用的地方
export const init = (config) => {
ReactDOM.render(<MyReactApp config={config} />, someSelector);
}
export const init=(配置)=>{
render(,someSelector);
}
我如何转换我的index.jsx文件,以便可以在另一个应用程序中使用,就像在给定链接的答案中使用一样。就像他在另一个答案中所说的那样
<script src="./bundle.js" type="text/javascript"></script>
<script type="text/javascript">
MyApp.yourExportedFunction();
</script>
使用UMD库:我已将其作为输出添加到webpack.config.js文件中:{path:BUILD_DIR,filename:'bundle.js',library:'MyApp',libraryTarget:'UMD',umdNamedDefine:true,},它可以工作,但我共享了链接,因为我想修改index.jsx或html文件以接收一些参数,从我想使用的位置发送。就像在link中一样,您必须像他那样导出进行渲染的函数,并将参数传递给该函数,如他的示例所示。
<script src="./bundle.js" type="text/javascript"></script>
<script type="text/javascript">
MyApp.yourExportedFunction();
</script>
<div id="app"></div>
<script src=".path/to/other/project/bundle.js" type="text/javascript"></script>
render(<App />, document.getElementById('app'));