【问题标题】:Error while running ASP.NET Core with Angular Template使用 Angular 模板运行 ASP.NET Core 时出错
【发布时间】:2020-03-06 07:03:43
【问题描述】:

我正在尝试在新机器上运行我的工作项目。 Angular 应用程序使用 ng serve 单独运行良好,但是当我尝试从 Visual Studio 朗姆酒整个应用程序时,我收到以下错误:

AggregateException:发生一个或多个错误。 (发生了一个或多个错误。(NPM 脚本 'start' 退出,但未指示 Angular CLI 正在侦听请求。错误输出为:npm ERR!path C:\Windows\System32\package.json

我尝试了以下方法,但没有帮助:

  • 选择文件夹 ClientApp 中的 package.json 文件,然后将属性 Copy to Output Directory 更改为 Copy always。
  • 删除 package.lock.json 并运行 npm install。
  • 我的 package.json 中没有 –EXTRACT-CSS。是"start": "ng serve
  • 遵循https://github.com/silvairsoares/Angular-with-Asp.Net-Core/wiki 中提到的所有步骤

【问题讨论】:

标签: angular asp.net-core


【解决方案1】:

有两种方法可以在 aspnet 核心应用程序中运行 Angular 应用程序。假设您将应用程序作为控制台应用程序运行(不使用 IIS Express)并且 Angular 应用程序位于 ClientApp 文件夹中,您可以检查以下方法。

如果你想独立运行 Angular 服务器 将此添加到 Startup.cs 内的 Configure() 方法中

app.UseSpa(spa =>
        {

            spa.Options.SourcePath = "ClientApp";

            if (env.IsDevelopment())
            {
                spa.UseProxyToSpaDevelopmentServer("http://localhost:4200"); // Use this instead to use the angular cli server
            }
        });

如果你想同时在 aspnet core app 里面启动 将此添加到 Startup.cs 内的 Configure() 方法中

app.UseSpa(spa =>
        {
            spa.Options.SourcePath = "ClientApp";

            if (env.IsDevelopment())
            {
                spa.UseAngularCliServer(npmScript: "start");
                spa.Options.StartupTimeout = TimeSpan.FromSeconds(60); // Increase the timeout if angular app is taking longer to startup

            }
        });

在此之前,请确保 npm install 在您的客户端应用文件夹中。

【讨论】:

  • 非常感谢 :) UseProxyToSpaDevelopmentServer 解决了问题
【解决方案2】:

如果你的项目代码如下,

"scripts": {
    "ng": "ng",
    "start": "ng serve --extract-css",
    "build": "ng build --extract-css",
    ...
}

修复跟随

"scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~8.2.5",
    "@angular/cdk": "^8.2.0",
    "@angular/common": "~8.2.5",
    "@angular/compiler": "~8.2.5",
    "@angular/core": "~8.2.5",
    "@angular/forms": "~8.2.5",
    "@angular/http": "^7.2.15",
    "@angular/material": "^8.2.0",
    "@angular/platform-browser": "~8.2.5",
    "@angular/platform-browser-dynamic": "~8.2.5",
    "@angular/router": "~8.2.5",
    "bootstrap": "^4.3.1",
    "hammerjs": "^2.0.8",
    "jquery": "^3.4.1",
    "ngx-toastr": "^11.2.1",
    "rxjs": "~6.4.0",
    "tslib": "^1.10.0",
    "zone.js": "~0.9.1"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.803.4",
    "@angular/cli": "~8.3.4",
    "@angular/compiler-cli": "~8.2.5",
    "@angular/language-service": "~8.2.5",
    "@types/node": "~8.9.4",
    "@types/jasmine": "~3.3.8",
    "@types/jasminewd2": "~2.0.3",
    "codelyzer": "^5.0.0",
    "jasmine-core": "~3.4.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~4.1.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.1",
    "karma-jasmine": "~2.0.1",
    "karma-jasmine-html-reporter": "^1.4.0",
    "protractor": "~5.4.0",
    "ts-node": "~7.0.0",
    "tslint": "~5.15.0",
    "typescript": "~3.5.3"
  }

【讨论】:

  • 它是“开始”:“ng serve”。我的包中没有`-EXTRACT-CSS`
  • 我正在使用 Angular 7。为什么要升级?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-09
  • 1970-01-01
  • 2017-03-13
  • 1970-01-01
  • 2022-08-12
相关资源
最近更新 更多