运行build --prod时,不会生成任何Service Worker文件ngsw.json/ngsw-worker.json.角度5 [英] No Service worker files ngsw.json / ngsw-worker.json generated when running build --prod. Angular 5

查看:105
本文介绍了运行build --prod时,不会生成任何Service Worker文件ngsw.json/ngsw-worker.json.角度5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用angular 5.1.2 Angular-cli 1.6.3

Using angular 5.1.2 Angular-cli 1.6.3

所有文件都很原始.dist文件夹已创建

all files are pretty vanilla. dist folder is created

我的ngsw-config.json

My ngsw-config.json

{
"index": "/index.html",
"assetGroups": [
    {
        "name": "app",
        "installMode": "prefetch",
        "resources": {
            "files": [
                "/favicon.ico",
                "/index.html"
            ],
            "versionedFiles": [
                "/*.bundle.css",
                "/*.bundle.js",
                "/*.chunk.js"
            ]
        }
    },
    {
        "name": "assets",
        "installMode": "lazy",
        "updateMode": "prefetch",
        "resources": {
            "files": [
                "/assets/**"
            ]
        }
    }]   

}

我的app.module.ts使用

my app.module.ts uses

  ServiceWorkerModule.register('ngsw-worker.js', { enabled: environment.production })

使用香草environment.ts和environment.prod.ts

am using vanilla environment.ts and environment.prod.ts

main.ts

import './polyfills.ts';
import {enableProdMode} from '@angular/core';
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';

import {AppModule} from './app/app.module';
import {environment} from './environments/environment';

if (environment.production) {
enableProdMode();
}

platformBrowserDynamic().bootstrapModule(AppModule)
.then(() => {
if ('serviceWorker' in navigator) {
    navigator.serviceWorker.register('ngsw-worker.js');
}

})
.catch(err => console.log(err));

不确定要检查什么

推荐答案

是否将 serviceWorker 属性添加到 .angular-cli.json中的 apps 第一项中?如果没有,则不会创建angular-cli.

Do you add serviceWorker property to apps first entry in .angular-cli.json? If not, angular-cli not built it.

"apps": [
  {
    ...
    "serviceWorker": true
  }
}


EDIT (截至2018年12月), .angular-cli.json 现在为 angular.json ,并且现在,serviceWorker的格式为:


EDIT As of December 2018, .angular-cli.json is now angular.json, and the format for the serviceWorker is now:

{
    ...
    "projects": {
         "my-project-name": {
             "architect": {
                 "build": {
                     "configurations": {
                        "production": {
                           "serviceWorker": true
                        }
                     }
                 }
             }
         }
    }

您需要为每个需要服务工作者的配置指定serviceWorker:true.

You'll need to specify serviceWorker: true for every configuration where you want the service worker.

这篇关于运行build --prod时,不会生成任何Service Worker文件ngsw.json/ngsw-worker.json.角度5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆