尝试将我的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?
预先感谢。
我相信您缺少地图功能。您想要这样的东西:
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/