我根据此处的文档使用 django-pipeline 和 browserify -
http://gregblogs.com/how-django-reactjs-and-browserify/
当像这样加载 NPM/Bower 包时,我让它工作得很好 -
'build_js': {
'source_filenames': (
'js/bower_components/jquery/dist/jquery.js',
'bootstrap/js/bootstrap.js',
'js/bower_components/react/react-with-addons.js',
'js/bower_components/react/react-dom.js',
'datatables/js/jquery.dataTables.js',
'datatables/js/dataTables.bootstrap.js',
'js/node_modules/marked/marked.min.js',
'js/node_modules/react-router/umd/ReactRouter.js',
'js/child.js',
'js/parent.js',
'js/build.browserify.js',
),
'output_filename': 'js/build_js.js',
问题是我试图在 build.browserify.js 中引用 child.js 和 parent.js
这是3个文件的内容-
child.js
var Child = React.createClass({
render: function(){
return (
<div>
and this is the <b>{this.props.name}</b>.
</div>
)
}
});
父.js
var Parent = React.createClass({
render: function(){
return (
<div>
<div> This is the parent. </div>
<Child name="child"/>
</div>
)
}
});
build.browserify.js
ReactDOM.render(
<Parent />,
document.getElementById('content')
);
我的浏览器实际上出现了 3 个错误-
我的 child.js 和 parent.js 文件都在第 4 行发生以下情况 -
Uncaught SyntaxError: Unexpected token <
然后我在第 3 行的 build.browserify.browserified.js 上得到这个
Uncaught ReferenceError: Parent is not defined
这是该文件的内容 -
(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){
ReactDOM.render(
React.createElement(Parent, null),
document.getElementById('content')
);
},{}]},{},[1]);
编辑 -
如果我像这样将所有代码放在一个 build.browserify.js 文件中,它就可以工作 -
var Child = React.createClass({
render: function(){
return (
<div>
and this is the <b>{this.props.name}</b>.
</div>
)
}
});
var Parent = React.createClass({
render: function(){
return (
<div>
<div> This is the parent. </div>
<Child name="child"/>
</div>
)
}
});
ReactDOM.render(
<Parent />,
document.getElementById('app')
);