7

当我使用 angular dotnet new 命令创建 asp.net 核心项目时,不会将 angular.json 文件(angular 6)添加到项目中,因此我们无法使用 angular cli。如果我手动添加 angular.json 文件,项目会给出以下异常!?

Workspace needs to be loaded before it is used. Error: Workspace needs to be loaded before it is used. at Workspace._assertLoaded (D:\NetAngular\node_modules\@angular-devkit\core\src\workspace\workspace.js:59:19) at Workspace.getProjectByPath (D:\NetAngular\node_modules\@angular-devkit\core\src\workspace\workspace.js:103:14) at Object.getPackageManager (D:\NetAngular\node_modules\@angular\cli\utilities\config.js:97:35) at UpdateCommand.runSchematic (D:\NetAngular\node_modules\@angular\cli\models\schematic-command.js:74:38) at UpdateCommand.<anonymous> (D:\NetAngular\node_modules\@angular\cli\commands\update.js:70:25) at Generator.next (<anonymous>) at D:\NetAngular\node_modules\@angular\cli\commands\update.js:7:71 at new Promise (<anonymous>) at __awaiter (D:\NetAngular\node_modules\@angular\cli\commands\update.js:3:12) at UpdateCommand.run (D:\NetAngular\node_modules\@angular\cli\commands\update.js:69:16)
4

8 回答 8

11

尤里卡......

dotnet new angular comman 不会在应用程序的根目录上创建 angular.json 文件。在某些网站上,他们说使用后

npm install --save--dev @angular/cli@latest

命令使用

npm update @angular/cli

这会将您的 angular-cli.json 文件迁移到 angular.json 文件!!!但这里的重点是没有 angular-cli.json 文件,也没有 angular.json。

我通过创建我自己的 angular.json 文件解决了这个问题(你可以用你的项目名称更改 vega)

 {
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "vega": {
      "root": "",
      "sourceRoot": "ClientApp",
      "projectType": "application",
      "prefix": "app",
      "schematics": {},
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "ClientApp/dist/vega"
          },
          "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
            }
          }
        }
      }
    }
  }
}

之后,我能够使用 angular cli 命令并创建组件和服务。

快乐的代码

于 2018-05-10T11:11:33.927 回答
11

我遇到了同样的错误,是的 angular.json 是问题所在。当您手动添加外部 css 路径时,可能会发生此错误。我在引用引导 css 时错过了逗号。

在样式数组中添加自定义 CSS 时,我错过了逗号(,)

错误文件:

如果您在
“../node_modules/bootstrap/dist/bootstrap.min.css”之后看到下面的代码逗号丢失。

"styles": [
          "../node_modules/bootstrap/dist/bootstrap.min.css"
          "src/styles.css"

        ]

解决方案: 添加逗号

"styles": [
          "../node_modules/bootstrap/dist/bootstrap.min.css",
          "src/styles.css"

        ]

命令行错误

于 2018-05-29T05:07:57.047 回答
9

同样的错误。试过卸载重装升级,ng还是报这个错误信息。

删除一个空的 ~/.angular-config.json 文件后,一切都恢复正常了。

于 2018-11-07T10:17:37.313 回答
3

确认您对 angular.json 进行了一些更改。如果您的 angular.json 文件中有语法问题,则会发生此错误。我会说这是意外行为,应该改为说文件中存在语法问题。

问题

于 2018-07-20T07:34:24.937 回答
1

在 之后添加逗号后buildOptimizer:true,它工作正常。

"configurations": {
        "production": {
          "optimization": true,
          "outputHashing": "all",
          "sourceMap": false,
          "extractCss": true,
          "namedChunks": false,
          "aot": true,
          "extractLicenses": true,
          "vendorChunk": false,
          "buildOptimizer": true //I missed to put comma here...
          "fileReplacements": [
            {
              "replace": "src/environments/environment.ts",
              "with": "src/environments/environment.prod.ts"
            }
          ]
        }
      }
于 2018-07-21T10:31:17.387 回答
1

我确实更改了我的 angular.json 文件,但我没有看到任何错误。在仔细检查了逗号和我用编码(UTF-8)保存的所有内容之后,然后格式化了文档(右键单击并格式化文档),删除了 10 条冗余行。之后,我可以运行 angular cli update 以及命令。

于 2019-01-29T15:14:41.320 回答
0

我有类似的错误:

"An unhandled exception occurred: Workspace config file cannot be loaded: ///Projects///project/angular.json Schema validation failed with the following errors: Data path "" should NOT have additional properties(scripts). See "/private/var/folders/f8/cq9wj86n3gbfj8h64f8jlx2m0000gn/T/ng-5TCBsc/angular-errors.log" for further details."

It was because of extra 'scripts' key I added in angular.json earlier. Removed that and it worked!

于 2019-10-30T16:51:50.013 回答
0

I had the same error,This error can occur while:

  1. it may be from the @angular/cli version.
  2. missed the comma char at end of line in angular.json or tsconfig.json.

  3. unicode of the angular.json or tsconfig.json file (will be UTF-8).

  4. 将在or处存在comment线。(在我的情况下,有评论行)。angular.jsontsconfig.jsontsconfig.json
{

  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    //"outDir": "./dist/out-tsc", // comment line caused the error. I removed this line
    "outDir": "../content/ManagmentJob",
    "sourceMap": true,
    "declaration": false,
    "module": "es2015",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    ...
}
于 2020-03-14T05:57:45.083 回答