【发布时间】:2017-09-07 22:07:58
【问题描述】:
我已经为此奋斗了一段时间,并决定写一篇文章。
我正在使用 ASP.Net Core 5.0 和 Angular 2 上的 VS2017 构建一个简单的单页应用程序,其模板取自 ASP.NET Core 模板包。该应用程序应该管理一个联系人列表数据库。
我的想法是默认的起始“/home”页面应该使用 HomeComponent 显示联系人列表。所有路由都可以正常工作,但是当应用程序启动时或每当我尝试路由到“/home”时,它会一直转到 ASP Home 视图的 Index.cshtml 页面,而不是使用 Angular 路由。
知道如何让它始终通过 Angular 吗?我想将 HomeComponent 与 '/home' 路由对齐,但它一直转到 ASP 页面,该页面只显示“正在加载...”,而我并不真正需要(我认为)。
我已经尝试了很多不同的解决方案,但我无法让任何工作。我可能在这里遗漏了一些明显的东西,因为我在这些方面不是太先进,所以如果你能保持基本,请做;-)
这是我在 Startup.cs 中的配置方法:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions
{
HotModuleReplacement = true
});
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index" });
routes.MapSpaFallbackRoute(
name: "spa-fallback",
defaults: new { controller = "Home", action = "Index",});
});
}
app.module.shared.ts:
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { AppComponent } from './components/app/app.component'
import { NavMenuComponent } from './components/navmenu/navmenu.component';
import { DetailsComponent } from './components/details/details.component';
import { EditComponent } from './components/editContact/editContact.component';
import { NewContactComponent } from './components/newContact/newContact.component';
import { HomeComponent } from './components/home/home.component';
import { ContactServices } from './services/services';
export const sharedConfig: NgModule = {
bootstrap: [ AppComponent ],
declarations: [
AppComponent,
NavMenuComponent,
DetailsComponent,
EditComponent,
NewContactComponent,
HomeComponent
],
providers: [ContactServices],
imports: [
RouterModule.forRoot([
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{ path: 'home', component: HomeComponent },
{ path: 'details', component: DetailsComponent },
{ path: 'new', component: NewContactComponent },
{ path: '**', redirectTo: 'home' }
])
]};
ASP 的主页 Index.cshtml:
@{
ViewData["Title"] = "Home Page";
}
<app>Loading...</app>
<script src="~/dist/vendor.js" asp-append-version="true"></script>
@section scripts {
<script src="~/dist/main-client.js" asp-append-version="true"></script>
}
Aaa 和 home.component.ts:
import { Component } from '@angular/core';
import { ContactServices } from '../../services/services';
import { Response } from '@angular/http';
@Component({
selector: 'home',
templateUrl: './home.component.html'
})
export class HomeComponent {
public ContactList = [];
public constructor(private conService: ContactServices) {
this.conService.getContactList()
.subscribe(
(data: Response) => (this.ContactList = data.json())
);
}
}
提前谢谢各位!任何建议将不胜感激。
【问题讨论】:
标签: asp.net-mvc angular2-routing single-page-application