我正在尝试为Xstate创建一个解释器,并尝试将我在单独文件中创建的Machine传递给它,类似于:
import { Machine } from 'xstate';
const testMachine = Machine({
id: 'testMachine',
initial: 'start',
states: {
start: {
on: {
PUB_TOPIC: 'wait_micro_res',
},
},
wait_micro_res: {
on: {
MACHINE_DISCONNECTED: 'disconnection',
CONFIRMATION_RECEIVED: 'wait_order',
},
},
wait_order: {
on: {
DISCONNECTION_ORDER: 'end',
EXPERIMENT_ORDER: 'wait_measurement',
},
},
wait_measurement: {
on: {
EXPERIMENT_FINISHED: 'end',
MEASUREMENT_RECEIVED: 'receive_measurement',
},
},
receive_measurement: {
on: {
SEND_2_EXPERIMENT_MS: 'wait_measurement',
},
},
disconnection: {
on: {
RECONNECTION: 'wait_micro_res',
},
},
end: {
type: 'final',
},
},
});
export default {
testMachine,
};我试着这样创建它:
import { interpret } from 'xstate/lib/interpreter';
import testMachine from '../stateMachine/index';
const machineService = interpret(testMachine)
.onTransition((state) => {
console.log(state.value);
})
.start();然而,我得到了这个错误:
TypeError: Cannot set property '_sessionid' of undefined当我尝试在解释器的同一文件中创建机器时,一切都运行得很好。我试着记录机器,它似乎被正确地导入,但我不知道是否还有一个我不知道的错误
发布于 2020-09-15 22:52:57
你的出口似乎有问题。您正在将{ testMachine }而不是testMachine导出为默认导出。
您应该使用:
export default testMachine;然后,当您import testMachine from '../stateMachine/index';时,您将获得所需的对象。
现在,您正在导入一个具有包含您的计算机的属性testMachine的对象。如果要保留导出,请使用:
const machineService = interpret(testMachine.testMachine)https://stackoverflow.com/questions/63904272
复制相似问题