ngx-rocket / generator-ngx-rocket

:rocket: Extensible Angular 14+ enterprise-grade project generator
https://ngx-rocket.github.io/
MIT License
1.53k stars 216 forks source link

try to use HttpService, fails #222

Closed hope4555 closed 6 years ago

hope4555 commented 6 years ago

I'm submitting a...

[X ] Bug report

Current behavior

Can not use HttpService in ngx-rocket/starter-kit seems to be using deprecated Http class

Expected behavior

should be able to use Http service

Minimal reproduction of the problem with instructions

Take code from https://github.com/ngx-rocket/starter-kit

try to use HttpService somewere

Environment

sinedied commented 6 years ago

Please see https://github.com/ngx-rocket/generator-ngx-rocket/issues/94

HttpClient cannot be used for now as it is lacking some functionality, but the deprecated Http service used as a base for our extension should still be working.

What is the exact error you're having?

hope4555 commented 6 years ago

I tried adding HttpService to AuthenticationService and use it in the login method First error i got was "No provider for HttpService!" I added HttpService to "core.module.ts" and now i get "No provider for ConnectionBackend!"

sinedied commented 6 years ago

That's normal, since HttpService is provided as a drop-in replacement for Angular's Http service, so you just have to import and inject Http service to use it.

Here is a working example:

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { of } from 'rxjs/observable/of';
import { map } from 'rxjs/operators';
import { Http, Response } from '@angular/http';

[...]

@Injectable()
export class AuthenticationService {

  private _credentials: Credentials | null;

  constructor(private http: Http) {
    const savedCredentials = sessionStorage.getItem(credentialsKey) || localStorage.getItem(credentialsKey);
    if (savedCredentials) {
      this._credentials = JSON.parse(savedCredentials);
    }
  }

  login(context: LoginContext): Observable<Credentials> {
    // Replace by proper authentication call
    const data = {
      username: context.username,
      token: '123456'
    };
    this.setCredentials(data, context.remember);
    return this.http.get('/jokes/random?category=dev')
      .pipe(
        map(() => data),
      );
    // return of(data);
  }

  [...]
}