Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs Mobx未观察初始化时未定义的对象属性_Reactjs_Typescript_Mobx_Mobx React - Fatal编程技术网

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开始观察属性