【问题标题】:constant export doesn't work on react application恒定导出不适用于反应应用程序
【发布时间】:2019-09-03 02:55:07
【问题描述】:

我正在使用自己的 babel、webpack 配置从头开始构建简单的 React 应用程序,而没有使用 create-react-app。

Css 导入、React 组件、ES6 转译效果很好。 当我创建常量文件并在我的组件文件中导入时,出现错误

我已将常量移至组件本身,它工作正常。 但是当我创建常量文件并从组件中导入它时,它不起作用。

export default SKILLS = [
  {
    id: 1,
    title: 'React',
    point: 8,
  },
  {
    id: 2,
    title: 'Redux',
    point: 8,
  }
]

这是我的常量文件代码

import SKILLS from '../data/skills';

这是在我的组件中导入部分

文件夹结构

|-data
|  |-skills.js
|-components
|  |-ResumeSection.js

这应该可以正常工作。

我在 chrome 的开发控制台上遇到错误:

skills.js:2 Uncaught ReferenceError: SKILLS is not defined
    at eval (skills.js:2)
    at Module../src/data/skills.js (index_bundle.js:3921)
    at __webpack_require__ (index_bundle.js:727)
    at fn (index_bundle.js:101)
    at eval (ResumeSection.js:5)
    at Module../src/components/ResumeSection.js (index_bundle.js:3874)
    at __webpack_require__ (index_bundle.js:727)
    at fn (index_bundle.js:101)
    at eval (App.js:6)
    at Module../src/App.js (index_bundle.js:3828)
(anonymous) @ skills.js:2
./src/data/skills.js @ index_bundle.js:3921
__webpack_require__ @ index_bundle.js:727
fn @ index_bundle.js:101
(anonymous) @ ResumeSection.js:5
./src/components/ResumeSection.js @ index_bundle.js:3874
__webpack_require__ @ index_bundle.js:727
fn @ index_bundle.js:101
(anonymous) @ App.js:6
./src/App.js @ index_bundle.js:3828
__webpack_require__ @ index_bundle.js:727
fn @ index_bundle.js:101
(anonymous) @ index.js:6
./src/index.js @ index_bundle.js:3933
__webpack_require__ @ index_bundle.js:727
fn @ index_bundle.js:101
(anonymous) @ client:3
0 @ index_bundle.js:3944
__webpack_require__ @ index_bundle.js:727
(anonymous) @ index_bundle.js:794
(anonymous) @ index_bundle.js:797

我的 webpack 配置:

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  entry: './src/index.js',
  output: {
    path: path.join(__dirname, '/dist'),
    filename: 'index_bundle.js'
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: ['babel-loader'],
      },
      {
        test: /\.css$/,
        exclude: /node_modules/,
        use: ['style-loader', 'css-loader'],
      },
    ]
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: './src/index.html'
    })
  ]
}

【问题讨论】:

  • @MattOestreich,我尝试在我的webpack.config.js 文件中插入resolve 部分,但它不起作用。
  • 我忽略了您的导出方式...@nhp 提供了答案。这应该对你有用。

标签: reactjs webpack


【解决方案1】:

这是无效的语法,如果你想使用 default 导出,你有两个选择:

const SKILLS = [
  {
    id: 1,
    title: 'React',
    point: 8,
  },
  {
    id: 2,
    title: 'Redux',
    point: 8,
  }
]

export default SKILLS;
export default [
  {
    id: 1,
    title: 'React',
    point: 8,
  },
  {
    id: 2,
    title: 'Redux',
    point: 8,
  }
]

否则,您可以使用命名导出:

export const SKILLS = [
  {
    id: 1,
    title: 'React',
    point: 8,
  },
  {
    id: 2,
    title: 'Redux',
    point: 8,
  }
]

但这将用作import { SKILLS } from "../resources/skill";

【讨论】:

    【解决方案2】:

    在您的 skill.js 中导出 const SKILLS

    export const SKILLS = [
      {
        id: 1,
        title: 'React',
        point: 8,
      },
      {
        id: 2,
        title: 'Redux',
        point: 8,
      }
    ]
    

    然后导入使用

    import {SKILLS} from "../resources/skill";
    
    // do everything with SKILLS
    

    【讨论】:

    • 这个。或者你可以export default [{ ... }];(删除名字)。然后导入为skills。
    • 不敢相信我忽略了这一点。如果您仍然想使用export default,您只需这样做:export default [...] 然后导入您将使用import SKILLS from ....
    • 很高兴认识马特
    • 完美! @JonathanBogdanovicz
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-14
    • 1970-01-01
    • 1970-01-01
    • 2020-08-02
    • 1970-01-01
    • 2020-07-03
    • 1970-01-01
    相关资源
    最近更新 更多