我正在使用MOBX-socket.io-为我的应用程序做出反应。在每个套接字事件上,我都调用一个操作来更新我的mobx store属性,如下所示:
export default class VehicleDataStore {
@observable steeringType = '';
constructor(websocketClientStore) {
websocketClientStore.subscribe('steeringType', this.updateSteeringType.bind(this));
}
@action
updateSteeringType(val) {
const steeringType = getVdAttrValue(val);
console.log('Store received new value: ', steeringType); <== "LEFT_HAND_DRIVE"
if (steeringType !== SIGNAL_INVALID) {
this.steeringType = SteeringType;
console.log('Store value updated: ', this.steeringType);
}
}
}
但是如果我登录this.steeringType
,它将记录:
Proxy {Symbol(mobx administration): e}
[[Handler]]: Object
[[Target]]: Object
LEFT_HAND_DRIVE: "LEFT_HAND_DRIVE"
RIGHT_HAND_DRIVE: "RIGHT_HAND_DRIVE"
Symbol(mobx administration): e {target: {…}, values: Map(2), name: "[email protected]", keysAtom: e, defaultEnhancer: ƒ, …}
get LEFT_HAND_DRIVE: ƒ ()
set LEFT_HAND_DRIVE: ƒ (t)
get RIGHT_HAND_DRIVE: ƒ ()
set RIGHT_HAND_DRIVE: ƒ (t)
__proto__: Object
[[IsRevoked]]: false
目标值到达对象。如何防止将字符串转换为可观察对象?还是为什么会这样?
Mobx存储值是可观察到的,但它们的行为类似于字符串,数组或对象。使用此值时,您将获得实际值,例如
const value = `${this.steeringType}`;
console.log(typeof value); // string
console.log(value); // LEFT_HAND_DRIVE etc.