简体   繁体   English

用ng服务和dotnet核心Angular启动Angular

[英]Launch Angular with ng serve with dotnet core Angular

I have created a new Angular project with the dotnet core CLI: 我已经使用dotnet核心CLI创建了一个新的Angular项目:

dotnet new angular -o my-app

I have set up my own deployment and build process outside of my project, so my project is essentially dumb when it comes to building and deploying. 我已经在项目外部设置了自己的部署和构建过程,因此,在构建和部署方面,我的项目本质上是愚蠢的。 My project should never build any .js files and store them locally in the project. 我的项目永远不要构建任何.js文件并将其本地存储在项目中。 This is basically what I want: 这基本上是我想要的:

  1. I press F5/launch debugger 我按F5 /启动调试器
  2. The dotnet core project starts and runs npm install && ng serve dotnet核心项目启动并运行npm install && ng serve
  3. Once done, or whenever it's needed, a browser will open and show the ng serve site (I think default is http://localhost:4200 ) 完成后或需要时,浏览器将打开并显示ng serve站点(我认为默认值为http://localhost:4200

So far I'm close to getting this to work, but for some reason, the dotnet core project serves me static files that are built, which I don't want (because I want live reload from ng serve ). 到目前为止,我已经接近使它生效,但是由于某种原因,dotnet核心项目为我提供了我不需要的静态文件(因为我想从ng serve重新加载)。 Here's what I can do right now: 我现在可以做的是:

  1. Press F5 to launch the debugger 按F5启动调试器
  2. A ng window appears with the debugging information, that shows info like npm install and ng serve were run 随即显示一个ng窗口,其中包含调试信息,其中显示了诸如npm installng serve已运行
  3. Opens http://localhost:5000 which serves me the static files, as if I built the application with ng build 打开http://localhost:5000 ,它为我提供静态文件,就好像我使用ng build应用程序一样

If I can explain what I really want: 如果我能解释我真正想要的是:

  1. Press F5 to launch the debugger 按F5启动调试器
  2. Project starts and the ng window appears, showing I ran npm install && ng serve -port 4200 项目启动并显示ng窗口,显示我已运行npm install && ng serve -port 4200
  3. Browser launches http://localhost:4200 浏览器启动http:// localhost:4200

Right now it opens at port 5000 (or whatever I specify in the launchSettings.json file), but I can't get it to launch 4200 because technically I don't want my dotnet core project to start a new web server (since ng serve already creates one). 现在,它在打开5000端口(或不管我在指定launchSettings.json文件),但我不能把它推出4200,因为技术上我不希望我的dotnet的核心项目,开始一个新的Web服务器(因为ng serve已经创建了一个)。 Is this possible? 这可能吗?

package.json package.json

"scripts": {
    "start": "npm install && ng serve --port 4200 &REM"
}

Startup.cs 启动文件

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Error");
        }

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

            if (env.IsDevelopment())
            {
                spa.UseAngularCliServer("start");
            }
        });
    }
}

I found this . 我发现了这个

In nutshell, you have to replace the following line in Startup.cs 简而言之,您必须在Startup.cs替换以下行

spa.UseAngularCliServer(npmScript: "start");

with

spa.UseProxyToSpaDevelopmentServer("NG-SERVER-URL");

You have to run ng serve once, this will tell you the url where it's listening on ( NG-SERVER-URL ) 您必须运行一次ng serve ,这将告诉您正在监听的url( NG-SERVER-URL

This modification allows to run the ng serve in separate process. 此修改允许在单独的过程中运行ng serve Asp.Net will connect and dispatch calls to it. Asp.Net将连接并调度对其的调用。

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

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