angular - 无法在 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"
}
解决方案
这是官方文档。您可以运行以下命令,该命令将设置将您的应用程序转换为 PWA 所需的文件和库。
ng add @angular/pwa --project *project-name*
运行此命令时,除了更改配置文件和安装库外,
1)它会在“src”文件夹下添加manifest.json(不在资产中),最好保持默认方式,尽管您可以尝试从那里映射它。
2)它会在项目的根目录(src的父目录)中添加ngsw-config.json。这必须在从 Angular 6 开始的这个位置(在 5 中,它在 src 中,我猜它在 6 中已更改为根文件夹)在您的情况下找不到该文件,这导致了问题。要么把你现有的移到这里,要么删除现有的,让上面的一个为你创建它(这更好)。
推荐阅读
- node.js - 使用节点 js 套接字在 laravel API 中进行实时跟踪
- javascript - 窗口属性变得未定义
- python - 分析混淆矩阵的准确度结果
- python - Pygame:显示不更新
- sparse-matrix - 如何将蓝图添加到 tidymodels 中的 workflow_set
- python - 我正在创建的蒙版正在剪切我要粘贴的图像
- javascript - JS/PHP <--> 嵌入式 PHP HTML
- php - 如何在单个产品页面上复制添加到购物车按钮,但重定向到另一个页面?
- sql - 有人可以向我解释一下 BIDENT 是什么吗
- reporting-services - Power bi 报表生成器中的多个页面大小和方向设置