我有奇怪的错误。
当我尝试像这样导入 redux 时:
import Redux from 'redux'
当我试图安慰它时
console.log('Redux is', Redux);
它告诉我 Redux 是未定义的。
我正在使用带有 babel loader 的 webpack,所有标准配置,似乎没有任何问题。我也 yarn 删除了所有模块并重新添加它们,仍然得到同样的结果。现在我的 index.js 很简单,如下所示:
import Redux from 'redux';
console.log('Redux is ', Redux);
我的 webpack 配置如下:'
module.exports = {
devtool: 'inline-source-map',
entry: ['./src/index.jsx', 'babel-polyfill'],
output: {
path: path.resolve('dist'),
filename: 'bundle.js',
publicPath: '/',
},
resolve: {
extensions: ['.js', '.jsx'],
},
module: {
rules: [
{
test: /\.jsx?$/,
use: {
loader: 'babel-loader',
},
include: __dirname,
exclude: /node_modules/,
}
],
},
devServer: {
historyApiFallback: true,
},
plugins: [HtmlWebpackPluginConfig],
};
我正在使用 yarn 。下面是我的 package.json
"dependencies": {
"redux": "^3.7.2"
},
"devDependencies": {
"babel-cli": "^6.24.1",
"babel-core": "^6.25.0",
"babel-loader": "^7.1.1",
"babel-polyfill": "^6.23.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-2": "^6.24.1",
"html-webpack-plugin": "^2.29.0",
"path": "^0.12.7",
"webpack": "^3.4.1",
"webpack-dev-server": "^2.6.1"
},
如果我通过在 index.html 上添加脚本标签来切换到使用 CDN,它工作正常。
=========编辑========
作为 Gilad 的回答,这样做是可行的
import { createStore } from redux;
// do stuff with createStore
但是为什么我不能这样做:
import Redux from 'redux';
const { createStore } = Redux;
后一个给我“无法读取未定义的属性‘createStore’”
=========更多编辑========
我刚刚看了 redux 的源代码,它是用 typescript 写的,没有“export default”或“export as namespace”,这可能是我不能使用的原因
import Redux from 'redux';
const { createStore } = Redux;
如果我错了请纠正我。
请帮忙。
谢谢
最佳答案
您的配置没问题。
除了使用 import Redux from 'redux'
,例如使用 Redux.createSore
,您还可以使用:import { createStore } from 'redux '
然后直接调用createStore
。
combineReducers
和 redux 公开的任何其他函数也是如此。
关于reactjs - 导入 redux 结果为 undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45387444/