【问题标题】:Why a class is not being loaded correctly in React?为什么在 React 中没有正确加载一个类?
【发布时间】:2019-08-12 21:40:52
【问题描述】:

在我的应用程序中,我使用 React on Rails 和 webpack 来构建它。

我将 OSX 与 VSCode 一起使用。当我尝试从我的文件中导入我的 Redux Store 时,我得到一个奇怪的“未捕获的错误:找不到模块”../../bundles/store”。

但是在下一行,同一个文件,导入是正确的。

这是我的客户端/app/components/wallbuilder/wallbuilder.jsx 导入:

import { store as STORE } from "../../bundles/store";
import { enclosure } from "../../bundles/store/action_creators.jsx";

这里是我的客户端/app/bundles/store.jsx 文件:

import {createStore, combineReducers} from 'redux';

// Note: this file is not very decomposed on purpose.
// Start splitting only if this is unwieldy to handle

function counter(state = 0, action) {
  switch (action.type) {
    case 'INCREMENT':
      return state + 1
    case 'DECREMENT':
      return state - 1
    default:
      return state
  }
}
// Enclosure
import * as ACTIONS from './store/actions.jsx';
const ENCLOSURE_INITIAL = {cladding: '', type: '', showFilters: true};

function enclosure(state = ENCLOSURE_INITIAL, action) {
  switch(action.type) {
  case ACTIONS.ENCLOSURE_SET_CLADDING:
    return Object.assign({}, state, {cladding: action.value})
  case ACTIONS.ENCLOSURE_SET_TYPE:
    return Object.assign({}, state, {type: action.value})
  case ACTIONS.ENCLOSURE_SHOW_FILTERS:
    return Object.assign({}, state, {showFilters: action.value})
  default:
    return state;
  }

}



export const store = createStore(combineReducers({counter, enclosure}));

尝试导入 {store as STORE } 的这一行是被破坏的行。

在我拥有的文件夹/文件上。

那么,我的 wallbuilder 文件中没有正确加载商店的选项有哪些?

【问题讨论】:

标签: node.js reactjs webpack


【解决方案1】:

我认为你在这里犯了一个小错误。 JSX 是 react jsx 语法的扩展,但你的代码只是 js 代码,所以你需要像这样将文件扩展名更改为 js

import { enclosure } from "../../bundles/store/action_creators.js";

【讨论】:

  • 我把我所有的文件都移到了同一个文件夹中,它工作正常。我认为与导入“../../|
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-26
  • 2015-11-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多