Angular 7-未注册服务工作者 [英] 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)>应用程序>服务工作者时,没有可用的服务工作者,而当我在其中设置为离线时,网站也无法正常工作.
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.
有关我的角度应用程序的其他信息:
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,服务工作者设置似乎已损坏:
It seems the service worker setup is broken for the @angular/cli@7.1.4:
作为一个临时解决方案,您可以通过修改文件 src/main.ts 来手动注册(一旦修复,您可以删除手动服务人员注册.):
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-未注册服务工作者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!