【问题标题】:How to make a installable PWA using vuejs?如何使用 vuejs 制作可安装的 PWA?
【发布时间】:2021-02-02 00:08:19
【问题描述】:

我想通过 chrome/firefox 使用按钮 (显示 pwa 安装提示)使我的 vue 应用程序可安装

<button @click="install">install</button>

var deferredPrompt
methods: {
  install() {
      deferredPrompt.prompt()
      deferredPrompt.userChoice.then((choiceResult) => {
          if (choiceResult.outcome === 'accepted') {
              console.log('User accepted the A2HS prompt')
          } else {
              console.log('User dismissed the A2HS prompt')
          }
          deferredPrompt = null
      })  
  }    
}

一旦我构建了应用程序及其在线,我单击按钮并在控制台上得到 deferredPrompt null。 我缺少什么?

【问题讨论】:

    标签: vue.js vuejs2 progressive-web-apps


    【解决方案1】:

    您的window 对象上似乎没有beforeinstallprompt 事件侦听器。

    在 vuejs 中,您需要在 created() 事件中执行此操作,例如:

    export default {
      name: "App",
      data() {
        return {
          deferredPrompt: null
        };
      },
      created() {
        window.addEventListener("beforeinstallprompt", (e) => {
          e.preventDefault();
          // Stash the event so it can be triggered later.
          this.deferredPrompt = e;
        });
      },
      methods: {
        async install() {
          this.deferredPrompt.prompt();
        }
      }
    };
    

    此外,您应该仅在触发beforeinstallprompt 事件并且设置deferredPrompt 时才显示安装按钮。否则,按钮将可见但不执行任何操作。

    查看https://medium.com/js-dojo/promoting-your-vue-js-pwa-installation-6bd112342c19了解更多详情。

    【讨论】:

    • 第 64 行是 this.deferredPrompt.prompt(); @PeteLe 我正在使用您的确切代码
    猜你喜欢
    • 1970-01-01
    • 2022-06-21
    • 1970-01-01
    • 2020-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多