- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在研究使用 Vue CLI 搭建的 Vue PWA,默认情况下使用 register-service-worker
用于在 registerServiceWorker.js
中公开 serviceWorker 生命周期方法的包:
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)
}
registerServiceWorker.js
导入到
main.js
:
import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store/index'
import './registerServiceWorker'
Vue.config.productionTip = false
new Vue({
router,
store,
render: h => h(App)
}).$mount('#app')
registerServiceWorker.js
的范围内调用我的 Vuex $store 或 App.vue $refs.components 中的方法,例如
updated () {
// Something like
this.$store.commit('showUpdateNotification')
// OR
this.$refs.updateNotification.show()
}
registerServiceWorker.js
?
最佳答案
在您的 registerServiceWorker.js
中导入 Vuex 商店文件。
IE。
/* eslint-disable no-console */
import { register } from 'register-service-worker'
import appConfig from './app.config.json'
import store from './store'
if (process.env.NODE_ENV === 'production') {
register(`${appConfig.router.baseUrl}service-worker.js`, {
ready() {
console.log(
'App is being served from cache by a service worker.'
)
},
cached() {
console.log('Content has been cached for offline use.')
},
updated() {
console.log('New content is available; please refresh.')
store.commit('showUpdateNotification')
},
offline() {
console.log(
'No internet connection found. App is running in offline mode.'
)
},
error(error) {
console.error('Error during service worker registration:', error)
},
})
}
关于vue.js - 如何从 registerServiceWorker.js 调用 Vue 应用程序 $refs.components 或 Vuex $store 中的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60763119/
我正在尝试创建一个获取 pokemon API 的 react 应用程序,但我遇到了以下问题,即尝试导入错误:“./registerServiceWorker”不包含默认导出(导入为“register
我正在开发 React Web 应用程序,并使用 create-react-app 来部署它。有一个名为 registerServiceWorker.js 的文件,它负责初始化 service Wor
当您使用 create-react-app 引导您的 React 应用程序时有一个名为 registerServiceWorker.js 的文件被导入并在 index.js 中用作: import R
在App.js的末尾,我们放置 export default App; 将 App 声明为 app.js 的默认导出。 然后,当我们将此文件导入到 index.js 时,我们只需输入 //Import
我在尝试编译我的 React 应用程序时遇到了麻烦。当我编译应用程序时,我收到以下错误:找不到模块:无法解析 './registerServiceWorker 这是我的 index.js 文件: im
我是 React 的新手,我想知道以下代码中 registerServiceWorker() 的用途是什么? import React from 'react'; import ReactDOM fr
作为 React 的新手,我想知道是否需要 registerServiceWorker.js。 我问是因为我注意到(在花了无数小时查看之后)它与我的 DOM 上呈现的内容冲突。在我的开发环境中,一切正
当我使用“create-react-app projectname”构建时第一次我在 src 目录中有 registerServiceWorker.js但是突然间,当我构建新项目时,我只得到了 ser
当我想我会使用 Visual Studio Code 来获得一个新的 React JS 应用程序并开始学习它等时。我浏览了 Microsoft 网站 https://code.visualstudio
我正在研究使用 Vue CLI 搭建的 Vue PWA,默认情况下使用 register-service-worker用于在 registerServiceWorker.js 中公开 serviceW
我是一名优秀的程序员,十分优秀!