在 Laravel 5.7 React 应用程序中,收到错误:“classProperties”当前未启用
Posted
技术标签:
【中文标题】在 Laravel 5.7 React 应用程序中,收到错误:“classProperties”当前未启用【英文标题】:In Laravel 5.7 React app, getting the error: 'classProperties' isn't currently enabled 【发布时间】:2019-06-02 11:37:22 【问题描述】:我是 React.js 的新手。我安装了 Laravel 5.7 并通过运行以下命令将 Vue.js 脚手架与 React 交换:
php artisan preset react
现在的问题是,我不能为组件内的状态分配任何东西。
例如,如果我在组件内部执行以下操作:
state = foo: false ;
我得到了错误:
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):
我安装了:
@babel/plugin-proposal-class-properties
并像这样更新了 .babelrc(Babel 配置文件):
"presets": [
"@babel/preset-env",
"@babel/preset-react"
],
"plugins": [
"@babel/plugin-proposal-class-properties"
]
我关注了this,但没有运气。
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 组件
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'));
它没有按预期工作,我不断收到此错误:
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):
请帮帮我。
提前致谢。
【问题讨论】:
感谢您的提问。我有一个类似的问题。你的帖子帮我解决了。谢谢! 不客气@dns_nx 【参考方案1】:根文件夹中的Cretae .babelrc 文件。
写入.babelrc:
"插件": ["@babel/plugin-proposal-class-properties"]
运行 npm install --save-dev @babel/plugin-proposal-class-properties
运行 npm run dev
希望对你有所帮助。
【讨论】:
感谢您抽出宝贵的时间写下这个答案,并解决我的问题【参考方案2】:我刚刚在 Laravel Framework 5.7.19 上进行了测试,以下步骤有效:
-
确保您的
.babelrc
文件位于应用程序的根文件夹中,并添加以下代码:
"plugins": ["@babel/plugin-proposal-class-properties"]
运行npm install --save-dev @babel/plugin-proposal-class-properties
。
运行npm run watch
。
【讨论】:
在我的情况下它不起作用。我在根目录上创建了一个 .bablerc 文件,但奇怪的是,我一直收到同样的错误。 它对我有用。【参考方案3】:简单的添加到你的
package.json
"babel":
"presets": [
"@babel/preset-env",
"@babel/preset-react"
],
"plugins": [
"@babel/plugin-proposal-class-properties"
]
【讨论】:
它给出了错误Unknown plugin "@babel/plugin-proposal-class-properties" specified
【参考方案4】:
你可以简单
创建 .babelrc 文件并将此代码粘贴到您的 .babelrc 文件中 "plugins": ["@babel/plugin-proposal-class-properties"]
现在运行npm install --save-dev @babel/plugin-proposal-class-proper
npm run watch
完成
【讨论】:
【参考方案5】:只需将这些行添加到您的 Package.json 文件中:
"babel":
"presets": [
"@babel/preset-env",
"@babel/preset-react"
],
"plugins": [
"@babel/plugin-proposal-class-properties"
]
【讨论】:
以上是关于在 Laravel 5.7 React 应用程序中,收到错误:“classProperties”当前未启用的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Laravel 5.7 和 Laravel Collective 中使用动作 [重复]