2017-11-06 2 views
0

angular-in-memory-web-api(バージョン0.5.1)を使用しようとしています。私は地元のオブジェクトと「データベース」を設定する場合、これは正常に動作しますが、私は次のエラーでのHTTPリクエストを介してローカルのJSONファイルから自分のデータを取得しようとする場合、それは失敗します。できるだけ早く私はインポートとしてメモリ内の角度のWebクライアントとhttpClientの循環依存関係

Uncaught Error: Provider parse errors: Cannot instantiate cyclic dependency! HttpClient ("[ERROR ->]"): in NgModule AppModule in ./[email protected]:-1

httpClient私のサービスにパッケージ。

app.module.ts:メモリ内の-data.service.ts

import { BrowserModule } from '@angular/platform-browser'; 
import { NgModule } from '@angular/core'; 
import { HttpClientModule } from '@angular/common/http'; 
// Imports for loading & configuring the in-memory web api 
import { HttpClientInMemoryWebApiModule } from 'angular-in-memory-web-api'; 
import { InMemoryDataService } from 'app/shared/services/in-memory-data.service'; 

import { AppComponent } from './app.component'; 
// other imports of app components 

@NgModule({ 
    imports:  [ 
    BrowserModule, 
    HttpClientModule, 
    HttpClientInMemoryWebApiModule.forRoot(InMemoryDataService), // always import after the HttpClientModule 
    ], 
    declarations: [ 
    AppComponent, 
    // ... 
    ], 
    providers: [ // app wide services not concerning the problem ], 
    bootstrap: [ AppComponent ] 
}) 
export class AppModule { } 

import { Injectable } from '@angular/core'; 
import { HttpClient } from '@angular/common/http'; 
import { InMemoryDbService } from 'angular-in-memory-web-api'; 
import { RequestInfo } from 'angular-in-memory-web-api'; 

@Injectable() 
export class InMemoryDataService implements InMemoryDbService {  
    // constructor(http: HttpClient) { // this creates the cyclic dependency 
    constructor() { 

    }  
    createDb(reqInfo?: RequestInfo) { 
    const db = {} 
    // fetch data from local JSON files and set up "database" object  
    return db; 
    } 

} 

HttpClientModuleに関連したこの問題ですか、それはangular-in-memory-web-api問題ですか?

+0

jsonファイルへのリクエストを行う場合は、InMemoryWebApiModuleを使用しないでください。そのためには、httpclientを使ってhttp getリクエストを作成してください。 – Alex

+0

JSONファイルからインメモリデータベースサービスを初期化することは、私がやりたいことなので、かなりのデータであるためJSONファイルをリクエストする必要があります。 – jowey

+0

はい、jsonファイルをリクエストしてください。inmemorywebapiではありません。 – Alex

答えて

0

私は同じ問題に直面しています。それはどのように解決できたのでしょうか、コンストラクタには注入しないでください。

createDBから、循環エラーなしで正しく挿入されたhttpclientがあります。

import { Injectable, Injector } from '@angular/core'; 
import { HttpClient } from '@angular/common/http'; 
import { InMemoryDbService } from 'angular-in-memory-web-api'; 
import { RequestInfo } from 'angular-in-memory-web-api'; 

@Injectable() 
export class InMemoryDataService implements InMemoryDbService { 
    httpClient: HttpClient; 
    constructor(private inject: Injector) { 

    }  
    createDb(reqInfo?: RequestInfo) { 
    const db = {} 
    this.httpClient = this.inject.get(HttpClient); 
    // fetch data from local JSON files and set up "database" object  
    return db; 
    } 

} 
関連する問題