我该如何解决在React中无法分配为只读对象的属性“ exports”?

时间:2019-06-26 12:27:42

标签: javascript html css node.js reactjs

我是新手,我想从头开始构建一个简单的应用程序。我遇到一条错误消息:

  

未捕获的TypeError:无法分配为只读属性'exports'的   对象'#'在Module ../ src / app / about.js(bundle.js:2081)在    webpack_require (bundle.js:724)

about.js

var React = require("react");
var createReactClass = require("create-react-class");
import { Link } from "react-router";

var About = createReactClass({
  render: function() {
    return(
        <Link to={"/"}>Home</Link>
    );
  }
});

module.exports = About;

webpack.config.js

var path = require('path');

module.exports = {
  entry: path.resolve(__dirname, 'src') + '/app/index.js',
  output: {
    path: path.resolve(__dirname, 'dist') + '/app',
    filename: 'bundle.js',
    publicPath: '/app/'
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        include: path.resolve(__dirname, 'src'),
        loader: 'babel-loader'
      },
      {
        test: /\.css$/,
        loader: 'style-loader!css-loader'
      },
      {
        test: /\.(jpe?g|png|gif|woff|woff2|eot|ttf|svg)(\?[a-z0-9=.]+)?$/,
        loader: 'url-loader?limit=100000'        
      }
    ]
  }
};

package.json

  "devDependencies": {
    "babel-core": "^6.16.0",
    "babel-loader": "^6.2.5",
    "babel-preset-es2015": "^6.16.0",
    "babel-preset-react": "^6.16.0",
    "create-react-class": "^15.6.3",
    "css-loader": "^2.1.1",
    "style-loader": "^0.23.1",
    "webpack": "^1.13.2",
    "webpack-dev-server": "^1.16.1"
  }

.babelrc

{
  "presets": [
    "@babel/preset-env",
    "@babel/preset-react"
  ]
}

我认为这与我的module.exports

有关

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

在文件顶部,而不是使用import方法,请使用以下方法:

const { Link } = require("react-router");