无法在 angular js 中配置服务工作者
Unable to configure service worker in angular js
我正在制作 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"
}
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 中更改为根文件夹)在您的案例中找不到导致问题的文件。将现有的移动到此处或删除现有的并让上面的为您创建它(更好)。
我正在制作 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"
}
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 中更改为根文件夹)在您的案例中找不到导致问题的文件。将现有的移动到此处或删除现有的并让上面的为您创建它(更好)。