aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/beforeinstallpromptevent/index.html
blob: 7932a6509d15ef3a90e57f319003a148a8350f3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
---
title: BeforeInstallPrompt
slug: Web/API/BeforeInstallPromptEvent
tags:
  - PWA
  - beforeinstallprompt
  - 添加到主屏幕
  - 添加快捷方式
translation_of: Web/API/BeforeInstallPromptEvent
---
<p>{{ ApiRef() }} {{ Non-standard_header }}</p>

<p>在一个用户被提示”安装“一个网站到移动设备的一个主屏幕之前,  <code><strong>BeforeInstallPromptEvent </strong></code>{{domxref("Window.onbeforeinstallprompt")}} 处理程序触发。</p>

<p>该接口继承自{{domxref("Event")}}接口。</p>

<p>{{InheritanceDiagram(700, 60, 20)}}</p>

<h2 id="构造器">构造器</h2>

<dl>
 <dt>{{domxref("new window.BeforeInstallPromptEvent(name, eventInitOptions)")}}</dt>
 <dd>创建一个新的 <code><strong>BeforeInstallPromptEvent</strong></code>.</dd>
</dl>

<h2 id="属性">属性</h2>

<p><em>继承自父类,{{domxref("Event")}}.</em></p>

<dl>
 <dt>{{domxref("BeforeInstallPromptEvent.platform")}} {{readonlyinline}}</dt>
 <dd>返回一个包含了调度事件的平台(s)的 {{domxref("DOMString")}} 数组。这是为希望向用户提供版本选择的user agent提供的,例如,“web”或“play”允许用户在web版本或Android版本之间进行选择。</dd>
 <dt>{{domxref("BeforeInstallPromptEvent.userChoice")}} {{readonlyinline}}</dt>
 <dd>返回一个可以解析为 {{domxref("DOMString")}}{{jsxref("Promise")}} ,其值为 'installed' 或 'dismissed',用以判断用户是否选择安装该PWA。</dd>
</dl>

<h2 id="方法">方法</h2>

<dl>
 <dt>{{domxref("BeforeInstallPromptEvent.prompt()")}} </dt>
 <dd>立即弹出安装提示。允许开发者按照自己选择的时间弹出安装提示。该方法返回 {{jsxref("Promise")}}</dd>
</dl>

<h2 id="例子">例子</h2>

<pre class="notranslate">window.addEventListener("beforeinstallprompt", function(e) {
  // log the platforms provided as options in an install prompt
  console.log(e.platforms); // e.g., ["web", "android", "windows"]
  e.userChoice.then(function(outcome) {
    console.log(outcome); // either "installed", "dismissed", etc.
  }, handleError);
});
</pre>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

{{Compat("api.BeforeInstallPromptEvent")}}