属性“wakeLock”类型“Navigator”上不存在;

发布于 2025-01-15 21:09:50 字数 1558 浏览 2 评论 0原文

我需要在我的 Angular 应用程序中使用屏幕唤醒锁定 API
这是我的代码

if ('wakeLock' in navigator) {
         await navigator.wakeLock.request();
        
}

但是,Angular 编译失败并出现以下错误:

Error: src/app/feature/test/test/test.component.ts:20:42 - error TS2339: Property 'wakeLock' does not exist on type 'Navigator'.

Angular 版本和信息

     _                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/
    

Angular CLI: 13.0.3
Node: 16.13.0
Package Manager: npm 8.1.0
OS: win32 x64

Angular: 13.0.2
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... platform-server, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1300.3
@angular-devkit/build-angular   13.0.3
@angular-devkit/core            13.0.3
@angular-devkit/schematics      13.0.3
@angular/cdk                    13.0.3
@angular/cli                    13.0.3
@angular/flex-layout            13.0.0-beta.36
@angular/material               13.0.3
@schematics/angular             13.0.3
rxjs                            6.6.7
typescript                      4.4.4

如何修复打字稿以包含 Screen Wake Lock API 并可以编译代码?

I need to use the Screen Wake Lock API in my angular app
here is my code

if ('wakeLock' in navigator) {
         await navigator.wakeLock.request();
        
}

However, Angular compilation fails with the following error:

Error: src/app/feature/test/test/test.component.ts:20:42 - error TS2339: Property 'wakeLock' does not exist on type 'Navigator'.

Angular version and information

     _                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/
    

Angular CLI: 13.0.3
Node: 16.13.0
Package Manager: npm 8.1.0
OS: win32 x64

Angular: 13.0.2
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... platform-server, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1300.3
@angular-devkit/build-angular   13.0.3
@angular-devkit/core            13.0.3
@angular-devkit/schematics      13.0.3
@angular/cdk                    13.0.3
@angular/cli                    13.0.3
@angular/flex-layout            13.0.0-beta.36
@angular/material               13.0.3
@schematics/angular             13.0.3
rxjs                            6.6.7
typescript                      4.4.4

How can fix typescript to contain Screen Wake Lock API and can compile codes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

誰ツ都不明白 2025-01-22 21:09:50

这就是我为使其工作而所做的:

const anyNav: any = navigator
if ('wakeLock' in navigator) {
  anyNav["wakeLock"].request("screen")
}

我首先尝试添加 DefinelyTyped 定义,该定义修复了编辑器抛出的错误,但它仍然导致编译失败。这成功了,基本上我们将导航器保存到任何类型变量,这允许我们调用任何属性或方法而无需类型检查。

if 语句允许在尝试调用唤醒锁之前检查浏览器是否支持唤醒锁。

This is what I did to make it work:

const anyNav: any = navigator
if ('wakeLock' in navigator) {
  anyNav["wakeLock"].request("screen")
}

I first tried adding the DefinitelyTyped definition which fixed the editor throwing errors, but it was still causing compilation to fail. This did the trick, basically we are saving the navigator to an any type variable which allows us to call any properties or methods without type checking.

The if statement allows for checking that wakelock is supported by the browser before trying to call it.

滥情稳全场 2025-01-22 21:09:50

最后,两周后我找到了一种通过添加DefinitelyTyped来支持这一点的方法。两步可以解决这个问题。

  1. 安装@types/dom-screen-wake-lock
npm i @types/dom-screen-wake-lock -D
  1. 安装该包后,您必须将类型添加到tsconfig.json,例如这
{
...
   "compilerOptions":{
...
         "types":[
...
              "dom-screen-wake-lock"
...
            ]
...
}
...
}

Finally, after two weeks I found a way that supports this by adding the DefinitelyTyped. Two-step can fix this.

  1. Install the @types/dom-screen-wake-lock package
npm i @types/dom-screen-wake-lock -D
  1. After installing the package you must add types to tsconfig.json like this
{
...
   "compilerOptions":{
...
         "types":[
...
              "dom-screen-wake-lock"
...
            ]
...
}
...
}
庆幸我还是我 2025-01-22 21:09:50

检查您的安全上下文,因为 Wakelock 仅在启用 https 的情况下工作: https:// developer.mozilla.org/en-US/docs/Web/API/WakeLock

Check your security context because Wakelock works only with https enabled: https://developer.mozilla.org/en-US/docs/Web/API/WakeLock

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文