简体   繁体   English

无法在Angular JS中配置服务人员

[英]Unable to configure service worker in angular js

I am making an Angular progressive application. 我正在做一个Angular渐进式应用程序。 I am doing with service workers and getting an error on CLI - 我在与服务人员一起做,并且在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.

I am dealing with Angular 6. I know that, angular.json file should contain "serviceWorker": true but I can not find any like below in my angular.json file where I can write "serviceWorker": true . 我正在处理Angular6。我知道,angular.json文件应包含"serviceWorker": true serviceWorker "serviceWorker": true但我在我的angular.json文件中找不到以下类似内容,可以在其中写入"serviceWorker": true

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

My angular.json file is as below: 我的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"
}

Here is the official doc . 这是官方文件 You can run the below command which will setup the files and libs needed to convert your app into PWA. 您可以运行以下命令,该命令将设置将应用程序转换为PWA所需的文件和库。

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

When you run this command, apart from config file changes and installing libs, 运行此命令时,除了更改配置文件和安装库之外,

1) it will add manifest.json just under "src" folder(not in assets) and its better to keep it the default way, though you can try to map it from there. 1)它将在“ src”文件夹(不在资产中) 添加manifest.json,最好将其保留为默认方式,尽管您可以尝试从那里映射它。

2) it will add ngsw-config.json in the root of the project (parent directory of src). 2)它将在项目的根目录(src的父目录)中添加ngsw-config.json This has to be in this location starting Angular 6(in 5, it was in src I guess which was changed to root folder in 6) This file was not found in your case which is causing the issue. 这必须是从Angular 6开始的位置(在5中,它是在src中,我想它在6中已更改为根文件夹)在您的情况下未找到此文件,这引起了问题。 Either move your existing one here or delete existing one and let the above one create it for you(which is better). 将您现有的一个移到此处或删除现有的一个,然后让上面的一个为您创建(更好)。

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

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