要将 PWA 集成到您的项目中,您需要执行以下步骤:
安装这两个包
"@angular/pwa": "0.12",
"@angular/service-worker": "^7.2.12"
在 angular.json 文件中添加这样的 serviceWorker 配置
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"serviceWorker": true
}
}
像这样在angular.json的同级创建ngsw-config.json
{
"index": "/index.html",
"assetGroups": [
{
"name": "app",
"installMode": "prefetch",
"resources": {
"files": [
"/assets/favicon.ico",
"/*.css",
"/*.js"
],
"urls": [
"https://cdnjs.cloudflare.com/**"
]
}
}, {
"name": "assets",
"installMode": "lazy",
"updateMode": "prefetch",
"resources": {
"files": [
"/assets/**",
"/*.(eot|svg|cur|jpg|png|webp|gif|otf|ttf|woff|woff2|ani)"
]
}
}
]
}
最后在您的 app.module.ts 中添加导入部分
ServiceWorkerModule.register("ngsw-worker.js", { enabled: environment.production })