Javascript 如何修复require.确保错误网页,Babel6,反应?
我正在尝试使用require.sure为Facility.js创建块Javascript 如何修复require.确保错误网页,Babel6,反应?,javascript,reactjs,webpack,babeljs,Javascript,Reactjs,Webpack,Babeljs,我正在尝试使用require.sure为Facility.js创建块 import React, { Component } from 'react'; import { render } from 'react-dom'; class Facility extends Component { constructor(...args) { super(...args); this.state = { .... }
import React, { Component } from 'react';
import { render } from 'react-dom';
class Facility extends Component {
constructor(...args) {
super(...args);
this.state = {
....
};
}
render() {
return (
<div>
....
</div>
);
}
}
export default Facility;
//Tried module.exports = Facility;
Dev
我可以在我的源代码中看到facility.bundle.js,但是对它的调用返回未定义的错误
警告:React.createElement:类型不应为null,未定义,
布尔值,或数字。它应该是字符串(对于DOM元素)或
ReactClass(用于复合组件)。检查的渲染方法
非患者
及
未捕获不变冲突:元素类型无效:应为
字符串(用于内置组件)或类/函数(用于复合
组件)但得到:未定义。检查的渲染方法
非患者
生产(运行构建)
不允许加载本地资源:
file:///C:/Users/.../facility.bundle.js
网页包配置
output: {
path: path.join(__dirname, 'dist'),
filename: 'bundle.js',
publicPath: __dirname + '/',
chunkFilename: '[name].bundle.js'
},
首先,您需要修复webpack.config中的publicPath属性。你应使用:
publicPath: '/dist/'
而是绝对路径
第二,如果导出模块使用es6语法:
export default Facility;
您应该这样编写导入代码:
require.ensure([], function(require) {
Facility = require('./Facility').default;
ReactDOM.render(<Facility/>, document.getElementById('app'));
}, 'facility');
然后
require.ensure([], function(require) {
Facility = require('./Facility');
ReactDOM.render(<Facility/>, document.getElementById('app'));
}, 'facility');
此外,您还可以使用下一个技巧:
var bundleSrc = document.querySelector('[src*="bundle"]').getAttribute("src");
__webpack_public_path__ = bundleSrc.substr(0, bundleSrc.lastIndexOf("/") + 1);
您是如何使用非患者的
的?请注意,require。确保
是异步的,因此预计设施
最初将是未定义的
。如果您的代码没有考虑到这一点,它很可能会以您描述的方式失败。@bebraw NonPatientSer包含在主bundle.js文件中,并在应用程序启动时加载(我不确定您的确切意思)。这正是我想要实现的,这个文件将作为一个单独的文件包含在应用程序中。在网页文档中,我发现此[/link]无效,抱歉。我必须使用require.conference来创建应用程序块,这不仅要求importhanks正常工作,而且只出现了一个问题…bundle.js?ver=1.11:1不允许加载本地资源:file:///C:/Users/.../facility.bundle.js
当我尝试重新加载页面时,如果它从链接开始,则工作正常。componentDidMount(){var Facility;require.sure([],function(require){Facility=require('./Facility').default;render(,document.getElementById('fl');},'Facility');}render(){return(
修复了publicPath属性吗?是否使用任何服务器(例如webpack dev server)?是的。我想我需要运行时\uu webpack\uPublic\uPath\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu?
require.ensure([], function(require) {
Facility = require('./Facility');
ReactDOM.render(<Facility/>, document.getElementById('app'));
}, 'facility');
__webpack_public_path__ = '/publicPathToChunk/';
var chunk;
require.ensure([], function(require) {
chunk = require('./chunk');
}, 'chunk');
var bundleSrc = document.querySelector('[src*="bundle"]').getAttribute("src");
__webpack_public_path__ = bundleSrc.substr(0, bundleSrc.lastIndexOf("/") + 1);