Brunch.io与React和Babel语法错误

时间:2017-11-09 09:41:01

标签: reactjs ecmascript-6 syntax-error babel brunch

我第一次使用Brunch.io构建新的React应用程序。 这一切似乎工作正常但我尝试设置状态或使用箭头功能的那一刻我得到一个语法错误。我的猜测是我错过了一个Babel预设或另一个配置。

有人可以帮忙或知道这件遗失的东西是什么吗?

控制台错误:

09:27:18 - error: Compiling of app/components/index.js failed. Error: SyntaxError: app/components/index.js: Unexpect
ed token (10:15)
      |      * @return {string}    formatted list of authors
      |      */
   >  |     getAuthors = (arr) => {
      |                ^
      |         if (arr) {
      |             return arr.join('\r\n');
      |         } else {

的package.json

{
  "name": "brunch-app",
  "description": "Brunch.io application",
  "private": true,
  "author": "Brunch",
  "version": "0.0.1",
  "repository": "",
  "scripts": {
    "start": "brunch watch --server",
    "build": "brunch build --production"
  },
  "dependencies": {
    "prop-types": "^15.6.0",
    "react": "^16.0.0",
    "react-dom": "^16.0.0",
    "react-redux": "^5.0.6",
    "react-router-dom": "^4.2.2",
    "redux": "^3.7.2"
  },
  "devDependencies": {
    "auto-reload-brunch": "^2",
    "hmr-brunch": "^0.1",
    "babel-brunch": "~6.0.0",
    "babel-preset-latest": "^6",
    "babel-preset-react": "~6.22",
    "brunch": "^2",
    "clean-css-brunch": "^2",
    "uglify-js-brunch": "^2"
  }
}

早午餐-config.js:

exports.files = {
  javascripts: {
    joinTo: 'app.js'
  },
  stylesheets: {joinTo: 'app.css'}
};

exports.plugins = {
  babel: {presets: ['es2015', 'react']}
};

exports.hot = true;

components / index.js无法编译:

import React from 'react';

class Book extends React.Component {

    state = {
        authors: [],
        books: [],
    };

    getAuthors = (arr) => {
        if (arr) {
            return arr.join('\r\n');
        } else {
            return 'Unknown author';
        }
    };

    render() {

        return (
            <li>
                {book.authors && book.authors.map((author) => (
                    <span className='book-authors'>{author}</span>
                ))}
            </li>
        );
    }
}

export default Book;

1 个答案:

答案 0 :(得分:1)

getAuthors是一个类属性。这是一个提议的进化,您可以通过使用babel-plugin-transform-class-properties

让Babel进行转换