在角度项目中使用Workbox有多种方法
workbox-webpack-plugin ,所有关于如何使用它的资料都很详细 here . 你可以使用 GenerateSW 要生成完全工作的服务工作者、运行时缓存等,还有 InjectManifest
workbox-webpack-plugin
GenerateSW
InjectManifest
顺便说一句,如果你愿意自己编写而不是使用插件,你仍然可以在Angular项目中使用straight js编写服务人员,并使用Angular ServiceWorkerModule 要将其注册到您的应用程序,请使用 register() 函数(这应该在 imports 您还需要使用将workbox库导入到service worker中 importScripts('https://storage.googleapis.com/workbox-cdn/releases/3.6.2/workbox-sw.js');
ServiceWorkerModule
register()
imports
importScripts('https://storage.googleapis.com/workbox-cdn/releases/3.6.2/workbox-sw.js');