在我的React Native应用程序中,GeoService类是一组静态方法,带有两个声明的静态变量id和position。
如果我尝试使用this关键字从静态方法中引用静态类变量,它似乎会在不同的作用域中创建一个新变量。如果没有this关键字,它会给出一个无法解析变量的直接错误。
static关键字在JS中是否真的意味着任何有用的东西,以及什么方法可以在这里定义静态变量/方法?
CaptureView.js
class CaptureView extends Component {
constructor(props) {
super(props);
GeoService.start();
}
componentWillUnmount() {
GeoService.stop();
}
onButtonPress() {
Promise.all([this.cameraSrv.takePicture(), GeoService.getPosition()]).then(data => {
let lat = data[1].lat; // PROBLEM HERE - Cannot read property 'lat' of undefined
let log = data[1].lng;
});
}
}GeoService.js
import Logger from '../utils/Logger';
export default class GeoService {
static _id = undefined;
static _position = undefined;
static start() {
GeoService._getQuickPosition();
GeoService._watchAccuratePosition();
}
static stop() {
GeoService._clearWatch();
}
static getPosition() {
return GeoService._position;
}
static _getQuickPosition() {
navigator.geolocation.getCurrentPosition(
GeoService._successCallback,
GeoService._errorCallback,
{enableHighAccuracy: false, timeout: 20000, maximumAge: 1000}
);
}
static _watchAccuratePosition() {
if (GeoService._id) {
return;
}
GeoService._id = navigator.geolocation.watchPosition(
GeoService._successCallback,
GeoService._errorCallback,
{enableHighAccuracy: true, timeout: 20000, maximumAge: 1000}
);
Logger.info('GeoService watch started');
}
static _clearWatch() {
navigator.geolocation.clearWatch(GeoService._id);
delete GeoService._id;
Logger.info('GeoService watch ended');
}
static _successCallback(position) {
GeoService._position = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
Logger.info('GeoService new position: ' + JSON.stringify(GeoService._position));
}
static _errorCallback(error) {
Logger.info('GeoService error: ' + error);
}
}React本机控制台:
GeoService watch started
GeoService new position: {"lat":50.54822785298157,"lng":7.132454606843366}
GeoService new position: {"lat":50.54822785298157,"lng":7.132454606843366}
takePicture...
TypeError: Cannot read property 'lat' of undefined
at CaptureView.js:187
at tryCallOne (core.js:37)
at core.js:123
at JSTimers.js:298
at _callTimer (JSTimers.js:152)
at _callImmediatesPass (JSTimers.js:200)
at Object.callImmediates (JSTimers.js:473)
at MessageQueue.__callImmediates (MessageQueue.js:337)
at MessageQueue.js:135
at MessageQueue.__guard (MessageQueue.js:314)发布于 2019-09-04 22:56:47
以下是我的解决方案,将静态类变量称为GeoService.xxx
import Logger from '../utils/Logger';
export default class GeoService {
static _id = undefined;
static _position = undefined;
static start() {
GeoService._getQuickPosition();
GeoService._watchAccuratePosition();
}
static stop() {
GeoService._clearWatch();
}
static getPosition() {
return GeoService._position;
}
static _getQuickPosition() {
navigator.geolocation.getCurrentPosition(
GeoService._successCallback,
GeoService._errorCallback,
{enableHighAccuracy: false, timeout: 20000, maximumAge: 1000}
);
}
static _watchAccuratePosition() {
if (GeoService._id) {
return;
}
GeoService._id = navigator.geolocation.watchPosition(
GeoService._successCallback,
GeoService._errorCallback,
{enableHighAccuracy: true, timeout: 20000, maximumAge: 1000}
);
Logger.info('GeoService watch started');
}
static _clearWatch() {
navigator.geolocation.clearWatch(GeoService._id);
delete GeoService._id;
Logger.info('GeoService watch ended');
}
static _successCallback(position) {
GeoService._position = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
Logger.info('GeoService new position: ' + JSON.stringify(GeoService._position));
}
static _errorCallback(error) {
Logger.info('GeoService error: ' + error);
}
}发布于 2019-09-04 23:08:23
我认为只能在实例中使用this,因为它必须引用实例化的对象。看起来你的解决方案是写它的正确方式。
我不确定,但是在你的静态类中,我不认为你需要像这样嵌套函数调用:
static getPosition() {
return GeoService._position;
// return _position;
// may work as well
}https://stackoverflow.com/questions/57790889
复制相似问题