Angular Transfer State 不会阻止重复的 http 调用 [英] Angular Transfer State not preventing repeat http calls

查看:26
本文介绍了Angular Transfer State 不会阻止重复的 http 调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 http 请求,它被注入到我的组件中并从那里订阅.由于我在我的应用程序中引入了具有 angular 通用的服务器端渲染,因此页面上的结果至少重复了两次.

我有一个在点击时调用的方法,它执行对 facebook api 的 http 请求

getAlbum(albumId: number) {this.albumPhotos = this.state.get(ALBUM_PHOTOS_KEY, null as any);如果(!this.albumPhotos){this.facebookService.getBachadiffAlbumPhotos(albumId).subscribe(res => {this.bachataPicsArray = res;this.state.set(ALBUM_PHOTOS_KEY, res as any);});}}

我在导入下面声明了 const 变量

const ALBUM_PHOTOS_KEY = makeStateKey('albumPhotos');

我也声明了财产

专辑名称:任意;

我假设我已经完成了所有的导入,我在

解决方案

你说得对,如果你在服务器端或浏览器端只执行一次查询,你只需要以不同的方式处理你的服务两次.

伪逻辑:

  • 如果服务器 -> 执行 http 请求 -> 在传输状态中设置值
  • 如果浏览器 -> 从传输状态获取值

为此,您可以例如增强您的服务,如下所示:

@Injectable()导出类 FacebookEventsService {const ALBUM_PHOTOS_KEY: StateKey;构造函数(@Inject(PLATFORM_ID)私有平台ID:对象,私有http:HttpClient){this.ALBUM_PHOTOS_KEY = makeStateKey('albumPhotos');}getBachaDiffFacebookEvents(): Observable{//这里我们检查是服务器端还是浏览器端如果(isPlatformServer(this.platformId)){返回 this.getServerBachaDiffFacebookEvents();} 别的 {返回 this.getBrowserBachaDiffFacebookEvents();}}getServerBachaDiffFacebookEvents(): Observable{返回 this.http.get(this.facebookEventsUrl).map(res => {//这里保存也导致转移状态this.transferState.set(ALBUM_PHOTOS_KEY, calendarEvents);});}getBrowserBachaDiffFacebookEvents(): Observable{返回新的 Observable(观察者 => {观察者.next(this.transferState.get(ALBUM_PHOTOS_KEY, null));});}}

更新

要使用此逻辑,您还需要:

  1. TransferHttpCacheModule(在 app.module.ts 中初始化).

<块引用>

TransferHttpCacheModule 安装一个 Http 拦截器,避免客户端上的重复 HttpClient 请求,用于请求在服务器端渲染应用程序时已经完成.

https://github.com/angular/universal/tree/master/模块/通用

  1. ServerTransferStateModule 在服务器端和 BrowserTransferStateModule 在客户端使用 TransferState

https://angular.io/api/platform-b​​rowser/TransferState

P.S.:请注意,如果您这样做并增强您的服务器,当然您将不再需要在上面显示的 getAlbum() 方法中设置 transfer-state 中的值

更新 2

如果您想像在 gallery.component.ts 中那样处理服务器端和浏览器端,您可以执行以下操作:

getAlbum(albumId: number) {如果(isPlatformServer(this.platformId)){如果(!this.albumPhotos){this.facebookService.getBachadiffAlbumPhotos(albumId).subscribe(res => {this.bachataPicsArray = res;this.state.set(ALBUM_PHOTOS_KEY, null);});}} 别的 {this.albumPhotos = this.state.get(ALBUM_PHOTOS_KEY,null);}}

更新 3

问题是,您的操作 getAlbum 永远不会在服务器端调用.此操作仅在浏览器端使用,一旦页面呈现,当用户单击特定操作时.因此,在这种特定情况下使用转移状态是不正确/不需要的.

此外不确定您服务中的 Observable 是否已正确订阅.

这里要改变什么才能让它运行:

gallery.component.ts

getAlbum(albumId: number) {this.facebookService.getBachadiffAlbumPhotos(albumId).subscribe(res => {this.albumPhotos = res;});}

facebook-events.service.ts

getBachadiffAlbumPhotos(albumId: number): Observable{this.albumId = 专辑Id;this.facebookAlbumPhotosUrl = `https://graph.facebook.com/v2.11/${this.albumId}/photos?limit=20&fields=images,id,link,height,width&access_token=${this.访问令牌}`;return Observable.fromPromise(this.getPromiseBachaDiffAlbumPhotos(albumId));}private getPromiseBachaDiffAlbumPhotos(albumId: number): Promise<{}>{返回新的承诺((解决,拒绝)=> {this.facebookAlbumPhotosUrl = `https://graph.facebook.com/v2.11/${this.albumId}/photos?limit=20&fields=images,id,link,height,width&access_token=${this.访问令牌}`;让 facebookPhotos: FacebookPhoto[] = new Array();让 facebookPhoto: FacebookPhoto;const 参数:HttpParams = new HttpParams();this.http.get(this.facebookAlbumPhotosUrl, {params: params}).subscribe(res => {让 facebookPhotoData = res['data'];for(让facebookPhotoData的照片){facebookPhotos.push(脸书照片 = {身份证:照片.id,图像:photo.images[3].source,链接:照片链接,高度:照片.高度,宽度:照片.宽度});}解决(脸书照片);}, (错误) =>{拒绝(错误);});});}

更新 4

ngOnInit 在服务器端执行,这意味着我在这里的第一个答案必须在这种情况下使用.

此外,还要注意,在服务器端您无权访问窗口,因此调用 $

使用 gallery.component.ts 你可以做这样的事情来只运行一次 http 查询,但这并不能解决你所有的问题,我认为它仍然需要进一步改进.>

ngOnInit() {如果(isPlatformServer(this.platformId)){this.facebookService.getBachadiffFacebookVideos().subscribe(res => {this.bachataVidsArray = res;this.state.set(VIDEOS_KEY, res as any);});this.facebookService.getBachadiffFacebookLastClassPictures().subscribe(res => {this.bachataPicsArray = res;this.state.set(LAST_CLASS_PICTURES_KEY, res as any);});this.facebookService.getBachadiffAlbumNames().subscribe(res => {this.bachataAlbumHeaderNames = res;this.state.set(ALBUM_NAMES_KEY, res as any);});} 别的 {$('ul.tabs').tabs();this.bachataVidsArray = this.state.get(VIDEOS_KEY, null as any);this.bachataPicsArray = this.state.get(LAST_CLASS_PICTURES_KEY, null as any);this.bachataAlbumHeaderNames = this.state.get(ALBUM_NAMES_KEY, null as any);}}

I have the http request being made a service which is injected onto my component and subscribed to from there. Since I introduced server side rendering with angular universal to my application, the results on the page are repeated at least twice.

I have method which is called on click, which performs the http request to facebook's api

getAlbum(albumId: number) {

    this.albumPhotos = this.state.get(ALBUM_PHOTOS_KEY, null as any);

    if (!this.albumPhotos) {
      this.facebookService.getBachadiffAlbumPhotos(albumId).subscribe(res => {
        this.bachataPicsArray = res;
        this.state.set(ALBUM_PHOTOS_KEY, res as any);
      });
    }
  }

I declared the const variable below the imports

const ALBUM_PHOTOS_KEY = makeStateKey('albumPhotos');

And I also declared the property

albumNames: any;

I am assuming I have done all of the imports right I have the code on github in the gallery component.

解决方案

You are on the right pass, you just need to handle your service differently if you are on the server or the browser side to perform your queries only once and not twice.

Pseudo logic:

  • If server -> Do http request -> Set value in transfer-state
  • If browser -> Get value from transfer-state

To do so, you could for example enhance your Service like following:

@Injectable()
export class FacebookEventsService {
    const ALBUM_PHOTOS_KEY: StateKey<number>;

    constructor(@Inject(PLATFORM_ID) private platformId: Object, private http: HttpClient) {
       this.ALBUM_PHOTOS_KEY = makeStateKey('albumPhotos');
    } 

    getBachaDiffFacebookEvents(): Observable<CalendarEvent[]> {
        // Here we check if server or browser side
        if (isPlatformServer(this.platformId)) {
            return this.getServerBachaDiffFacebookEvents();
        } else {
            return this.getBrowserBachaDiffFacebookEvents();
        }
    }

    getServerBachaDiffFacebookEvents(): Observable<CalendarEvent[]> {

           return this.http.get(this.facebookEventsUrl)
             .map(res => {

                  // Here save also result in transfer-state
                  this.transferState.set(ALBUM_PHOTOS_KEY, calendarEvents);

             });
     }


        getBrowserBachaDiffFacebookEvents(): Observable<CalendarEvent[]> {
           return new Observable(observer => {
            observer.next(this.transferState.get(ALBUM_PHOTOS_KEY, null));
          });
     }
}

UPDATE

To use this logic you would also need:

  1. TransferHttpCacheModule (to be initialized in app.module.ts).

TransferHttpCacheModule installs a Http interceptor that avoids duplicate HttpClient requests on the client, for requests that were already made when the application was rendered on the server side.

https://github.com/angular/universal/tree/master/modules/common

  1. ServerTransferStateModule on the server side and BrowserTransferStateModule on the client side to use TransferState

https://angular.io/api/platform-browser/TransferState

P.S.: Note that if you do so and enhance your server, of course you would not need anymore to set the value in transfer-state in your getAlbum() method you displayed above

UPDATE 2

If you want to handle the server and browser side as you did in your gallery.component.ts, you could do something like the following:

getAlbum(albumId: number) {

    if (isPlatformServer(this.platformId)) {

      if (!this.albumPhotos) {
        this.facebookService.getBachadiffAlbumPhotos(albumId).subscribe(res => {
          this.bachataPicsArray = res;
          this.state.set(ALBUM_PHOTOS_KEY, null);
        });
      }
    } else {

      this.albumPhotos = this.state.get(ALBUM_PHOTOS_KEY,null);
    }

  }

UPDATE 3

The thing is, your action getAlbum is never called on the server side. This action is only used on the browser side, once the page is rendered, when the user click on a specific action. Therefore, using transfer-state in that specific case isn't correct/needed.

Furthermore not sure that the Observable in your service was correctly subscribed.

Here what to change to make it running:

gallery.component.ts

getAlbum(albumId: number) {

    this.facebookService.getBachadiffAlbumPhotos(albumId).subscribe(res => {
        this.albumPhotos = res;
    });
  }

facebook-events.service.ts

getBachadiffAlbumPhotos(albumId: number): Observable<Object> {

    this.albumId = albumId;
    this.facebookAlbumPhotosUrl =    `https://graph.facebook.com/v2.11/${this.albumId}/photos?limit=20&fields=images,id,link,height,width&access_token=${this.accessToken}`;

    return    Observable.fromPromise(this.getPromiseBachaDiffAlbumPhotos(albumId));
  }

private getPromiseBachaDiffAlbumPhotos(albumId: number): Promise<{}> {
    return new Promise((resolve, reject) => {
      this.facebookAlbumPhotosUrl = `https://graph.facebook.com/v2.11/${this.albumId}/photos?limit=20&fields=images,id,link,height,width&access_token=${this.accessToken}`;

      let facebookPhotos: FacebookPhoto[] = new Array();
      let facebookPhoto: FacebookPhoto;

      const params: HttpParams = new HttpParams();
      this.http.get(this.facebookAlbumPhotosUrl, {params: params})
        .subscribe(res => {

          let facebookPhotoData = res['data'];

          for (let photo of facebookPhotoData) {

            facebookPhotos.push(
              facebookPhoto = {
                id: photo.id,
                image: photo.images[3].source,
                link: photo.link,
                height: photo.height,
                width: photo.width
              });
          }

          resolve(facebookPhotos);
        }, (error) => {
          reject(error);
        });
    });
  }

UPDATE 4

ngOnInit is executed on the server side, this means that my very first answer here has to be use in this case.

Furthermore, also note that on the server side you doesn't have access to the window, therefore calling $

With gallery.component.ts you could do something like this to run only the http queries once but this won't solve all your problems, I think it will still need further improvements.

ngOnInit() {
    if (isPlatformServer(this.platformId)) {
      this.facebookService.getBachadiffFacebookVideos().subscribe(res => {
        this.bachataVidsArray = res;
        this.state.set(VIDEOS_KEY, res as any);
      });


  this.facebookService.getBachadiffFacebookLastClassPictures().subscribe(res => {
        this.bachataPicsArray = res;
        this.state.set(LAST_CLASS_PICTURES_KEY, res as any);
      });

      this.facebookService.getBachadiffAlbumNames().subscribe(res => {
        this.bachataAlbumHeaderNames = res;
        this.state.set(ALBUM_NAMES_KEY, res as any);
      });
    } else {
      $('ul.tabs').tabs();

      this.bachataVidsArray = this.state.get(VIDEOS_KEY, null as any);
      this.bachataPicsArray = this.state.get(LAST_CLASS_PICTURES_KEY, null as any);
      this.bachataAlbumHeaderNames = this.state.get(ALBUM_NAMES_KEY, null as any);
    }
  }

这篇关于Angular Transfer State 不会阻止重复的 http 调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆