React ES6 SystemJS - Uncaught(in promise)错误:意外的令牌<(...)

时间:2016-10-11 18:59:54

标签: javascript reactjs ecmascript-6 systemjs

我已通过下面的react安装并导入了react-domSystem.config,但我仍然会收到以下错误:

  

未捕获(在承诺中)错误:意外的令牌<(...)

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>

app.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')
);

任何想法我还需要配置什么?

2 个答案:

答案 0 :(得分:0)

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

AddressService

吞:

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

对于使用SystemJS的非生产(痛苦地缓慢):

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']);

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

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

这使您免于其他繁琐的开发工作流程listed

答案 1 :(得分:-1)

SDK通常出现在html5应用程序中,当服务器配置为返回Unexpected token <而不是404页面的内容时(因此在动态路由上按f5仍然有效)。检查浏览器开发人员控制台中的网络面板,然后查看使用html内容提供的js文件。