组件中的ReactJS意外令牌

ReactJS Unexpected Token in Component

本文关键字:意外 令牌 ReactJS 组件      更新时间:2023-09-26

我有一个组件:

import React from 'react';
export default class LeftNavigation extends React.Component {
    static contextTypes = {
        getStore: React.PropTypes.func.isRequired
    };
    render() {
        return (
            <div>
                Left
            </div>
        )
    }
}

但当我尝试捆绑(与JSPM)时,我得到:

SyntaxError: file:///C:/wamp/www/rentapp/public/src/components/add-property/LeftNavigation.js: Unexpected token (5:24)
     export default class LeftNavigation extends React.Component {
         static contextTypes = {
        getStore: React.PropTypes.func.isRequired
        }
    };

我该怎么解决这个问题?


这是我的package.json文件:

 {
  "private": true,
  "scripts": {
    "prod": "gulp --production",
    "dev": "gulp watch"
  },
  "devDependencies": {
    "bootstrap-sass": "^3.0.0",
    "gulp": "^3.9.1",
    "jspm": "^0.16.34",
    "laravel-elixir": "^5.0.0"
  },
  "jspm": {
    "directories": {
      "baseURL": "public"
    },
    "dependencies": {
      "body-parser": "npm:body-parser@^1.15.0",
      "express": "npm:express@^4.13.4",
      "fluxible": "npm:fluxible@^1.1.0",
      "fluxible-addons-react": "npm:fluxible-addons-react@^0.2.8",
      "jsx": "github:floatdrop/plugin-jsx@^1.2.1",
      "react": "npm:react@^15.0.2",
      "react-dom": "npm:react-dom@^15.0.2",
      "react-router": "npm:react-router@^2.4.0"
    },
    "devDependencies": {
      "babel": "npm:babel-core@^6.8.0",
      "babel-runtime": "npm:babel-runtime@^5.8.24",
      "core-js": "npm:core-js@^1.1.4"
    }
  },
  "dependencies": {
    "babel": "^6.5.2",
    "body-parser": "^1.15.0",
    "express": "^4.13.4",
    "fluxible": "^1.1.0",
    "react": "^15.0.2"
  },
  "name": "rentapp",
  "description": "[![Build Status](https://travis-ci.org/laravel/framework.svg)](https://travis-ci.org/laravel/framework) [![Total Downloads](https://poser.pugx.org/laravel/framework/d/total.svg)](https://packagist.org/packages/laravel/framework) [![Latest Stable Version](https://poser.pugx.org/laravel/framework/v/stable.svg)](https://packagist.org/packages/laravel/framework) [![Latest Unstable Version](https://poser.pugx.org/laravel/framework/v/unstable.svg)](https://packagist.org/packages/laravel/framework) [![License](https://poser.pugx.org/laravel/framework/license.svg)](https://packagist.org/packages/laravel/framework)",
  "version": "1.0.0",
  "main": "gulpfile.js",
  "directories": {
    "test": "tests"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
}
  • 你使用6.3.17以上的babel版本吗
  • 您是否将transform类属性作为babel插件包含在内
  • 最后但并非最不重要的是,尝试在babel配置中至少使用stage-1

将我的config.js的第一位更改为以下内容修复了问题,不知道为什么:

System.config({
  baseURL: "/",
  defaultJSExtensions: true,
  transpiler: "babel",
  babelOptions: {
    "optional": [
      "runtime",
      "optimisation.modules.system"
    ],
    "blacklist": [],
    "stage": 0
  },
...