Angular 6如何管道http调用

鲁斯塔姆·加涅涅夫(Rustam Ganeyev)

我有一个HTTP服务,该服务使用HttpClient进行API调用:

//provider.service.ts
export interface Lesson {
    id?: number,
    name: string,
    type: LessonType,
    teacher_data: string,
    student_data: string
}

export class ProviderService {
    constructor(private http: HttpClient) {}

    postLesson(form): Observable<Lesson> {        
        const body = this.getFormUrlEncoded(form.value);
        return this.http.post<Lesson>('/api/lesson/', body, this.postHttpOptions);
    }
}

我有一个使用此ProviderService的组件,如下所示:

onSubmit():void {
    this.providerService.createLesson(lessonForm).subscribe(result=> {
        console.log(result);
        //do my stuff
      });
    }
  }

它工作正常,一切都很好。现在,我想创建一个LessonService,以使所有http调用都通过该服务。它会缓存结果,发出更改等。

我是这样写的:

//updated lessons.component.ts
    onSubmit():void {
        this.LessonsService.createLesson(this.lessonForm).subscribe(result=> {
            console.log(result);
            //do my stuff
        });
    }

//lessons.service.ts
export class LessonsService {
    constructor(private http: ProviderService) {}

    @Output() lessonChange: EventEmitter<Lesson> = new EventEmitter();

    public createLesson(form): Observable<Lesson> {
        let observable = this.http.postLesson(form);
        observable.pipe(
            map(result => {
                //this code is not executed, I do not understand why                
                this.lessonChange.emit(result);
                return result;
            })
        );    
        return observable;
  }
}

当我从HTTP提供程序获取结果时,我想发出一个事件。我做错了什么?

巴鲁克

有两种方法可以执行此操作,一种是将放入pipe变量声明中,然后使用tap代替map

export class LessonsService {
    @Output() lessonChange: EventEmitter<Lesson> = new EventEmitter();

    public createLesson(form): Observable<Lesson> {
        let observable = this.http.postLesson(form).pipe(
            tap(result => {
                //this code is not executed, I do not understand why                
                this.lessonChange.emit(result);
                return result;
            })
        );    
        return observable;
  }
}

另一种方法是创建对变量的订阅

export class LessonsService {
    @Output() lessonChange: EventEmitter<Lesson> = new EventEmitter();

    public createLesson(form): Observable<Lesson> {
        let observable = this.http.postLesson(form);
        observable.subscribe(result => {
            //this code is not executed, I do not understand why                
            this.lessonChange.emit(result);
            return result;
        );    
        return observable;
  }
}

我个人会选择后者,这样您可以确保仅在成功时才发出事件。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Angular 6中的HTTP get调用

Angular 6嵌套HTTP调用嵌套调用永不执行

Angular 6:如何在进行http调用时将响应类型设置为文本

如何使用多个参数调用Angular 2管道?

如何从Angular 2中的组件创建和调用管道?

Angular:如何使用RXJS 6调用finally()

如何调用多个API并订阅angular 6?

如何跟踪Angular HTTP POST调用进度

Angular:如何阻止$ http调用阻止UI?

Angular如何从带有管道名称的字符串中调用管道?

Angular 6-日期管道

如何构建Angular 6 http服务?

Angular 6:如何间隔获取http

从 Angular 6 中的 http 调用访问对象属性

Angular 6:在调用 HTTP 请求后不设置类的属性

Angular $ http调用同步

如何在Angular 6中使用新的RXJS 6管道/贴图返回解析数据?

Angular 4管道中的异步调用

如何使用 Angular 异步管道调用从异步流接收的函数?错误:动作表达式中不能有管道

Angular 6如何获取从组件到服务的方法调用状态?

Angular / RxJS如何使用可观察管道生成ajax调用层次结构,但返回单个响应?

如何在Angular2中链接Http调用?

如何使用angular2 / http进行ajax调用?

如何在Angular JS $ http调用中显示超时错误?

Angular 2和RxJS:如何更改返回Observable的HTTP调用

Angular 如何取消路由参数更改时的 http 调用

http调用Angular后如何更新全局变量

如何在Angular的Http调用期间提取特定的键/值?

如何在 Angular 8 中处理异步 HTTP 调用?