gpt4 book ai didi

reactjs - 使用 requireJS 将脚本类型更改为 "text/babel"

转载 作者:行者123 更新时间:2023-12-03 13:55:16 24 4
gpt4 key购买 nike

我正在使用 requireJS 加载 React 组件,但收到错误“Uncaught SyntaxError: Unexpected token <”,因为文件的脚本类型是“text/javascript”而不是“text/babel”。为了解决这个问题,我尝试按照 requireJS docs 的解释设置 scriptType和explained in this question ,但我无法让它发挥作用,也无法找到一个很好的例子来说明如何使其发挥作用。

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]

最佳答案

而不是

scriptType: {
'inputWindow': "text/babel"
}

尝试

scriptType: 'text/babel'

它应该可以工作。现在您正在尝试对一个对象进行字符串化,所以难怪它不起作用。 ;)

关于reactjs - 使用 requireJS 将脚本类型更改为 "text/babel",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35244020/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com