Angular 7 - Service Worker 未注册 [英] Angular 7 - service worker not registered
问题描述
我按照https://angular.io/guide/service-worker-getting-started" 使我的应用程序成为 PWA.
I did everything as written in "https://angular.io/guide/service-worker-getting-started" to make my application PWA.
正是使用了这个命令:
ng add @angular/pwa
npm install http-server -g
ng build --prod
http-server -p 8080 -c-1 dist
然后我在 Chrome 上打开了这个网址(隐身)
Then I opened this url on Chrome (in incognito)
当我打开面向开发人员的工具 (f12) > 应用程序 > Service Workers 时,没有可用的 Service Worker,当我在那里设置离线时,网站也无法工作.
When I open tools for developers (f12) > Applications > Service Workers there is no service worker available and website don't work when I set offline there.
有关我的 Angular 应用的其他信息:
Additional info about my angular app:
package.json:(最重要的)
package.json: (the most important ones)
"@angular/core": "^7.0.2",
"@angular/pwa": "^0.10.6",
"@angular/service-worker": "^7.0.4",
"@angular-devkit/build-angular": "^0.10.6",
"@angular/cli": "^7.0.6",
推荐答案
@angular/cli@7.1.4 的 service worker 设置似乎已损坏:
It seems the service worker setup is broken for the @angular/cli@7.1.4:
作为临时解决方案,您可以通过修改文件 src/main.ts 自行手动注册(一旦修复,您可以删除手动 Service Worker 注册.):
As a temporary solution you can register it manually yourself by modifying the file src/main.ts (Once fixed you can remove the manual service worker registration.):
.....
platformBrowserDynamic().bootstrapModule(AppModule).then(() => {
if ('serviceWorker' in navigator && environment.production) {
navigator.serviceWorker.register('/ngsw-worker.js');
}
}).catch(err => console.log(err));
PS:Github 问题已创建:https://github.com/angular/angular-cli/issues/13351
PS: Github issue created: https://github.com/angular/angular-cli/issues/13351
PS:Github 问题 #2 创建:https://github.com/angular/angular-cli/issues/15025
PS: Github issue #2 created: https://github.com/angular/angular-cli/issues/15025
这篇关于Angular 7 - Service Worker 未注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!