【发布时间】:2016-04-04 04:52:04
【问题描述】:
我尝试使用 require.ensure 为 Facility.js 创建一个块
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;
NonPatientSer.js
var Facility;
require.ensure([], function(require) { Facility = require('./Facility'); }, 'facility');
开发 我可以在我的源代码中看到 facility.bundle.js 但对其的调用返回 undefined 并且此错误
警告:React.createElement:类型不应为 null、未定义、 布尔值或数字。它应该是一个字符串(对于 DOM 元素)或 ReactClass(用于复合组件)。检查渲染方法
NonPatientSer
和
未捕获的不变违规:元素类型无效:预期为 字符串(用于内置组件)或类/函数(用于复合 组件)但得到:未定义。检查渲染方法
NonPatientSer.
生产(运行构建)
不允许加载本地资源: file:///C:/Users/.../facility.bundle.js
Webpack 配置
output: {
path: path.join(__dirname, 'dist'),
filename: 'bundle.js',
publicPath: __dirname + '/',
chunkFilename: '[name].bundle.js'
},
【问题讨论】:
-
您如何使用
NonPatientSer?请注意,require.ensure是异步的,因此预计Facility最初将是undefined。如果您的代码没有考虑到这一点,它很可能会按照您描述的方式失败。 -
@bebraw NonPatientSer 包含在主 bundle.js 文件中,并在应用程序启动时加载(我不确定您的确切含义)。正是我想要实现的,该文件将作为单独的文件包含在应用程序中。在 Webpack 文档中,我发现了这个 link[/link]
标签: javascript reactjs webpack babeljs