1
我目前想实现一个mobx存储,我可以从任何地方拨打像这样:React native和MobX:如何创建全球商店?
import {userStore} from '../UserStore.js';
在文件的结尾,我导出功能如下所示:
const userStore = new UserStore();
export {userStore};
正如我明白,每次我调用import
功能时,都会重新创建对象,导入UserStore的多个文件不会共享相同的变量。
但是,我希望每个导入的文件UserStore
都导入具有完全相同变量的相同对象。我怎样才能做到这一点?我不完全确定如何实现,所以任何想法和示例将不胜感激:)
完整的代码(UserStore.js声明),如果有任何帮助,如下所示(查看底部的出口语句)
import {observable, computed, action} from 'mobx';
import {ObservableMap, toJS} from 'mobx';
import {Fb} from './firebase.js';
class UserStore {
/** GPS */
@observable usrLng = 0.0;
@observable usrLat = 0.0;
@observable watchID = null;
@action
watchCurLocation() {
this.watchID = navigator.geolocation.watchPosition((position) => {
console.log("Recording GPS data from within the Store!!");
this.usrLat = parseFloat(position.coords.latitude);
this.usrLng = parseFloat(position.coords.longitude);
}, (error) => console.log(JSON.stringify(error)), {
enableHighAccuracy: true,
timeout: 2000,
maximumAge: 1000
});
}
@action
clearWatch() {
navigator.geolocation.clearWatch(this.watchID);
}
/*/ GPS */
/** BIKE BOOKING */
@observable interestBikeNo = -1;
@observable bookedBikeNo = -1;
@action
setInterestBikeNo(bn) {
this.interestBikeNo = bn;
}
}
const userStore = new UserStore();
export {userStore};
这是最优雅的解决方案,我在很长一段时间已经看到了,非常感谢! – DaveTheAl