gpt4 book ai didi

mobx-state-tree - 有动态类型支持吗?

转载 作者:行者123 更新时间:2023-12-05 07:17:21 25 4
gpt4 key购买 nike

在 MobX 状态树中,如何定义一个动态类型的属性?例如:可以分配给任何类型..比如整数、字符串、 bool 值、对象等

有什么想法吗?在 MST 中必须定义类型吗?

最佳答案

在这种情况下最好使用联合

myModel = types.model({
title: types.string,
dynamicProperty: types.union(
types.number,
types.string,
types.boolean,
types.model({
anotherProperty: types.string
}),
testModel
)
})

testModel = types.model({})

dynamicProperty 可以是字符串、数字、 bool 值或内部定义的模型实例,或者 testModel 实例。

API 文档中有关联合类型的更多信息 https://github.com/mobxjs/mobx-state-tree/blob/master/docs/API/README.md#union

关于mobx-state-tree - 有动态类型支持吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58820318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com