将jsx转换为js后dom未定义错误

Neg*_*iri 5 javascript reactjs babeljs

我创建了一个jsx文件,如下所示

  /** @jsx dom */
(function(){
'use strict';

define([
        'jquery',
        'react',
        'react-dom'
    ],

    function($, React, ReactDOM){

        var AppView = React.createClass({
            render: function() {
                return <div>Hello World</div>;
            }
        });

        ReactDOM.render(<AppView />, document.getElementById('dsl-application'));

    });
})();
Run Code Online (Sandbox Code Playgroud)

并使用以下命令将jsx转换为js

babel --watch src --out-dir bundle --preset react
Run Code Online (Sandbox Code Playgroud)

和输出文件如下

(function () {
'use strict';

define(['jquery', 'react', 'react-dom'], function ($, React, ReactDOM) {

    var AppView = React.createClass({
        displayName: 'AppView',

        render: function () {
            return dom(
                'div',
                null,
                'Hello World'
            );
        }
    });


    ReactDOM.render(dom(AppView, null), document.getElementById('dsl-application'));
});
})();
Run Code Online (Sandbox Code Playgroud)

问题是我遇到了Uncaught ReferenceError:在浏览器中运行js文件时未定义dom的问题。转换后添加dom。任何线索如何解决这个问题?

Alf*_*yum 0

要将 JSX 在浏览器中转换为 JS,您需要browser.jsbabel. 有关简单示例,请参阅https://github.com/ruanyf/react-demos/blob/master/demo01/index.html

[[根据更多信息更新]]

使用以下文件,然后运行npm install​​和webpack,然后您的index.html服务就可以使用了。

索引.html

<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
    <div id="example"></div>

    <script type="text/javascript" src="bundle.js" charset="utf-8"></script>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

包.json

{
  "name": "another",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "react": "^0.14.7",
    "react-dom": "^0.14.7"
  },
  "devDependencies": {
    "babel-core": "^6.5.1",
    "babel-loader": "^6.2.2",
    "babel-preset-es2015": "^6.5.0",
    "babel-preset-react": "^6.5.0",
    "webpack": "^1.12.13",
    "webpack-dev-server": "^1.14.1"
  }
}
Run Code Online (Sandbox Code Playgroud)

main.jsx

'use strict'

var React = require('react')
var ReactDOM = require('react-dom')
var AppView = require('./component.jsx')

ReactDOM.render(
    <AppView />,
    document.getElementById('example')
);
Run Code Online (Sandbox Code Playgroud)

组件.jsx

'use strict'

var React = require('react')

var AppView = React.createClass({
    render: function(){
        return(<h1>Hello, world!</h1>)
    }
});

module.exports = AppView
Run Code Online (Sandbox Code Playgroud)

webpack.config.js

module.exports = {
    entry: './main.jsx',
    output: {
        path: __dirname,
        filename: "bundle.js"
    },
    module: {
      loaders: [
        {
          test: /\.jsx?$/,
          exclude: /(node_modules|bower_components)/,
          loader: 'babel', // 'babel-loader' is also a legal name to reference
          query: {
            presets: ['react', 'es2015']
          }
        }
      ]
    }
}
Run Code Online (Sandbox Code Playgroud)