HttpClientInMemoryWebApiModule 错误。我可以要两个吗?

HttpClientInMemoryWebApiModule Error. Can I have two?

我正在尝试实现 objects(情绪和面孔)的两个不同数据库。但是,当我在 app.module.ts 中同时执行这两个操作时,我在控制台中收到以下错误:

"{body: {...}, url: "api/emotions", headers: HttpHeaders, 状态: 404 , 状态文本: "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-脸.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};
     }
     }

我猜你不能像下面那样有两个 HttpClientInMemoryWebApiModule.forRoot 声明,因为 forRoot() 配置方法采用 InMemoryDataService 并且从 api 来看它不接受数组。

HttpClientInMemoryWebApiModule.forRoot( InMemoryDataService : true, { dataEncapsulation: false } )
HttpClientInMemoryWebApiModule.forRoot( InMemoryDataFaceService, { dataEncapsulation: false } )

我的建议是只有一个 InMemoryDataService,其中 returns 两个服务。

createDb() {
  let service1 = [];
  let service2 = [];
  return { service1, service1 }
}

你可以参考这个 which implements in a similar fashion. I guess it's also a cleaner way to implement, here是另一个在同一个数据服务中实现两个服务的例子

OPINIONATED - but do you really need to do this?

您打算在屏幕上显示图像吗?我认为您可以将图像存储在项目的 img 文件夹中,并在数据服务中提供它的 url,一旦您得到响应,您就可以使用它。

let images = [
      { id: 1, path: 'img/image1.png', },
      { id: 2, path: 'img/image1.png', },
      { id: 3, path: 'img/image1.png', },
      { id: 4, path: 'img/image1.png' }
    ];

并且在UI

<ul>  
  <li *ngFor="#img of images">
    <img src="{{ img.path }}" />
  </li>
<ul>