首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有React和Babel语法错误的Brunch.io

带有React和Babel语法错误的Brunch.io
EN

Stack Overflow用户
提问于 2017-11-09 09:41:01
回答 1查看 246关注 0票数 1

我第一次使用Brunch.io来构建一个新的反作用应用程序。这一切似乎都很好,但当我尝试设置状态或使用箭头函数时,就会出现语法错误。我的猜测是,我错过了一个Babel预置或另一个配置。

有人能帮忙或者知道这件丢失的东西可能是什么吗?

控制台错误:

代码语言:javascript
复制
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

代码语言:javascript
复制
{
  "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"
  }
}

brunch-config.js:

代码语言:javascript
复制
exports.files = {
  javascripts: {
    joinTo: 'app.js'
  },
  stylesheets: {joinTo: 'app.css'}
};

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

exports.hot = true;

组件/index.js无法编译:

代码语言:javascript
复制
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;
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-09 10:20:11

getAuthors是一个类属性。这是一种拟议的进化,您可以通过使用babel-类属性让Babel实现

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47198446

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档