javascript - React ES6 SystemJS - 未捕获( promise )错误 : Unexpected token <(…)

标签 javascript reactjs ecmascript-6 systemjs

我通过下面的 System.config 安装并导入了 reactreact-dom,但我仍然收到以下错误:

Uncaught (in promise) Error: Unexpected token <(…)

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>ES2015 Module Example</title>
</head>
<body>
    <script src="lib/system.js"></script>
    <script>
        System.config({
            "baseURL": "src",

            // Set defaultJSExtensions to true so you don't have to use .js extension when importing the es6 module.
            "defaultJSExtensions": true,

            // 'plugin-babel' or 'traceur' or 'typescript'
            transpiler: 'traceur',

            map: {
                'react': './node_modules/react/dist/react.min.js',
                'react-dom': './node_modules/react-dom/dist/react-dom.min.js',
                'traceur': './lib/traceur.min.js',
                'plugin-babel': './lib/plugin-babel/plugin-babel.js',
                'systemjs-babel-build': './lib/plugin-babel/systemjs-babel-browser.js'
            },
        });
        System.import("app.js");
    </script>
</body>
<div id="example"></div>
</html>

应用程序.js:

import React from 'react';
import ReactDOM from 'react-dom';

var Hello = React.createClass({
    render: function() {
        return <h1>Hello {this.props.name}</h1>;
    }
});

ReactDOM.render(
    <Hello name="World" />,
    document.getElementById('example')
);

知道我还需要配置什么吗?

最佳答案

browserify 是最好的解决方案(用于生产和开发)- 对我来说:

npm install --save-dev babel-preset-react

一饮而尽:

var gulp = require('gulp');

var browserify = require('browserify');
var babelify = require('babelify');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var uglify = require('gulp-uglify');
var sourcemaps = require('gulp-sourcemaps');
var livereload = require('gulp-livereload');

gulp.task('build', function() {
    // app.js is your main JS file with all your module inclusions
    return browserify({entries: 'src/app.js', debug: true})
        .transform("babelify", { presets: ["es2015", "react"] })
        .bundle()
        .pipe(source('compile.min.js'))
        .pipe(buffer())
        .pipe(sourcemaps.init())
        .pipe(uglify())
        .pipe(sourcemaps.write('./maps'))
        .pipe(gulp.dest('js'))
        .pipe(livereload());
});

gulp.task('default', ['build']);

至于使用 SystemJS 的非生产环境(非常慢):

<!DOCTYPE html>
<script src="https://jspm.io/system@0.19.js"></script>
<script>
System.config({
  transpiler: 'babel',
  babelOptions: {}
});
System.import('./main.js');
</script>

您仍然可以使用 gulp 进行开发。只需将其添加到 gulpfile 中:

gulp.task('watch', ['build'], function () {
    livereload.listen();
    gulp.watch('js/*.js', ['build']);
});

gulp.task('default', ['watch']);

这使您免于其他繁琐的开发工作流程,如 listed在这里。

关于javascript - React ES6 SystemJS - 未捕获( promise )错误 : Unexpected token <(…),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39984743/

相关文章:

javascript - 在用户通过 Facebook 进行身份验证后,如何表明用户已通过身份验证?

javascript - 由于 <template> 在 HTML 中内联,Vue.js 2.0 在 Edge 中为空白

javascript - 删除 DOM 中的小数位

javascript - react 。登录后新路由器

reactjs - React Native TouchableOpacity OnPress 不使用循环

javascript - 更好的方法来处理 JavaScript 类中的状态变化

javascript - d3.json 请求成功但收到错误消息

JavaScript 或 React - 单页网站中的数据库安全

node.js - npm start 使用 React.js 给出 webpack cli 错误

javascript - 检测 ES2015 代码是由 babel 转译的还是本地运行的?