【发布时间】:2020-10-28 20:04:30
【问题描述】:
我正在通过https://www.w3.org/TR/orientation-event/ 和https://developer.mozilla.org/en-US/docs/Web/API/Detecting_device_orientation 学习如何在 React Native 应用程序中使用设备方向传感器。
我的 App.js:
const App = () => {
window.addEventListener("deviceorientation", onOrientationChanged,true);
function onOrientationChanged(event) {
var absolute = event.absolute;
var alpha = event.alpha;
var beta = event.beta;
var gamma = event.gamma;
console.log(beta);
}
return (
<>
<ScrollView
contentInsetAdjustmentBehavior="automatic">
<Text>kfkasdfasd</Text>
</ScrollView>
</>
);
};
运行此结果:
TypeError: window.addEventListener is not a function. (In 'window.addEventListener("deviceorientation", onOrientationChanged, true)', 'window.addEventListener' is undefined)
我无法用谷歌搜索任何有同样问题的人,所以当我遵循文档并且没有添加任何额外内容时,我对如何解决这个问题感到困惑。
【问题讨论】: