无法在角度js中配置服务工作者

时间:2018-06-13 06:50:57

标签: angular progressive-web-apps angular6 angular-service-worker

我正在制作一个Angular渐进式应用程序。我正在与服务工作者一起工作并在CLI上出错 -

Error: Expected to find an ngsw-config.json configuration file in the /home/nancy/Desktop/beautyOfSoul folder. Either provide one or disable Service Worker in your angular.json configuration file.

我正在处理Angular 6.我知道,angular.json文件应该包含"serviceWorker": true但我在angular.json文件中找不到任何类似的内容,我可以在其中编写"serviceWorker": true

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

我的angular.json文件如下:

  {
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "beautyOfSoul": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "prefix": "app",

      "schematics": {
        "@schematics/angular:component": {
          "styleext": "scss"
        }
      },
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/beautyOfSoul",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.app.json",
            "assets": [
              "src/favicon.ico",
              "src/assets", 
  "src/assets/manifest.json"
            ],


            "styles": [
              "src/styles.scss"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true      
           }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "beautyOfSoul:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "beautyOfSoul:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "beautyOfSoul:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.spec.json",
            "karmaConfig": "src/karma.conf.js",
            "styles": [
              "src/styles.scss"
            ],
            "scripts": [],
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ]
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "src/tsconfig.app.json",
              "src/tsconfig.spec.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    },
    "beautyOfSoul-e2e": {
      "root": "e2e/",
      "projectType": "application",
      "architect": {
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "beautyOfSoul:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "beautyOfSoul:serve:production"
            }
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": "e2e/tsconfig.e2e.json",
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    }
  },

  "defaultProject": "beautyOfSoul"
}

1 个答案:

答案 0 :(得分:2)

Here is the official doc。您可以运行以下命令,该命令将设置将您的应用程序转换为PWA所需的文件和库。

ng add  @angular/pwa --project *project-name*

运行此命令时,除了配置文件更改和安装库之外,

1)它会在" src" 文件夹下(不在资源中)添加 manifest.json,最好将其保留为默认方式,尽管您可以尝试映射它从那里。

2)它会在项目的根目录(src的父目录)中添加 ngsw-config.json。这必须在这个位置从Angular 6开始(在5中,它在src中我猜它在6中被更改为根文件夹)在你的情况下找不到这个文件导致了这个问题。要么在这里移动现有的一个,要么删除现有的一个,让上面的一个为你创建它(这是更好的)。