我正在尝试抽象我的代码,并且我在以下结构中有以下两个文件
main.js
components
- parent.js
main.js
require('./components/Parent');
ReactDOM.render(
<Parent />,
document.getElementById('content')
);
组件/Parent.js
var Parent = React.createClass({
displayName: 'Parent',
render: function(){
return (
<div>
<div> This is the parent page. </div>
</div>
)
}
});
索引.html
<div id="app">
</div>
我运行以下 -
browserify -t reactify main.js -o main_js.js
这是它创建的 javascript
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
require('./components/Parent');
ReactDOM.render(
React.createElement(Parent, null),
document.getElementById('content')
);
},{"./components/Parent":2}],2:[function(require,module,exports){
var Parent = React.createClass({
displayName: 'Parent',
render: function(){
return (
React.createElement("div", null,
React.createElement("div", null, " This is the parent page. ")
)
)
}
});
},{}]},{},[1]);
运行页面在第 5 行抱怨React.createElement(Parent, null),
Uncaught ReferenceError: Parent is not defined
但是正如您所见,文件已加载,那么为什么在文件中找不到它呢?