我尝试在移动网络中使用“加速计”之类的html传感器api, 在纯html env中可以正常工作,但是在Vue中则失败。
我使用此示例代码可以在纯HTML上运行 https://developer.mozilla.org/en-US/docs/Web/API/Sensor_APIs
并启用Chrome标志传感器: chrome:/ flags /#enable-generic-sensor-extra-classes
在html中,我使用此
let accelerometer = null;
try {
accelerometer = new Accelerometer({ referenceFrame: 'device' });
accelerometer.addEventListener('error', event => {
// Handle runtime errors.
if (event.error.name === 'NotAllowedError') {
// Branch to code for requesting permission.
} else if (event.error.name === 'NotReadableError' ) {
console.log('Cannot connect to the sensor.');
}
});
accelerometer.addEventListener('reading', () => reloadOnShake(accelerometer));
accelerometer.start();
} catch (error) {
// Handle construction errors.
if (error.name === 'SecurityError') {
// See the note above about feature policy.
console.log('Sensor construction was blocked by a feature policy.');
} else if (error.name === 'ReferenceError') {
console.log('Sensor is not supported by the User Agent.');
} else {
throw error;
}
}
但是在Vue中,我有这样的错误
Sensor is not supported by the User Agent.
当仅声明:let accelerometer = new Accelerometer()时,将得到如下的错误信息
Uncaught (in promise) ReferenceError: Accelerometer is not defined
at eval
如何在Vue env中获取加速度计值。