Node.js 如何导入此函数?
我试图建立一个POS系统,与厨房进行通信。现在我正在尝试将reducer导入到我的express服务器,但我似乎无法将其作为函数或模块导入。我正在将React和useContext与useReducer一起使用,希望能够有一个中央系统来管理所有调度操作。以下是影响最小化的页面(如果有帮助):Node.js 如何导入此函数?,node.js,reactjs,typescript,express,Node.js,Reactjs,Typescript,Express,我试图建立一个POS系统,与厨房进行通信。现在我正在尝试将reducer导入到我的express服务器,但我似乎无法将其作为函数或模块导入。我正在将React和useContext与useReducer一起使用,希望能够有一个中央系统来管理所有调度操作。以下是影响最小化的页面(如果有帮助): INDEX.JS const app = require('express')(); const server = require('http').createServer(app); const choo
INDEX.JS
const app = require('express')();
const server = require('http').createServer(app);
const chooseSystem = require('./../kassa/controller/login/login.tsx');
import {reducer} from './src/utils/reducer'
const io = require("socket.io")(server, {
cors: {
origin: "http://192.168.0.25:8100",
methods: ["GET", "POST"]
}
});
io.on('connection', (socket) =>
{
// Landing page, user chooses system to log in to POS or kitchen
socket.on('CHOOSESYSTEM', dispatchFunction => {
chooseSystem.chooseSystem(dispatchFunction);
});
});
server.listen(3000, () => { console.log("Listening on 3000");});
/////////////////////////////////////////////////////////////////////////////
REDUCER.TSX
import React from "react";
import { GlobalInitContextProps, GlobalStateProps } from "./interfaces";
const reducer = (state = initGlobalState, action: any) => {
switch (action.type) {
case "LOG_IN_TO_POS":
return {
...state,
isLoggedIn: {
inSystem: true,
inPOS: true,
},
};
case "LOG_IN_TO_KITCHEN":
return {
...state,
isLoggedIn: {
inSystem: true,
inPOS: false,
},
};
case "GO_TO":
return {
...state,
...action.payload,
};
default:
return new Error("Something went wrong with the reducer");
}
};
// Export
export {reducer};
我尝试将其导出为如下模块:module.exports.reducer=reducer,并在我的index.js中使用require(“”),但它不起作用。找不到模块。不要介意功能,因为没有其他页面就没有意义
这是我的项目结构:
- 卡萨
- INDEX.JS
- SRC
- 乌提尔斯
- 减速器
- 乌提尔斯
REDUCER.TSX
import React from "react";
import { GlobalInitContextProps, GlobalStateProps } from "./interfaces";
export default (state = initGlobalState, action: any) => {
switch (action.type) {
case "LOG_IN_TO_POS":
return {
...state,
isLoggedIn: {
inSystem: true,
inPOS: true,
},
};
case "LOG_IN_TO_KITCHEN":
return {
...state,
isLoggedIn: {
inSystem: true,
inPOS: false,
},
};
case "GO_TO":
return {
...state,
...action.payload,
};
default:
return new Error("Something went wrong with the reducer");
}
};
而进口它就像
import reducer from './src/utils/reducer'
谢谢你的回答。我尝试了您的方法,但不幸的是,我仍然在说:SyntaxError:意外标识符时出错。“从“./src/utils/reducer”导入reducer。意外的标识符指向“import”后面的单词reducer。为什么要在后端使用React reducer?我不知道。有没有更好的方法来处理全局状态或操纵它?