打字稿类型BeforeInstallPromptEvent [英] Typescript type BeforeInstallPromptEvent

查看:75
本文介绍了打字稿类型BeforeInstallPromptEvent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 beforeinstallprompt 事件时,我应该使用哪种类型?

What's kind of type I should use when using beforeinstallprompt event?

我尝试了 BeforeInstallPromptEvent 类型,但是给了我一个错误:

I tried BeforeInstallPromptEvent type, but gave me an error:

export class PwaService {
  //promptEvent: BeforeInstallPromptEvent;
  promptEvent;
  constructor(private swUpdate: SwUpdate, platform: PlatformService) {
    if(platform.isBrowser()){
      swUpdate.available.subscribe(event =>  {
        /*if (askUserToUpdate()) {
          window.location.reload();
        }*/
      });
      window.addEventListener('beforeinstallprompt', event => {
        this.promptEvent = event;
      });
    }
  }

  install(): void {
    if(this.promptEvent){
      this.promptEvent.prompt();
    }
  }
}

推荐答案

BeforeInstallPromptEvent 是非标准的Web API,目前仅受Chrome和Android支持.我什至不确定Google是否认为它稳定,但是无论哪种情况,我都不希望很快在TypeScript DOM库中看到正式的类型定义.

The BeforeInstallPromptEvent is a non-standard Web API and currently only supported by Chrome and Android. I'm not even sure whether Google considers it stable yet, but in either case I wouldn't expect to see an official type definition in the TypeScript DOM library any time soon.

不过,您可以自己定义类型,例如在 .d.ts文件中.我使用下面的定义(来自 MDN 的评论)在Chrome 68中足够准确.

However you can define the type yourself, e.g. in a .d.ts file. I use the definition below (comments from MDN), which seems to be accurate enough in Chrome 68.

/**
 * The BeforeInstallPromptEvent is fired at the Window.onbeforeinstallprompt handler
 * before a user is prompted to "install" a web site to a home screen on mobile.
 *
 * @deprecated Only supported on Chrome and Android Webview.
 */
interface BeforeInstallPromptEvent extends Event {

  /**
   * Returns an array of DOMString items containing the platforms on which the event was dispatched.
   * This is provided for user agents that want to present a choice of versions to the user such as,
   * for example, "web" or "play" which would allow the user to chose between a web version or
   * an Android version.
   */
  readonly platforms: Array<string>;

  /**
   * Returns a Promise that resolves to a DOMString containing either "accepted" or "dismissed".
   */
  readonly userChoice: Promise<{
    outcome: 'accepted' | 'dismissed',
    platform: string
  }>;

  /**
   * Allows a developer to show the install prompt at a time of their own choosing.
   * This method returns a Promise.
   */
  prompt(): Promise<void>;

}

这篇关于打字稿类型BeforeInstallPromptEvent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆