假设我有一个正常的反应应用程序,使用redux和一些ajax调用。 如果我想将它传递给别人我会给他们捆绑的js文件我用的WebPack创建,并要求他们将其纳入自己的HTML +呈现一个div与ID“对myApp”的例如:导入或需要由反应应用程序创建的捆绑js文件
<div id="myApp"></div>
好吧,如果他们的网站也创建了反应,他们想要包括我的捆绑js文件在他们的组件之一,当然提供相关的div
?
我试图用import
或require
模拟这个:
require('./path/to/myBundle.js');
import './path/to/myBundle.js';
例子:
//...
import './path/to/myBundle.js'; // the file that will render myApp to the relevant div
// ....
export function SomeApp(args){
return(
<div>
<div id="myApp"></div>
<SomeComponent />
</div>
);
};`
因为我得到了一些错误,这不起作用:
Uncaught Error: Minified React error #37; visit http://facebook.github.io/react/docs/error-decoder.html?invariant=37 for the full message or use the non-minified dev environment for full errors and additional helpful warnings.
和当我访问这个网站我看到:
_registerComponent(...): Target container is not a DOM element.
然而,它们是否会使用这个文件(myBundle.js)及其部件外(顶层index.html
例如),它会工作得很好,当然。
编辑: 我忘了提及,我想我知道问题是什么,应用程序没有HTML准备好这个div
呢。但我不知道一个好的原生方式来等待它的存在。
编辑#2以下@Frxstrem的回答: 我想要遵循这个答案,但我认为我做错了。 我有两份corry house slingshot demo app作为app1和app2。 改变了“产出”的APP1的webpack.config.prod.js
到:
output: {
path: path.resolve(__dirname, 'dist'),
publicPath: '/',
filename: 'app1Bundle.js',
library: "App1",
libraryTarget: "umd"
},
我试图使APP1 APP2的网页组件中。 所以我复制从APP1“已发布”文件APP2的根源,并呼吁该文件夹APP1,然后添加一个导入电话:
import {app1} from '../../app1/app1Bundle';
和返回函数内部匹配的标签:
const HomePage =() => {
return (
<div>
<app1 />
<h1>App 2</h1>
</div>
);
};
我收到上面发布的同样的错误。
我也尝试过不同的组合:
import app1 from '../../app1/app1Bundle'; // without curly braces
甚至刚刚起步的脚本作为一个正常的js脚本
import '../../app1/app1Bundle';
或 要求(” ../ ../ APP1/app1Bundle');
,然后试图呈现一个正常的div标签“APP1”的ID
const HomePage =() => {
return (
<div>
<div id="app1"></div>
<h1>App 2</h1>
</div>
);
};
似乎没有任何工作,因为我仍然得到同样的错误。 我认为问题是脚本加载的时间和元素的渲染。当捆绑脚本正在搜索时,我认为div还不存在。
我似乎得到相同的错误。你能否详细说明我应该在代码中更改哪些内容? –