Mobx未观察到初始化时未定义的对象属性

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

示例代码

interface Car {
   id: string,
   wheels: string,
   engine: string
}

class ExampleStore {
@Observable car:Car

public set car(value: Car){
    this.car = value
}
}

//Setting value of the car for the first time

const carExample = {
   id: "122",
   engine: "ABC and co."
}

car(carExample)

// Updating the value of car

const carExampleUpdate = {
   id: "122",
   wheels: "",
   engine: "ABC and co."
}

这里是当您首次设置不带车轮的汽车的值时,Mobx默认不观察车轮。因此,当您更新带轮子的汽车的价值时,Mobx仍然不会观察到轮子的属性,因为初始设置中未提及该属性。

我们如何使mobx观察轮的属性?

reactjs typescript mobx mobx-react
1个回答
0
投票

要回答这个问题,我们只需要使用extendObservable

 extendObservable(this.car, { wheels: "4" });

一旦使用此属性,然后mobx开始观察该属性。

© www.soinside.com 2019 - 2024. All rights reserved.