1

Environment

  • Angular: 4.4.4
  • Angular cli: 1.4.6
  • rxjs: 5.4.3

File Structure (Follows Google's guideline)

- app
    - app.module
    - core (Imported into app.module directly)
        - core.module
        - http-request.service
    - user (Lazy loaded)
        - user.module
        - shared
            - user.service
    - role (Lazy loaded)
        - role.module
        - shared
            - role.service

Scenario

  • Core module shares common services cross entire app. It is imported into app.module ONLY.
  • User and role modules are lazy loaded.
  • User module provides user service.
  • Role module provides role service.

Issue

  • The role's component calls role.service to get the data by using HttpClient. It worked.
  • The user's component calls user.service to get the data by using HttpClient. It also worked.
  • The role or user's component calls http-request.service with exactly the same code, error occured.
    • file: core.ec5.js
    • line: 13,500
    • error: Collection 'foo' not found
    • status: 404
    • statusText: 'Not Found'
    • url: /dynamic/foo/get
  • The role's component calls user.service, same error.
  • The user's component calls role.service, same error.

Current Finding

  • In one module, imports and provides the http-request.service and other module's service.
  • In one module imports another module.

Both worked. But looks weird.

Google Guideline

  • Module should provide its own service.
  • Any cross entire app's common service should be provided by core module and core module only.
  • Core module should be imported by app module and no anywhere else.

The finding broke these guidelines. That makes it weird.

User Service

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable()
export class UserService {
    constructor(private httpClient: HttpClient) { }

    get(): Observable<any> {
        this.httpClient.get('/dynamic/foo/get');
    }
}

Http Request Service

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

import { Observable } from 'rxjs/Observable';

@Injectable()
export class HttpRequestService {
    constructor(private httpClient: HttpClient) { }

    get(url): Observable<any> {
        return this.httpClient.get(url);
    }
}

My Concern

  • Why HttpClient has such strange behavior?
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Hui
  • 83
  • 13
  • You seem to have two separate questions here, which is unhelpful, and your [mcve] doesn't seem very minimal. I think that dupe answers your first question. – jonrsharpe Oct 16 '17 at 10:36
  • Thanks Jonrsharpe for your super quick response. – Hui Oct 16 '17 at 11:05
  • If that does answer the first part, I'd suggest you edit your question to remove the second part, accept the duplicate and ask the second part as a *separate* question. Give a [mcve] for that - your interceptor doesn't implement the interface as posted. – jonrsharpe Oct 16 '17 at 11:10
  • I just went through the link, this is different issue. There is no error working with the service shared by core module. It only breaks when working with HttpClient shared by another module. – Hui Oct 16 '17 at 11:11
  • Then [edit] this one to focus on that single issue and explain the difference. – jonrsharpe Oct 16 '17 at 11:12
  • Also make sure that the `forRoot` method doesn't also fix your problem, it's recommended for a core module in e.g. https://angular.io/guide/ngmodule#configure-core-services-with-coremoduleforroot – jonrsharpe Oct 16 '17 at 11:18
  • Issue solved! Removes InMemoryWebApiModule! – Hui Oct 17 '17 at 08:04
  • Feel free to write up and accept your own answer, but please don't edit it into the question. If another question gave you the answer, I think you can flag to be marked as a duplicate. – jonrsharpe Oct 17 '17 at 10:51

0 Answers0