【发布时间】:2017-10-25 07:48:39
【问题描述】:
我正在关注这个教学视频Building web apps powered by Angular 2.x using Visual Studio 2017,并且在 51:00 左右是我所在的部分,我在这个源文件中遇到了一个问题:
使用此功能:
getAccountSummaries() {
return this.http.get('api/Bank/GetAccountSummaries')
.map(response => response.json() as AccountSummary[])
.toPromise();
}
我在 .json() 上的 Visual Studio 中收到红色文本,上面写着
符号“json”无法正确解析,可能是因为它位于无法访问的模块中
当我尝试运行应用程序时,我收到异常消息:
System.Exception: Call to Node module failed with error: Error: Uncaught (in promise): Error: No provider for AccountService!
按照教程,我使用了与讲师相同的模板,但我认为从那以后肯定发生了一些变化,因为他只有一个 app.module.ts,而我的模板有四个:app.module.client.ts、app.module.server.ts 和 @987654329 @ 不幸的是,作为 ASP.NET Core 和 Angular2 的新手,我不知道它们为什么不同或意义何在。
到目前为止,我已经取得了成功,只需对他的app.module.ts 到我的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 { HomeComponent } from './components/home/home.component';
import { FetchDataComponent } from './components/fetchdata/fetchdata.component';
import { CounterComponent } from './components/counter/counter.component';
import { HeaderComponent } from './components/shared/header/header.component';
import { AccountListComponent } from './components/account/account-list/account-list.component';
import { AccountSummaryComponent } from './components/account/account-summary/account-summary.component';
import { AccountDetailComponent } from './components/account/account-detail/account-detail.component';
import { FormatAccountNumberPipe } from './components/shared/format-account-number.pipe';
import { AccountActivityComponent } from './components/account/acccount-activity/account-activity.component';
import { AccountService } from './components/shared/account.service';
export const sharedConfig: NgModule = {
bootstrap: [ AppComponent ],
declarations: [
AppComponent,
NavMenuComponent,
CounterComponent,
FetchDataComponent,
HomeComponent,
HeaderComponent,
AccountListComponent,
AccountDetailComponent,
AccountSummaryComponent,
AccountActivityComponent,
FormatAccountNumberPipe
],
imports: [
RouterModule.forRoot([
{ path: '', redirectTo: 'account', pathMatch: 'full' },
{ path: 'account', component: AccountListComponent },
{ path: 'detail/:id', component: AccountDetailComponent },
{ path: '**', redirectTo: 'account' }
])
],
providers: [ AccountService ]
};
不幸的是,直到 .json() 这条线之前,一切都编译得很好并且像他的一样工作。
我该如何解决?
【问题讨论】:
-
你的打字稿版本是什么
tsc -v,想问一下你在使用 ReSharper 吗? -
试试这个:从
app.module.shared.ts中删除提供者部分;将AccountService添加到app.module.client.ts的提供者部分。编译,运行。或者,只需将...sharedConfig.providers添加到app.module.client.ts的providers部分,并将服务声明保留在共享模块文件中。 -
@Hamed "Version 2.2.3" 是的,我正在使用 ReSharper。
-
@R.Richards 这些修复不起作用,同样的错误是缺少提供程序。
标签: asp.net angular asp.net-core asp.net-core-mvc .net-core