Angular 2 + / Auth0使用AuthHttp与服务和可观察

时间:2017-12-11 13:06:25

标签: angular angular2-services auth0 angular2-observables authhttp

尝试将我的Http调用转换为AuthHttp调用,并在使用AuthHttp时收到下面列出的编译错误。我做了2次尝试解决这个问题,两者都产生了不同的错误。我想保留我的订阅来自组件的结构,并让服务返回一个observable。

订阅的组件,两条路径都相同。

import { Component, OnInit } from '@angular/core';

import { Hero } from '../hero';

import { HeroService } from '../hero.service';

@Component({
  selector: 'app-dashboard',
  templateUrl: './dashboard.component.html',
  styleUrls: [ './dashboard.component.css' ]
})
export class DashboardComponent implements OnInit {
  heroes: Hero[] = [];

  constructor(private heroService: HeroService) { }

  ngOnInit() {
    this.getHeroes();
  }

  getHeroes(): void {
    this.heroService.getHeroes()
      .subscribe(heroes => this.heroes = heroes.slice(1, 5));
  }
}

服务,缺少相关的代码(其他功能有相同的问题,但我想如果我可以得到一个我可以弄清楚其他CRUD函数的帮助)我把它们都放在了我们没有考虑但是当我编译时我发表评论一出。

import { Injectable } from '@angular/core';
import { Hero } from './hero';
import { Observable } from 'rxjs/Observable';
import { of } from 'rxjs/observable/of';
import { MessageService } from './message.service';
// import { HttpClient, HttpHeaders } from '@angular/common/http';
import { AuthHttp } from 'angular2-jwt';
import { catchError, map, tap } from 'rxjs/operators';

@Injectable()
export class HeroService {

  ...

  /** GET heroes from the server */
  getHeroes(): Observable<Hero[]> {
    // Trial 1
    return this.authHttp.get<Hero[]>(this.heroesUrl).pipe(
      tap(heroes => this.log(`fetched heroes, ${this.heroesUrl}`)),
      catchError(this.handleError('getHeroes', []))
    );

    // Trial 2
    return this.authHttp
      .get(this.heroesUrl)
      .map((res: Response) => <Hero[]>res.json());
  }

  ...

  constructor(
    // private http: HttpClient,
    private authHttp: AuthHttp,
    private messageService: MessageService) { }
}

收到的错误

// Trial 1
ERROR in src/app/hero.service.ts(36,12): error TS2558: Expected 0 type arguments, but got 1.

// Trial 2
  Property 'includes' is missing in type 'Promise<any>'.
ERROR in src/app/hero.service.ts(44,12): error TS2345: Argument of type '(res: Response) => Hero[]' is not assignable to parameter of type '(value: Response, index: number) => Hero[]'.
  Types of parameters 'res' and 'value' are incompatible.
    Type 'Response' is not assignable to type 'Response'. Two different types with this name exist, but they are unrelated.
src/app/hero.service.ts(44,31): error TS2352: Type 'Promise<any>' cannot be converted to type 'Hero[]'.
src/app/hero.service.ts(44,31): error TS2352: Type 'Promise<any>' cannot be converted to type 'Hero[]'.
  Property 'includes' is missing in type 'Promise<any>'.

我不确定我做错了什么,感谢任何帮助。

另一个相关问题是我是否需要通过AuthHttp传递http选项?这是服务的一个例子

  const httpOptions = {
    headers: new HttpHeaders({ 'Content-Type': 'application/json' })
  };

  /** PUT: update the hero on the server */
  updateHero (hero: Hero): Observable<any> {
    const url = `${this.heroesUrl}/${hero.id}`;
    return this.http.put(url, hero, httpOptions).pipe(
      tap(_ => this.log(`updated hero id=${hero.id}, ${url}`)),
      catchError(this.handleError<any>('updateHero'))
    );
  }

我在这里使用http,但会将其与authHttp交换,我不确定是否需要httpOptions?

提前致谢。

1 个答案:

答案 0 :(得分:0)

我相信你错过了地图功能。 你想要这样的东西:

getTenantId (): Observable<string> {
 return this.authHttp.get(this.apiUrl).map(res => res.json());
}

更多可以在这里找到...

https://auth0.com/blog/introducing-angular2-jwt-a-library-for-angular2-authentication/