简体   繁体   English

Angular 6.1.2 PWA 不工作

[英]Angular 6.1.2 PWA not working

I tried all the solutions on the internet on how to add PWA to an Angular project, but still in Chrome Dev Tools, there are no service worker registered.我尝试了互联网上关于如何将 PWA 添加到 Angular 项目的所有解决方案,但仍然在 Chrome Dev Tools 中,没有注册 service worker。

I did run ng add @angular/pwa , with ng build --prod and http-server -o and still no service worker registered.我确实运行了ng add @angular/pwa ,使用ng build --prodhttp-server -o ,但仍然没有注册服务工作者。 It also does not add a @angular/service-worker package and also no Manifest.json file like it should as indicated all over the internet.它还没有添加@angular/service-worker包,也没有像 Internet 上显示的那样的Manifest.json文件。

I also tried creating a new project with PWA pre-installed with ng new myProject --service-worker , also not working.我还尝试使用预装了ng new myProject --service-worker PWA 创建一个新项目,但也无法正常工作。

I even tried registering the service working like below:我什至尝试注册服务,如下所示:

if ( 'serviceWorker' in navigator ) { window.addEventListener('load', function() { navigator.serviceWorker.register('/service-worker.js'); });

A side note: When I run ng add @angular/pwa I get a message "Path '/ngsw-config.json' already exist", so I found out that file is under @schematics package.旁注:当我运行ng add @angular/pwa我收到一条消息“路径 '/ngsw-config.json' 已经存在”,所以我发现该文件在@schematics包下。 The script only adds a @angular/pwa package, which does not corrolate with the Angular PWA docs.该脚本仅添加了一个@angular/pwa包,它与 Angular PWA 文档无关。


Here is my environment :这是我的环境:

Angular 6.1.2
Angular CLI: 6.1.3
Node 8.11.3
NPM  5.6.0

What do I need to do to get a plain PWA Angular project?我需要做什么才能获得一个普通的 PWA Angular 项目?

It is the new version of @angular/pwa package that has a few bugs.这是@angular/pwa包的新版本,有一些错误。 So running ng add @angular/pwa@0.6.8 worked perfectly for me.所以运行ng add @angular/pwa@0.6.8对我来说非常有效。

To test the service worker locally: If you have Firebase added to your project (hosting), you can run ng build --prod and then firebase serve .在本地测试服务工作者:如果您已将 Firebase 添加到您的项目(托管),您可以运行ng build --prod然后ng build --prod firebase serve When you don't have Firebase, you can run ng build --prod , cd into the dist folder (depending on your config) and then run http-server -o .当您没有 Firebase 时,您可以运行ng build --prod , cd 进入 dist 文件夹(取决于您的配置),然后运行http-server -o If you don't have http-server module, install it by running npm i -g http-server如果您没有http-server模块,请通过运行npm i -g http-server安装它

Try using the Angular Console: https://angularconsole.com/尝试使用 Angular 控制台: https : //angularconsole.com/

It abstracts away many of the need to know logic that the cli has.它抽象了许多了解 cli 具有的逻辑的需要。

It's in beta but it should help you create the base for your PWA.它处于测试阶段,但它应该可以帮助您为 PWA 创建基础。 Give it a try it's build from the Nrwl Team that also builds Nx which is an enhancement to the angular/cli using schematics试一试它是由 Nrwl 团队构建的,该团队还构建了 Nx,这是使用原理图对 angular/cli 的增强

I had the same issue.我遇到过同样的问题。 The problem was than this command: ng add @angular/pwa问题不在于这个命令:ng add @angular/pwa

didn't add module @angular/pwa to package.json dependencies.没有将模块 @angular/pwa 添加到 package.json 依赖项。

I decided it so.我是这样决定的。

First I run ng add @angular/pwa Then I did: npm install @angular/pwa首先我运行 ng add @angular/pwa 然后我做了: npm install @angular/pwa

And it all works!这一切都有效!

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

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