It is very simple to introduce PWA into the Vue project. The steps are as follows: 1. Install dependenciesvue add @vue/pwa Since the add keyword is used, some files will be created in the project after a successful installation. If the project uses git, you can easily see the file changes: A registerServiceWorker.js file will be generated in the src folder and imported in main.js. This file automatically generates the code for registering the service worker. The code of registerServiceWorker.js is as follows: 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.\n' + 'For more details, visit https://goo.gl/AFskqB' ) }, 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) } }) } 2. Configure pwa in the vue.config.js file:module.exports = { pwa: workboxOptions: { skipWaiting: true, clientsClaim: true, importWorkboxFrom: 'local', importsDirectory: 'js', navigateFallback: '/', navigateFallbackBlacklist: [/\/api\//] } } } 3. Manually add the manifest.json file to the public directory of the project. The content of manifest.json is as follows:{ "short_name": "Application short name", // Will be displayed under the mobile desktop application icon in the future "name": "Application full name", // Will be displayed under the computer desktop application icon in the future "icon": [ { "src": "./img/icons/android-chrome-192x192.png", "sizes": "192x192", "type": "image/png" }, { "src": "./img/icons/android-chrome-512x512.png", "sizes": "512x512", "type": "image/png" } ], // Desktop icons, an array, pay attention to the image size and format "start_url": "index.html", // URL when the application starts "display": "standalone", "background_color": "#080403", "theme_color": "#080403" } The display field indicates the display mode. The specific parameters and descriptions are as follows:
4. Add the following code to the index.html file:<meta name="theme-color" content="#080403"> <link rel="manifest" href="manifest.json" rel="external nofollow" > Next is the moment to witness the miracle. Execute npm run build to check whether the manifest.json file is introduced in the index.html file under dist, and then see if the corresponding configuration is generated. If so, congratulations, your first pwa project is completed! The above are the details of the steps to introduce PWA into the Vue project. For more information about introducing PWA into the Vue project, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: Mybatis+mysql uses stored procedures to generate serial number implementation code
>>: Nginx content cache and common parameter configuration details
[Problem description] On the application side, th...
The test environment is set up with a mariadb 5.7...
Today I received a disk alarm exception. The 50G ...
I had been working on the project before the New ...
In Node.js, a .js file is a complete scope (modul...
The BGCOLOR attribute can be used to set the back...
Table of contents Preface Confusing undefined and...
Preface: In the previous article, we mainly intro...
Table of contents Introduction Install Display Fi...
Note: This demo is tested in the mini program env...
Tag type (display mode) HTML tags are generally d...
Is there any way to remove spaces from a certain ...
1 Download MySQL8 from the official website and i...
Automatic backup of MySQL database using shell sc...
The image can be saved on hub.docker.com, but the...