目前我的nodejs服务器中的所有模块都是作为require()导入的,即:
let path = require('path');
let express = require('express');
let http = require('http');
let app = express();
Run Code Online (Sandbox Code Playgroud)
但是,我关注的教程显示它们导入为:
import express from 'express'
import path from 'path'
Run Code Online (Sandbox Code Playgroud)
哪个引发错误:
SyntaxError: Unexpected token import
Run Code Online (Sandbox Code Playgroud)
我的webpack.config.js设置为:
module: {
rules: [
{
test: /\.js?$/,
use: 'babel-loader',
exclude: /node_modules/
}
]
}
Run Code Online (Sandbox Code Playgroud)
在bablerc中:
{
"presets": ["es2015", "react"]
}
Run Code Online (Sandbox Code Playgroud)
我的包版本:
"babel-core": "^6.7.6",
"babel-loader": "^6.2.4",
"babel-preset-es2015": "^6.6.0",
"babel-preset-react": "^6.5.0",
"react": "^15.0.1",
"devDependencies": {
"babel-cli": "^6.18.0",
"babel-preset-env": "0.0.3",
"webpack": "^2.2.1",
"webpack-dev-middleware": "^1.10.1",
"webpack-dev-server": "^2.4.1",
"webpack-hot-middleware": "^2.17.1"
}
Run Code Online (Sandbox Code Playgroud)
导入适用于我的所有反应组件文件,而不是server.js.如何将服务器切换到从require导入?
我有一个session reducer(使用redux-session库),它使用中间件从localstore恢复状态.我可以从调试工具中看到这是按预期工作的,但它正在被我的用户reducer的初始状态所取代.
我觉得我应该使用preloadedState,但我不能将reducer的结果变成createStore?
正在正确恢复storedState(我可以将其登录到控制台).
session: {user: {data: bhBSh}}, user: {data: null}
Run Code Online (Sandbox Code Playgroud)
在重新加载页面时,我无法看到将"会话"复制回"用户"的最佳方法?
会话缩减器:
function sessionReducer (state = {}, action) {
switch (action.type) {
case 'LOAD_STORED_STATE':
console.log(action.storedState); //Working!!!!!
return action.storedState;
default:
return state;
}
}
Run Code Online (Sandbox Code Playgroud)
用户减速机:
import { fromJS } from 'immutable';
import {
USER_LOGGING_IN,
USER_LOGGED_IN,
USER_LOGGED_OUT,
} from '../../constants';
const userInitialState = fromJS({
data: null,
isLoading: false,
});
function userReducer(state = userInitialState, action) {
switch (action.type) {
case USER_LOGGING_IN:
return state
.set('isLoading', true);
case USER_LOGGED_IN:
return state
.set('data', action.payload)
.set('isLoading', …Run Code Online (Sandbox Code Playgroud) 我正在尝试将javascript列表转换为具有指定键的字典.我正在使用以下代码:
let list = ['cat', 'rabbit', 'fish']
list = list.map(x => {
return ({ animal: x });
})
console.log(list);Run Code Online (Sandbox Code Playgroud)
当然这不起作用.编辑:它确实
预期结果:
[
{
"animal": "cat"
},
{
"animal": "rabbit"
},
{
"animal": "fish"
}
]
Run Code Online (Sandbox Code Playgroud)
编辑:
let和list = list实际上是问题中的拼写错误 - 它在我的真实代码中是正确的.我没有测试这个片段,因为我认为它不起作用.然后我也混淆了自己并确实输入了错误的预期结果.我的代码更复杂,发布它没有意义.作为作品,我认为我的错误必须在其他地方.谢谢您的帮助.