0

尝试将我的 Http 调用转换为 AuthHttp 调用,并且在使用 AuthHttp 时出现下面列出的编译错误。我进行了 2 次尝试来解决这个问题,但都产生了不同的错误。我想保留订阅来自组件并让服务返回可观察的结构。

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

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 了?

提前致谢。

4

1 回答 1

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/

于 2018-03-02T11:33:52.690 回答