简体   繁体   English

无法使用角度cli以角度注册服务工作者

[英]Unable to register service worker in angular using angular cli

I am trying to covert my angular app into a PWA so tried to add service worker using npm install @angular/service-worker --save and ng add @angular/pwa then after that I build the app in production mode using ng build --prod and serve the app using ng serve --open but there in console --> application tab I got manifest.json running fine but service worker is not getting registered and showing nothing in service worker section. 我试图将我的角度应用程序转换为PWA,所以尝试使用npm install @angular/service-worker --saveng add @angular/pwa添加服务工作者然后我使用ng build --prod在生产模式下构建应用程序ng build --prod并使用ng serve --open为应用程序ng serve --open但是在控制台 - >应用程序选项卡中我使manifest.json运行正常但service worker没有注册并且在服务工作者部分中没有显示任何内容。

Here is my src/index.html 这是我的src / index.html
Here is my src/main.ts 这是我的src / main.ts
Here is my angular.json 这是我的angular.json
Here is my ngsw-config.json 这是我的ngsw-config.json
Here is my src/app/app.module.ts 这是我的src / app / app.module.ts

Service Worker run ONLY ON HTTPS domain .. if you test it on localhost it can't work .. maybe try to run your localhost site with angular cli with: 服务工作者ONLY ON HTTPS域上运行..如果您在localhost上测试它无法工作..也许尝试使用angular cli运行您的localhost站点:

ng serve --prod --ssl=true

Hope it helps you 希望它能帮到你

try also in the main.ts file : 也可以在main.ts文件中尝试:

platformBrowserDynamic().bootstrapModule(AppModule)
  .then(() => {
    if ('serviceWorker' in navigator && environment.production) {
      navigator.serviceWorker.register('/ngsw-worker.js', {
        updateViaCache: 'none'
      }).then((sw) => {
        sw.update();
      });
    }
  })
  .catch(err => console.log(err));

and double check your dist folder for all necessary files : 并仔细检查您的dist文件夹中的所有必要文件:

ngsw-worker.js

and

 ngsw.json 

as you see in screentshot 正如你在screentshot中看到的那样

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM