【发布时间】:2016-02-06 17:24:00
【问题描述】:
我正在使用 requireJS 加载 React 组件,但我收到错误“Uncaught SyntaxError: Unexpected token docs 和 explained in this question 的说明设置 scriptType,但我无法让它工作或找到一个很好的例子来说明如何使这个工作。
requireConfig.js:
requirejs.config({
baseUrl: 'scripts/',
paths:
{
jquery: 'jquery-1.9.0',
react: 'libs/build/react',
reactdom: 'libs/build/react-dom',
browser: '//cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.23/browser.min',
inputWindow: 'inputWindow/inputWindow'
},
scriptType: {
'inputWindow': "text/babel"
}
});
define(function (require) {
var InputWindow = require('inputWindow');
InputWindow.initialize();
});
inputWindow.js:
define(function(require){
var React = require('react');
var ReactDOM = require('reactdom');
var InputWindow = React.createClass({
render: function(){
return(<div>
{this.props.message}
</div>)
}
});
function initialize(){
ReactDOM.render(<InputWindow message="Hello World!"/>, document.getElementById('inputWindowDiv'))
}
return {
initialize: initialize,
}
})
当我使用该部分配置 requireConfig.js 时
scriptType:{
'inputWindow':'text/babel'
}
然后将文件 inputWindow.js 加载到带有标签的 index.html 中
type="[Object Object]"
直到 requireJS 超时。
screen capture of inputWindow.js loaded with type=[Object Object]
【问题讨论】:
-
对不起,我现在没有答案,但我想知道,requirejs 是必需的吗?我认为有更简单的方法来加载和转换 jsx / react 组件。
-
这不是必需的,但它是我熟悉的工具。我想知道这个问题的答案,即使最终的最佳答案是使用不同的工具。