【问题标题】:Angular 8 do I need to subscribe to a request if I don't care about the response如果我不关心响应,Angular 8 是否需要订阅请求
【发布时间】:2020-01-17 12:44:45
【问题描述】:

我希望这是有道理的。 我决定改变我的一些服务的工作方式仅仅是因为订阅响应和处理不同视图中的创建、更新和删除变得有点麻烦。所以我决定做一个这样的通用服务:

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { map } from 'rxjs/operators';

import { environment } from '@environments/environment';
import { Resource } from '../models/resource';
import { ToastrService } from 'ngx-toastr';
import { BehaviorSubject } from 'rxjs';

@Injectable({
    providedIn: 'root',
})
export class DataService<T extends Resource> {
    items: BehaviorSubject<T[]>;

    constructor(private endpoint: string, private http: HttpClient, private toastr: ToastrService) {
        this.items = new BehaviorSubject<T[]>([]);
    }

    initialize(feedId: number) {
        return this.http.get<T[]>(`${environment.apiUrl}/feeds/${feedId}/${this.endpoint}`).pipe(
            map(response => {
                console.log(this.endpoint, response);
                this.items.next(response);
                return response;
            }),
        );
    }

    get(id: number) {
        return this.http.get<T>(`${environment.apiUrl}/${this.endpoint}/${id}`);
    }

    create(filter: T) {
        return this.http.post<T>(`${environment.apiUrl}/${this.endpoint}`, filter).pipe(
            map((response: any) => {
                const message = response.message;
                const item = response.model;

                let items = this.items.value;
                items.push(item);

                this.emit(items, message);

                return response.model;
            }),
        );
    }

    update(filter: T) {
        return this.http.put<T>(`${environment.apiUrl}/${this.endpoint}`, filter).pipe(
            map((response: any) => {
                const message = response.message;
                const item = response.model;

                let items = this.items.value;
                this.remove(items, filter.id);
                items.push(item);

                this.emit(items, message);

                return response.model;
            }),
        );
    }

    delete(id: number) {
        return this.http.delete<string>(`${environment.apiUrl}/${this.endpoint}/${id}`).pipe(
            map((response: any) => {
                let items = this.items.value;
                items.forEach((item, i) => {
                    if (item.id !== id) return;
                    items.splice(i, 1);
                });

                this.emit(items, response);

                return response;
            }),
        );
    }

    private remove(items: T[], id: number) {
        items.forEach((item, i) => {
            if (item.id !== id) return;
            items.splice(i, 1);
        });
    }

    private emit(items: T[], message: string) {
        this.items.next(items);
        this.toastr.success(message);
    }
}

这个服务背后的想法是 initialize 方法只被调用一次,当它被调用时,你可以看到它将响应映射到 items 数组在服务本身内。然后,当执行创建、更新或删除时,更改的是该数组。

这将(理论上)允许任何组件订阅 items 数组以根据任何更改进行更新。

所以,我有一些“扩展”该服务的服务,例如:

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

import { Filter } from '@models';
import { DataService } from './data.service';
import { ToastrService } from 'ngx-toastr';

@Injectable({
    providedIn: 'root',
})
export class FilterService extends DataService<Filter> {
    constructor(httpClient: HttpClient, toastr: ToastrService) {
        super('filters', httpClient, toastr);
    }
}

到目前为止,一切都很好。所以,我的问题是:我必须调用 initialize 方法并调用订阅吗?

例如,目前我有这个组件:

import { Component, OnInit, Input } from '@angular/core';
import { first } from 'rxjs/operators';

import { FilterService } from '@services';
import { NgAnimateScrollService } from 'ng-animate-scroll';

@Component({
    selector: 'app-feed-filters',
    templateUrl: './filters.component.html',
    styleUrls: ['./filters.component.scss'],
})
export class FiltersComponent implements OnInit {
    @Input() feedId: number;
    displayForm: boolean;

    constructor(private animateScrollService: NgAnimateScrollService, private filterService: FilterService) {}

    ngOnInit() {
        this.initialize();
    }

    navigateToForm() {
        this.displayForm = true;
        this.animateScrollService.scrollToElement('filterSave');
    }

    private initialize(): void {
        this.filterService
            .initialize(this.feedId)
            .pipe(first())
            .subscribe(() => {});
    }
}

正如您在私有方法中看到的那样,我是 pipe,然后是 first,然后是 subscribe,如果我想从那里得到结果,我会这样做。在我的“子”组件中,我有这个:

import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { first } from 'rxjs/operators';

import { Filter } from '@models';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { ConfirmationDialogComponent } from '@core';
import { FilterService } from '@services';
import { FiltersSaveComponent } from './filters-save.component';

@Component({
    selector: 'app-filters',
    templateUrl: './filters.component.html',
    styleUrls: ['./filters.component.scss'],
})
export class FiltersComponent implements OnInit {
    filters: Filter[];

    constructor(private modalService: NgbModal, private filterService: FilterService) {}

    ngOnInit() {
        this.filterService.items.subscribe(filters => (this.filters = filters));
    }

    openModal(id: number) {
        const modalRef = this.modalService.open(ConfirmationDialogComponent);
        modalRef.componentInstance.message = 'Deleting a filter is irreversible. Do you wish to continue?';
        modalRef.result.then(
            () => {
                this.filterService.delete(id);
            },
            () => {
                // Do nothing
            },
        );
    }

    openSaveForm(filter: Filter) {
        const modalRef = this.modalService.open(FiltersSaveComponent);
        modalRef.componentInstance.feedId = filter.feedId;
        modalRef.componentInstance.filterId = filter.id;
        modalRef.componentInstance.modal = true;
    }
}

如您所见,我从 filterService 订阅了 items 数组。 所以,在我的父控制器中,我认为我实际上并不需要订阅,但如果我删除它,它就不起作用了。

我认为我可以做类似的事情:

private initialize(): void {
    this.filterService.initialize(this.feedId);
}

而不是

private initialize(): void {
    this.filterService
        .initialize(this.feedId)
        .pipe(first())
        .subscribe(() => {
            // I don't need this
        });
}

是我做错了什么,还是我必须这样做? 我希望我自己解释一下:)

【问题讨论】:

    标签: angular rxjs rxjs-subscriptions


    【解决方案1】:

    您必须在HttpClient 上的任何请求方法上调用subscribe 才能发送请求。 HttpClient 返回一个冷的 observable,这意味着它在订阅它之前不会运行(而不是立即开始运行的热 observable)。

    此外,HttpClient 中的 observable 只会发出一个值,即响应,这意味着不需要将其通过管道传输到 first。您的最终逻辑将如下所示:

    this.filterService.initialize(this.feedId).subscribe(() => undefined);
    

    或者,您可以在DataService 中订阅,而不是订阅使用服务的位置,然后您的通话将如下所示:

    this.filterService.initialize(this.feedId);
    

    HttpClient 的一个好处是它们返回的 observables 永远不会再次发出,因此无需跟踪订阅并稍后关闭它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-23
      • 2020-04-26
      • 1970-01-01
      • 2023-04-09
      相关资源
      最近更新 更多