mobx-state-tree - MST:用自己的孩子定义模型
问题描述
刚开始摆弄 Mobx-state-tree。
我有这个模型,它具有可以用同一模型的实例填充的属性 parent
。children
所以基本上是这样的:
const Page = types.model({
guid: types.string,
title: types.string,
description: types.string,
parent: types.union(Page, types.undefined),
children: types.array(Page),
})
但是,显然Page
还没有创建这个模型。
我怎样才能做到这一点?
解决方案
在仔细阅读文档后,我找到了答案。使用types.late()
:
const Page = types.model({
guid: types.string,
title: types.string,
description: types.string,
parent: types.union(types.late(() => Page), types.undefined),
children: children: types.optional(types.array(types.late(() => Page)), []),
})
推荐阅读
- php - 即使从 CDN 使用 select2 功能也不起作用
- reactjs - 开玩笑地模拟动画完成回调
- php - json数组到json php
- ruby-on-rails - 运行 rspec 时如何从命令行设置常量
- google-bigquery - Dataflow Runner - 尝试刷新以获取初始 access_token
- node.js - 哪个 ORM 最适合 nodejs?
- c++ - 将用户输入作为包含通配符表达式的字符串变量
- html - 输入类型(HTML)中的值和数据值有什么区别
- javascript - 在窗口上使用 React 内置 onScroll
- bash - 用于为 ssh 创建子进程的 bash 脚本