问题描述
我正在尝试为Xstate创建解释,并试图将其在单独文件中创建的计算机传递给它,如下所示:
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
当我尝试在解释器的相同文件中创建计算机时,所有命令运行正常。我尝试登录该计算机,它似乎已正确导入,但是我不知道是否还有我不知道的其他错误
解决方法
您的出口似乎有问题。您正在导出{ testMachine }
作为默认导出,而不是testMachine
。
您应该使用:
export default testMachine;
然后,当您import testMachine from '../stateMachine/index';
时,将获得所需的对象。
目前,您正在导入具有属性testMachine
的对象,该对象包含您的计算机。
如果要保留该导出,请使用:
const machineService = interpret(testMachine.testMachine)