【问题标题】:Cannot use react-router BrowserRouter, Support for the experimental syntax 'classProperties' isn't currently enabled无法使用 react-router BrowserRouter,目前未启用对实验语法“classProperties”的支持
【发布时间】:2020-05-13 14:05:05
【问题描述】:

我正在尝试将反应路由添加到我的代码中。所以我用了BrowserRouter

class App extends Component {
    render() {
        return (
            <BrowserRouter>
                <div className = "App">
                    <Blog/>
                </div>
            </BrowserRouter>
        );
    }
}

package.json

{
  "name": "react-complete-guide",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "axios": "^0.19.2",
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "react-router": "^5.2.0",
    "react-router-dom": "^5.2.0",
    "react-scripts": "3.4.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
  }
}

当我尝试运行代码时出现编译错误:

./node_modules/react-router-dom/modules/BrowserRouter.js
SyntaxError: D:\Dev\Webpack Projects\React Udemy\blogs\node_modules\react-router-dom\modules\BrowserRouter.js: Support for the experimental syntax 'classProperties' isn't currently enabled (11:11):

   9 |  */
  10 | class BrowserRouter extends React.Component {
> 11 |   history = createHistory(this.props);
     |           ^
  12 | 
  13 |   render() {
  14 |     return <Router history={this.history} children={this.props.children} />;

Add @babel/plugin-proposal-class-properties (https://git.io/vb4SL) to the 'plugins' section of your Babel config to enable transformation.
If you want to leave it as-is, add @babel/plugin-syntax-class-properties (https://git.io/vb4yQ) to the 'plugins' section to enable parsing.

我确实尝试安装@babel/plugin-proposal-class-properties。同样的错误。

我怎样才能让路由上班谢谢。

【问题讨论】:

    标签: reactjs react-router


    【解决方案1】:

    好吧,这是我的错。我使用了这个导入

    import {BrowserRouter} from "react-router-dom/modules/BrowserRouter";
    

    而不是这个

    import {BrowserRouter} from "react-router-dom";
    

    【讨论】:

      猜你喜欢
      • 2021-04-26
      • 2019-02-13
      • 2020-03-06
      • 2022-10-22
      • 2020-08-07
      • 2020-01-08
      • 2019-11-10
      • 1970-01-01
      • 2021-02-09
      相关资源
      最近更新 更多