首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular 2+/Auth0使用AuthHttp提供服务和可观察性

Angular 2+/Auth0使用AuthHttp提供服务和可观察性
EN

Stack Overflow用户
提问于 2017-12-11 21:06:25
回答 0查看 244关注 0票数 0

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

订阅的组件对于两个跟踪都是相同的。

代码语言:javascript
复制
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函数),我把它们都放在原处,但当我编译时,我注释掉了一个。

代码语言:javascript
复制
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) { }
}

收到的错误

代码语言:javascript
复制
// 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选项?以下是该服务的一个示例

代码语言:javascript
复制
  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?

提前谢谢。

EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47753668

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档