Reactjs Mobx未观察初始化时未定义的对象属性
示例代码Reactjs Mobx未观察初始化时未定义的对象属性,reactjs,typescript,mobx,mobx-react,Reactjs,Typescript,Mobx,Mobx React,示例代码 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: "12
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不会观察车轮。因此,当您更新car with wheels的值时,Mobx仍然不会观察wheels属性,因为在初始设置中没有提到它
我们如何让mobx观察轮子属性?要回答这个问题,我们只需要使用extendObservable
extendObservable(this.car, { wheels: "4" });
一旦你使用它,然后在那之后mobx开始观察属性