[英]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: 这基本上是我想要的:
npm install && ng serve
npm install && ng serve
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: 我现在可以做的是:
ng
window appears with the debugging information, that shows info like npm install
and ng serve
were run ng
窗口,其中包含调试信息,其中显示了诸如npm install
和ng serve
已运行 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: 如果我能解释我真正想要的是:
ng
window appears, showing I ran npm install && ng serve -port 4200
ng
窗口,显示我已运行npm install && ng serve -port 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");
}
});
}
}
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.