在Laravel 5.7 React应用中,出现错误:当前未启用'classProperties'

时间:2019-01-07 19:27:00

标签: reactjs babel laravel-5.7

我对 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):

请帮帮我。

谢谢。

5 个答案:

答案 0 :(得分:5)

我刚刚在Laravel Framework 5.7.19上进行了测试,以下步骤可以正常工作:

  1. 确保您的.babelrc文件位于应用程序的根文件夹中,并添加以下代码:
{
  "plugins": ["@babel/plugin-proposal-class-properties"]
}
  1. 运行npm install --save-dev @babel/plugin-proposal-class-properties

  2. 运行npm run watch

答案 1 :(得分:5)

  1. Cretae .babelrc文件位于根文件夹中。

  2. 在.babelrc中写入:

  

{“插件”:[“ @ babel / plugin-proposal-class-properties”]}

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

  2. 运行npm run dev

enter image description here

希望它会对您有所帮助。

答案 2 :(得分:1)

简单将此添加到您的

package.json

var kdatasource = new kendo.data.DataSource({
        type: "odata",
        transport: {
            read: {
                    url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('ListTitleWithMoreThan5K')/items" //$scope.NextQuery,
                    dataType: "json",
                    contentType: "application/json",
                    beforeSend: function (xhr) {
                        xhr.setRequestHeader("Accept", "application/json; odata=verbose")
                }
            }
        },
        sort: { field: "ID" , dir: "desc"}, 
        requestEnd: function(e) {
            console.log(e);
            var response = e.response;
            var type = e.type;
            console.log(type); // displays "read"
            if(response)
            {
                //console.log(e.response.d.__next);
                $scope.NextQuery = e.response.d.__next; //getting next query in response. SP Online have 'SkipToken' not 'skip' for listitems. 
                console.log($scope.NextQuery);
            }
        },
        schema: {
            data: function (data) {
                return data.d && data.d.results ? data.d.results : [data.d];
            },
            total: function (data) {
                return 3000;//we can take care this with separate itemcount query
            },
            model: {
                fields: {
                    "ID": { type: "number" },
                    "Title": { type: "string" }
                }
            },
            errors: function (response) {
                console.error(response);
                return response.error;
            }
        },
        page:1,
        pageSize: 500, // this will become "$top" in query
        serverPaging: true,
        serverFiltering: true,
        serverSorting: true
});

$("#grid").kendoGrid({
    dataSource: kdatasource,
    columns: [{
                    field: "ID",
                    title: "ID",
                    width: 240
                },{
                    field: "Title",
                    title: "Title",
                    width: 240
                }],
    height: 550,
    groupable: false,
    sortable: true,
    filterable: true,
    pageable: {
        refresh: false,
        //pageSize: 10,
        pageSizes: [100, 200, 500, 1000],                   
        buttonCount: 5,
        change:function(e)
        {
             console.log("grid pager clicked!");
             console.log(e);
             //how to rebind grid for 2, 3, page number click?
        }
    } ,
    dataBound: function(e) {
        //console.log(e); // This is calling!
        //console.log("dataBound");
    }
});

答案 3 :(得分:0)

只需将以下行添加到您的 Package.json 文件中:

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

答案 4 :(得分:-1)

您可以简单地

  1. 创建 .babelrc 文件并将此代码粘贴到您的 .babelrc 文件中
    var name = req.body.templatename; var index_patterns = req.body.index_patterns; console.log(index_patterns); const opts: IndicesPutTemplateParams = { name: name, body: { index_patterns: [index_patterns], settings: { analysis: { filter: { autocomplete_filter: { type: "edge_ngram", min_gram: 1, max_gram: 20 } }, analyzer: { autocomplete: { type: "custom", tokenizer: "standard", filter: [ "lowercase", "autocomplete_filter" ] } } } }, mappings: { tags: { properties: { name: { type: "text", analyzer: "autocomplete", search_analyzer: "standard" }, normalized: { type: "text" }, status: { type: "text" }, createdat: { type: "date" }, updatedat: { type: "date" } } } } } } try { esClient.indices.putTemplate(opts).then((data: any) => { return res.json({ data }); console.log(data); }).catch((err: any) => { console.log(err); res.status(500).json({ err }) }); } catch (error) { res.status(500).json({ error }) } }```

  2. 现在运行{ "plugins": ["@babel/plugin-proposal-class-properties"] }

  3. 最后运行npm install --save-dev @babel/plugin-proposal-class-proper

完成