Vue.js 安装Asp.Net核心应用程序以启动带有webpack和HMR的Vue SPA
我正在尝试使用ASP.NET Core 3.0和Vue设置一个新的应用程序。我一直在努力正确地设置它,以便后端和前端服务器能够正确地集成,具有正确的路由和工作HMR。我现在确实可以用了,但我不知道这是不是正确的方法,而且我觉得有些部分没有发挥应有的作用 我当前的设置: Webpack配置为将文件输出到Vue.js 安装Asp.Net核心应用程序以启动带有webpack和HMR的Vue SPA,vue.js,asp.net-core,Vue.js,Asp.net Core,我正在尝试使用ASP.NET Core 3.0和Vue设置一个新的应用程序。我一直在努力正确地设置它,以便后端和前端服务器能够正确地集成,具有正确的路由和工作HMR。我现在确实可以用了,但我不知道这是不是正确的方法,而且我觉得有些部分没有发挥应有的作用 我当前的设置: Webpack配置为将文件输出到wwwroot。我还有HMR和 运行时启用: 在Startup.cs中: public void ConfigureServices(IServiceCollection services)
wwwroot
。我还有HMR和
运行时启用:
在Startup.cs
中:
public void ConfigureServices(IServiceCollection services)
{
.....
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "wwwroot";
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseStaticFiles();
app.UseSpaStaticFiles();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller}/{action=Index}/{id?}");
if (env.IsDevelopment())
{
if (System.Diagnostics.Debugger.IsAttached)
{
// run the dev server
endpoints.MapToVueCliProxy("{*path}", new SpaOptions { SourcePath = "ClientApp" },
npmScript: "serve", regex: "Compiled successfully");
}
}
// serve index.html file when no api endpoints are matched
endpoints.MapFallbackToFile("index.html");
}
我的此设置问题:
- 通常,HMR在内存中构建文件,但我在这里设置了它,将新文件写入文件系统,以便后端服务器可以访问和服务
。这是可行的,但是现在我的文件系统中有大量的膨胀。有没有办法将内存中的HMR与ASP.NET服务器结合使用index.html
- 代理并不总是可靠的;我经常在控制台中遇到无法连接到服务器的错误。此外,
有时会被缓存,我必须进行硬重新加载。能不能让它变得更可靠index.html
public void ConfigureServices(IServiceCollection services)
{
.....
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "wwwroot";
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseStaticFiles();
app.UseSpaStaticFiles();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller}/{action=Index}/{id?}");
if (env.IsDevelopment())
{
if (System.Diagnostics.Debugger.IsAttached)
{
// run the dev server
endpoints.MapToVueCliProxy("{*path}", new SpaOptions { SourcePath = "ClientApp" },
npmScript: "serve", regex: "Compiled successfully");
}
}
// serve index.html file when no api endpoints are matched
endpoints.MapFallbackToFile("index.html");
}