在 Laravel 5.7 React 应用程序中,收到错误:“classProperties"当前未启用 [英] In Laravel 5.7 React app, getting the error: 'classProperties' isn't currently enabled

查看:39
本文介绍了在 Laravel 5.7 React 应用程序中,收到错误:“classProperties"当前未启用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 React.js 很陌生.我安装了 Laravel 5.7 并通过运行以下命令将 Vue.js 脚手架与 React 交换:

php artisan 预设反应

现在的问题是,我无法为组件内的状态分配任何内容.

例如,如果我在组件内部执行以下操作:

state = { foo: false };

我收到错误:

 ./resources/js/components/Root.js 中的错误模块构建失败(来自 ./node_modules/babel-loader/lib/index.js):SyntaxError: D:\xampp\htdocs\smart-school-v0.1\resources\js\components\Root.js: 当前未启用对实验语法classProperties"的支持 (8:11):

我安装了:

@babel/plugin-proposal-class-properties

并像这样更新 .babelrc(Babel 配置文件):

<代码>{预设":["@babel/preset-env",@babel/preset-react"],插件":[@babel/plugin-proposal-class-properties"]}

我按照

希望能帮到你.

I'm very new to React.js. I installed Laravel 5.7 and swapped Vue.js scaffolding with React by running this command:

php artisan preset react

Now the problem is, I cannot assign anything to the state inside a component.

For example if I do the following inside the component:

state = { foo: false };

I got the error:

ERROR in ./resources/js/components/Root.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
SyntaxError: D:\xampp\htdocs\smart-school-v0.1\resources\js\components\Root.js: Support for the experimental syntax 'classProperties' isn't currently enabled (8:11):

I installed:

@babel/plugin-proposal-class-properties

and updated .babelrc (Babel configuration file) like this:

{
  "presets": [
    "@babel/preset-env",
    "@babel/preset-react"
  ],
  "plugins": [
      "@babel/plugin-proposal-class-properties"
  ]
}

I followed this but no luck.

Package.json

{
    "private": true,

    "scripts": {
        "dev": "npm run development",
        "development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
        "watch": "npm run development -- --watch",
        "watch-poll": "npm run watch -- --watch-poll",
        "hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
        "prod": "npm run production",
        "production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --no-progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
    },
    "devDependencies": {
        "@babel/plugin-proposal-class-properties": "^7.2.3",
        "@babel/preset-env": "^7.2.3",
        "@babel/preset-react": "^7.0.0",
        "axios": "^0.18",
        "babel-preset-react": "^6.23.0",
        "bootstrap": "^4.0.0",
        "cross-env": "^5.1",
        "jquery": "^3.2",
        "laravel-mix": "^4.0.13",
        "lodash": "^4.17.5",
        "popper.js": "^1.12",
        "react": "^16.2.0",
        "react-dom": "^16.2.0",
        "resolve-url-loader": "^2.3.1",
        "sass": "^1.15.3",
        "sass-loader": "^7.1.0"
    },
    "dependencies": {
        "react-router-dom": "^4.3.1"
    }
}

.babelrc

 {
    "presets": [
    "@babel/preset-env",
    "@babel/preset-react"
    ],
    "plugins": [
        "@babel/plugin-proposal-class-properties"
    ]
 }

Root.js Component

import React, { Component } from 'react';
import ReactDOM from 'react-dom';

export default class Root extends Component {

    state = { foo: false };

    render() {

        return (
        <p>Loaded</p>
        );
    }
}

if (document.getElementById('app')) {
    ReactDOM.render(<Root />, document.getElementById('app'));
}

It is not working as expected and I'm keep getting this error:

Syntax Error: SyntaxError: D:\xampp\htdocs\smart-school-v0.1\resources\js\components\Root.js: Support for the experimental syntax 'classProperties' isn't currently enabled (19:11):

17 |
18 |
> 19 |     state = { foo: false };
    |           ^
20 |
21 |     render() {
22 |

Add @babel/plugin-proposal-class-properties (https://git.io/vb4SL) to the 'plugins' section of your Babel config to enable transformation.


@ ./resources/js/app.js 14:0-28
@ multi ./resources/js/app.js ./resources/sass/app.scss
                                                                                                                                                                                            Asset     Size   Chunks
    Chunk Names
/css/app.css  0 bytes  /js/app  [emitted]  /js/app
/js/app.js  593 KiB  /js/app  [emitted]  /js/app

ERROR in ./resources/js/components/Root.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
SyntaxError: D:\xampp\htdocs\smart-school-v0.1\resources\js\components\Root.js: Support for the experimental syntax 'classProperties' isn't currently enabled (19:11):

Please help me out.

Thanks in advance.

解决方案

  1. Cretae .babelrc file in root folder.

  2. Write in .babelrc:

{ "plugins": ["@babel/plugin-proposal-class-properties"] }

  1. Run npm install --save-dev @babel/plugin-proposal-class-properties

  2. Run npm run dev

Hope it will help you.

这篇关于在 Laravel 5.7 React 应用程序中,收到错误:“classProperties"当前未启用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆