【发布时间】:2018-03-19 15:51:22
【问题描述】:
我正在尝试实现两个不同的对象数据库(情绪和面孔)。但是当我在 app.module.ts 中同时实现两者时,我的控制台中出现以下错误:
"{body: {…}, url: "api/emotions", headers: HttpHeaders, status: 404, statusText: "Not Found"}"
只用一个就可以了。 如果我将两者解析为相同的 forRoot。我明白了:
错误 TS2554:预期 1-2 个参数,但得到了 3 个。
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { LoginComponent } from './login.component';
import { DashboardComponent } from './dashboard.component';
import { EmotionsComponent } from './emotions.component';
import { EmotionSearchComponent } from './emotion-search.component';
import { EmotionDetailComponent } from './emotion-detail.component';
import { FaceService } from 'face.service';
import { FacesComponent } from './faces.component';
import { FaceSearchComponent } from './face-search.component';
import { FaceDetailComponent } from './face-detail.component';
import { EmotionService } from 'emotion.service';
import { MessageService } from 'message.service';
import {AppRoutingModule} from './app-routing.module';
import { AppComponent } from './app.component';
import { HttpClientModule } from '@angular/common/http';
import { HttpClientInMemoryWebApiModule } from 'angular-in-memory-web-api';
import { InMemoryDataService } from 'in-memory-data.service';
import { InMemoryDataFaceService } from 'in-memory-data-face.service';
@NgModule({
declarations: [
AppComponent,
LoginComponent,
DashboardComponent,
EmotionsComponent,
EmotionSearchComponent,
EmotionDetailComponent,
FacesComponent,
FaceSearchComponent,
FaceDetailComponent,
],
imports: [
BrowserModule,
AppRoutingModule,
HttpClientModule,
HttpClientInMemoryWebApiModule.forRoot( InMemoryDataService : true, { dataEncapsulation: false } )
HttpClientInMemoryWebApiModule.forRoot( InMemoryDataFaceService, { dataEncapsulation: false } )
],
providers: [ EmotionService, FaceService, MessageService ],
bootstrap: [AppComponent]
})
export class AppModule { }
in-memory-data.service.ts
import { InMemoryDbService } from 'angular-in-memory-web-api';
export class InMemoryDataService implements InMemoryDbService {
createDb() {
const emotions = [
{ id: 11, name: 'HAPPY' },
{ id: 12, name: 'SAD' },
{ id: 13, name: 'ANGRY' },
{ id: 14, name: 'EXCITED' },
{ id: 15, name: 'ANXIOUS' },
];
return {emotions};
}
}
in-memory-data-face.service.ts
import { InMemoryDbService } from 'angular-in-memory-web-api';
export class InMemoryDataFaceService implements InMemoryDbService {
createDb() {
const faces = [
{ id: 11, name: 'HAPPY2' },
{ id: 12, name: 'SAD2' },
{ id: 13, name: 'ANGR2Y' },
{ id: 14, name: 'EXCITED' },
{ id: 15, name: 'ANXIOUS' },
{ id: 16, name: 'RubberMan' },
{ id: 17, name: 'Dynama' },
{ id: 18, name: 'Dr IQ' },
{ id: 19, name: 'Magma' },
{ id: 20, name: 'Tornado' }
];
return {faces};
}
}
【问题讨论】:
-
以下配置中的 true 代表什么? HttpClientInMemoryWebApiModule.forRoot( InMemoryDataService : true, { dataEncapsulation: false } )
-
抱歉,不应该是真的,我只是在尝试一些东西。
-
不用担心,您是否再次尝试删除它?成功了吗?
标签: typescript angular2-routing httpclient