简体   繁体   English

使用Firebase执行角度2应用程序的性能

[英]Performance of an angular 2 application with Firebase

I have been creating a web application using angular2 with firebase (angularfire2), I want to know if my development method is optimized or not. 我一直在使用带有firebase(angularfire2)的angular2创建一个web应用程序,我想知道我的开发方法是否已经优化。

When user select a group, I check if he is already member of the group. 当用户选择一个组时,我会检查他是否已经是该组的成员。

ngOnInit() {
    this.af.auth.subscribe(auth => {
      if(auth) {
        this.userConnected = auth;
      }
    });

    this.router.params.subscribe(params=>{
      this.idgroup=params['idgroup'];
    });
    this._groupService.getGroupById(this.idgroup).subscribe(
      (group)=>{
        this.group=group;
          this.AlreadyPaticipe(this.group.id,this.userConnected.uid),
      }
    );
}

this method is work, but when I place the function AlreadyPaticipe(this.group.id,this.userConnected.uid) outside getGroupById(this.idgroup).subscribe() ,I get an error group is undefinded ,I now because angular is asynchrone. 这个方法是有效的,但是当我将函数AlreadyPaticipe(this.group.id,this.userConnected.uid)放在getGroupById(this.idgroup).subscribe() ,我得到一个错误组是未定的,我现在因为angular是asynchrone。 I don't khow how I can do it?. 我不知道我怎么能这样做? How I can optimize my code ?,How I can place the function AlreadyPaticipe(this.group.id,this.userConnected.uid) outside getGroupById(this.idgroup).subscribe() 我如何优化我的代码?,我如何将函数AlreadyPaticipe(this.group.id,this.userConnected.uid) getGroupById(this.idgroup).subscribe()

Thanks in advance. 提前致谢。

Everything as stream : 一切都像流:

Well first, you shouldn't subscribe that much, the best practice is to combine your observables into one and subscribe to it just once, because everytime you subscribe, you need to cleanup when your component is destroyed (not for http, neither ActivatedRoute though) and you end up managing your subscription imperatively (which is not the aim of RXjs). 首先,您不应该订阅那么多,最好的做法是将您的observable合并为一个并只订阅一次,因为每次订阅时,您需要在组件被销毁时进行清理(不是http,不是ActivatedRoute虽然并且你最终必须管理你的订阅(这不是RXjs的目标)。 You can find a good article on this topic here . 你可以在这里找到关于这个主题的好文章

You must think everything as a stream, all your properties are observables : 您必须将所有内容都视为流,所有属性都是可观察的:

this.user$ = this.af.auth.share(); //not sure of the share, I don't know firebase, don't know what it implies...
this.group$ = this.router.params.map(params => params["idgroup"])
    .switchMap(groupID => this.groupService.getGroupById(groupID)).share();
// I imagine that AlreadyPaticipe return true or false, but maybe i'm wrong
this.isMemberOfGroup$ = Observable.combineLatest(
    this.group$,
    this.user$.filter(user => user !== null)
).flatMap(([group, user]) => this.AlreadyPaticipe(groupID, user.uid));

You don't even have to subscribe ! 你甚至不必订阅! in your template you just need to use the async pipe. 在模板中,您只需要使用async管道。 for example: 例如:

<span>user: {{user$|async}}</span>
<span>group : {{group$|async}}</span>
<span>member of group : {{isMemberOfGroup$|async}}</span>

Or if you don't want to use the pipe, you can combine all those observable and subscribe only once : 或者,如果您不想使用管道,您可以将所有可观察的和仅订阅组合一次:

this.subscription = Observable.combineLatest(
    this.group$,
    this.user$,
    this.isMemberOfGroup$
).do(([group, user, memberofGroup]) => {
    this.group = group;
    this.user = user;
    this.isMemberofGroup = memberofGroup;
}).subscribe()

in this case, don't forget to this.subscription.unsubscribe() in ngOnDestroy() 在这种情况下,不要忘记this.subscription.unsubscribe()中的ngOnDestroy()

there is a very handy tool on rxJS docs (at the bottom of the page) that helps you to choose the right operator for the right behavior. rxJS文档 (位于页面底部)有一个非常方便的工具,可以帮助您为正确的行为选择合适的操作符。

I don't care about streams, I want it to work, quick n' dirty : 我不关心流,我希望它工作,快速n'脏:

If You don't want to change your code too much, you could use a Resolve guard that will fetch the data before your component is loaded. 如果您不想过多地更改代码,可以使用Resolve防护,它将在加载组件之前获取数据。 Take a look at the docs : 看看文档

In summary, you want to delay rendering the routed component until all necessary data have been fetched. 总之,您希望延迟渲染路由组件,直到获取所有必需的数据。 You need a resolver. 你需要一个解析器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM