[英]how to add custom install button for pwa
i want to add custom install
button for my progressive web app within the site.我想在站点内为我的渐进式 Web 应用程序添加自定义install
按钮。 i red many articles and tried the answer provided by them.我红了很多文章并尝试了他们提供的答案。 they use beforeinstallprompt
他们使用beforeinstallprompt
let deferredPrompt;
window.addEventListener('beforeinstallprompt', (e) => {
deferredPrompt = e;
});
but the problem i am facing is i want the button to directly installed the pwa instead of triggering the installation prompt.但我面临的问题是我希望按钮直接安装 pwa 而不是触发安装提示。 is it possible, if so how can i achieve that.有可能吗,如果可以,我该如何实现。 thanks you in advance.提前谢谢你。
The answer is, you can't.答案是,你不能。 According to the manifest spec :根据清单规范:
By design, this specification does not provide developers with an explicit API to "install" a web application.按照设计,本规范不为开发人员提供用于“安装”Web 应用程序的显式 API。
Try below code,试试下面的代码,
Step 1 - Create button or any controller第 1 步 - 创建按钮或任何控制器
<button id="installApp">Install</button>
Step 2 - Add below js code in your scripts noy in serviceworker第 2 步 - 在 serviceworker 的脚本中添加以下 js 代码
let deferredPrompt;
window.addEventListener('beforeinstallprompt', (e) => {
deferredPrompt = e;
});
const installApp = document.getElementById('installApp');
installApp.addEventListener('click', async () => {
if (deferredPrompt !== null) {
deferredPrompt.prompt();
const { outcome } = await deferredPrompt.userChoice;
if (outcome === 'accepted') {
deferredPrompt = null;
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.