MST:使用自身的子代定义模型

问题描述 投票:0回答:1

刚刚开始摆弄Mobx-state-tree。

我有这个模型,它具有parentchildren属性,可以用同一模型的实例填充。

基本上就是这样:

const Page = types.model({
    guid: types.string,
    title: types.string,
    description: types.string,
    parent: types.union(Page, types.undefined),
    children: types.array(Page),
})

但是,显然Page在创建此模型时尚不可用。

我该怎么做?

mobx-state-tree
1个回答
0
投票

仔细阅读文档后,我找到了答案。使用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)), []),
})
© www.soinside.com 2019 - 2024. All rights reserved.