1

正如标题所暗示的那样,我有一个受保护的转换,我想处于分层状态节点中,但是 xState 似乎无法读取保护的属性并返回一个“ TypeError: Cannot read property 'propertyName' of undefined" error

有没有办法在 xState 中做到这一点,或者在这种情况下我应该在没有分层状态节点的情况下继续

4

2 回答 2

2

我遇到的问题是我将上下文分配给了我的分层状态机而不是父机器,因此警卫无法访问它。将上下文移动到父机器已经解决了这个问题

于 2020-01-30T15:04:42.033 回答
1

我使用文档中的分层状态机示例来尝试对此进行建模,并在示例的末尾添加以下内容:

    on: {
      POWER_OUTAGE: '.red.blinking',
      POWER_RESTORED: '.red',
      POWER_TEST: {
        target: '.red.stop',
        cond: {
          type: 'test'
        }
      }
    }
  },{
    guards: {
      test: () => true
    }
  });

这似乎按预期工作,您可以在此处的可视化器中尝试这台机器和保护示例

您可以翻转测试守卫中的布尔值以使其正常工作/不工作。

以下是示例的完整代码供参考:

const pedestrianStates = {
    initial: 'walk',
    states: {
      walk: {
        on: {
          PED_COUNTDOWN: 'wait'
        }
      },
      wait: {
        on: {
          PED_COUNTDOWN: 'stop'
        }
      },
      stop: {},
      blinking: {}
    }
  };

  const lightMachine = Machine({
    key: 'light',
    initial: 'green',
    states: {
      green: {
        on: {
          TIMER: 'yellow'
        }
      },
      yellow: {
        on: {
          TIMER: 'red'
        }
      },
      red: {
        on: {
          TIMER: {
            target: 'green',
            cond: {
              type: 'searchValid'
            }
          }
        },
        ...pedestrianStates
      }
    },
    on: {
      POWER_OUTAGE: '.red.blinking',
      POWER_RESTORED: '.red',
      POWER_TEST: {
        target: '.red.stop',
        cond: {
          type: 'test'
        }
      }
    }
  },{
    guards: {
      test: () => true
    }
  });
于 2020-01-29T09:57:37.917 回答