我正在按照 ScreenOrientation.lock()
的文档操作,但我无法让它按我想要的方式工作。
https://developer.mozilla.org/en-US/docs/Web/API/ScreenOrientation/lock
在 Chrome 桌面上调用 window.screen.orientation.lock("portrait");
时出现错误 screen.orientation.lock() 在此设备上不可用。
作为 Uncaught Error 。无论如何检查设备是否支持锁定?
我已经将 "orientation":"portrait"
放入 manifest.json
但这只是默认方向而不是锁。
https://www.w3.org/TR/appmanifest/#orientation-member
旁注:
我在理解如何调用该方法时遇到了一些困难。如果有人发现此线程,则会出现错误示例:
ScreenOrientation.prototype.lock("portrait");
window.ScreenOrientation.prototype.lock("portrait");
导致此异常:
Unhandled Rejection (TypeError): Failed to execute 'lock' on 'ScreenOrientation': Illegal invocation
(ScreenOrientation as any).lock("portrait");
导致此异常:
TypeError: ScreenOrientation.lock is not a function
最佳答案
window.screen.orientation.lock()
返回 Promise . Promise 将调用您提供给链式 then()
方法的函数之一,具体取决于 Promise 是已解决(成功)还是被拒绝(失败)。
您可以像这样调用 window.screen.orientation.lock()
:
window.screen.orientation
.lock("portrait")
.then(
success => console.log(success),
failure => console.log(failure)
)
您可能希望在 resolve
和 reject
方法中添加一些更有用的东西,但这至少可以捕获拒绝并防止抛出 Unhandled Rejection 错误.
关于javascript - 检查设备是否支持 ScreenOrientation.lock() - Uncaught Error screen.orientation.lock() 在此设备上不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61747071/