当前位置:首页 > VUE

vue实现pwa

2026-03-07 10:17:14VUE

使用 Vue CLI 实现 PWA

Vue CLI 内置了 PWA 插件(@vue/cli-plugin-pwa),可以快速将 Vue 项目转换为 PWA。以下是具体实现步骤:

安装 Vue CLI(如已安装可跳过):

npm install -g @vue/cli

创建 Vue 项目:

vue create my-pwa-app

添加 PWA 插件:

vue add pwa

配置 PWA 参数

vue.config.js 中配置 PWA 选项:

vue实现pwa

module.exports = {
  pwa: {
    name: 'My PWA App',
    themeColor: '#4DBA87',
    msTileColor: '#000000',
    appleMobileWebAppCapable: 'yes',
    appleMobileWebAppStatusBarStyle: 'black',
    workboxPluginMode: 'GenerateSW',
    workboxOptions: {
      skipWaiting: true,
      clientsClaim: true,
    }
  }
}

注册 Service Worker

main.js 中注册 Service Worker:

import { register } from 'register-service-worker'

if (process.env.NODE_ENV === 'production') {
  register(`${process.env.BASE_URL}service-worker.js`, {
    ready() {
      console.log('App is being served from cache by a service worker.')
    },
    registered() {
      console.log('Service worker has been registered.')
    },
    cached() {
      console.log('Content has been cached for offline use.')
    },
    updatefound() {
      console.log('New content is downloading.')
    },
    updated() {
      console.log('New content is available; please refresh.')
    },
    offline() {
      console.log('No internet connection found. App is running in offline mode.')
    },
    error(error) {
      console.error('Error during service worker registration:', error)
    }
  })
}

创建 Manifest 文件

public 目录下创建 manifest.json

{
  "name": "My PWA App",
  "short_name": "PWA",
  "theme_color": "#4DBA87",
  "background_color": "#ffffff",
  "display": "standalone",
  "scope": "/",
  "start_url": "/",
  "icons": [
    {
      "src": "img/icons/android-chrome-192x192.png",
      "sizes": "192x192",
      "type": "image/png"
    },
    {
      "src": "img/icons/android-chrome-512x512.png",
      "sizes": "512x512",
      "type": "image/png"
    }
  ]
}

添加离线提示

App.vue 中添加离线状态检测:

vue实现pwa

mounted() {
  window.addEventListener('online', this.updateOnlineStatus)
  window.addEventListener('offline', this.updateOnlineStatus)
},
methods: {
  updateOnlineStatus() {
    this.isOnline = navigator.onLine
    if (!this.isOnline) {
      // 显示离线提示
    }
  }
}

构建和部署

构建生产环境版本:

npm run build

部署生成的 dist 目录到支持 HTTPS 的服务器,PWA 需要 HTTPS 才能正常工作。

测试 PWA 功能

使用 Lighthouse 工具测试 PWA 功能:

  1. 在 Chrome 中打开开发者工具
  2. 切换到 Lighthouse 标签
  3. 勾选 PWA 选项
  4. 运行测试

高级配置选项

对于更高级的缓存策略,可以修改 workboxOptions

workboxOptions: {
  runtimeCaching: [
    {
      urlPattern: /^https:\/\/api\.example\.com\/.*/,
      handler: 'NetworkFirst',
      options: {
        cacheName: 'api-cache',
        expiration: {
          maxEntries: 10,
          maxAgeSeconds: 60 * 60 * 24 // 1 day
        }
      }
    }
  ]
}

标签: vuepwa
分享给朋友:

相关文章

vue实现图片轮播

vue实现图片轮播

使用 Swiper 实现图片轮播 安装 Swiper 依赖 npm install swiper 在 Vue 组件中引入 Swiper <template> <div…

vue实现异步

vue实现异步

Vue 实现异步操作的常见方法 Vue 提供了多种方式来处理异步操作,包括数据获取、事件处理等场景。 使用 async/await 在 Vue 方法中可以直接使用 async/await 语法处理异…

vue实现123456

vue实现123456

Vue 实现数字 123456 的显示 在 Vue 中显示数字 123456 非常简单,可以通过以下方法实现: 方法一:直接在模板中显示 <template> <div>…

实现vue cli

实现vue cli

安装 Vue CLI 确保系统中已安装 Node.js(版本建议 12.x 或更高)。通过 npm 全局安装 Vue CLI: npm install -g @vue/cli 安装完成后验证版本:…

vue实现border样式

vue实现border样式

实现边框样式的方法 在Vue中实现边框样式可以通过多种方式完成,包括内联样式、CSS类绑定、动态样式绑定等。以下是几种常见的方法: 内联样式绑定 通过style属性直接绑定样式对象,适用于简单的边…

vue如何实现分离

vue如何实现分离

Vue 实现组件分离的方法 Vue 可以通过多种方式实现组件分离,提升代码的可维护性和复用性。以下是几种常见的方法: 单文件组件 (SFC) Vue 的单文件组件(.vue 文件)将模板、脚本和样式…