在角2 prevent内存泄漏? [英] Prevent memory leaks in Angular 2?

查看:109
本文介绍了在角2 prevent内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在角2是有关于内存管理任何具体的陷阱,我应该知道的?

什么是管理,以避免可能的泄漏组件的状态的最佳实践?

具体来说,我见过一些人从HTTP观测退订的在 ngOnDestroy 方法。我要始终做到这一点?

在角1.X我知道,当被摧毁了一个 $范围,它所有的听众都自动销毁为好,。关于角2成分观测什么?

  @Component({
  选择:图书馆,
  模板:`
    < TR * ngFor =#书籍的书|异步>
        &所述; TD> {{book.title.text}}&下; / TD>
        &所述; TD> {{book.author.text}}&下; / TD>
    < / TR>
  `
})
出口类库{
    书:可观察<任何取代;    构造函数(私人后端:后端){
        this.books = this.backend.get('/文'); //< - 它被摧毁
                                                 与组分//?
    }
};


解决方案

由于所要求的@katspaugh

在您的具体情况下,没有必要手动取消,因为那是异步管道的工作。

检查<一个href=\"https://github.com/angular/angular/blob/master/modules/angular2/src/common/pipes/async_pipe.ts#L73-L77\"相对=nofollow>来源$ C ​​$ C 以AsyncPipe。为了简便起见,我张贴的有关code

类AsyncPipe实现PipeTransform,{的OnDestroy
    // ...
    ngOnDestroy(){无效
        如果(是present(this._subscription)){
          this._dispose();
        }
    }

正如你所看到的异步管道实现的OnDestroy,当它的摧毁它会检查是否有一些订阅和删除。

您将重塑在这种特殊情况下的车轮(对不起,重复自己)。这并不意味着你不能/不应该退订自己在任何其他情况下,像你提到的之一。在这种情况下,用户通过组件之间的观测所以这是很好的做法手动退订沟通他们。

我不知道是否该框架可以检测到任何的活着的订阅和他们的退订时自动成分被破坏,那将需要当然更多的调查。

我希望这澄清了一些关于异步管道。

In Angular 2 are there any specific pitfalls regarding memory management, I should be aware of?

What are the best practices to manage the state of components in order to avoid possible leaks?

Specifically, I've seen some people unsubscribing from HTTP observables in the ngOnDestroy method. Should I always do that?

In Angular 1.X I know that when a $scope is destroyed, all listeners on it are destroyed as well, automatically. What about observables in Angular 2 components?

@Component({
  selector: 'library',
  template: `
    <tr *ngFor="#book of books | async">
        <td>{{ book.title.text }}</td>
        <td>{{ book.author.text }}</td>
    </tr>
  `
})
export class Library {
    books: Observable<any>;

    constructor(private backend: Backend) {
        this.books = this.backend.get('/texts'); // <-- does it get destroyed
                                                 //     with the component?
    }
};

解决方案

As requested by @katspaugh

In your specific case there's no need to unsubscribe manually since that's the Async pipe's job.

Check the source code for AsyncPipe. For brevity I'm posting the relevant code

class AsyncPipe implements PipeTransform, OnDestroy {
    // ...
    ngOnDestroy(): void {
        if (isPresent(this._subscription)) {
          this._dispose();
        }
    }

As you can see the Async pipe implements OnDestroy, and when it's destroyed it checks if is there some subscription and removes it.

You would be reinventing the wheel in this specific case (sorry for repeating myself). This doesn't mean you can't/shouldn't unsubscribe yourself in any other case like the one you referenced. In that case the user is passing the Observable between components to communicate them so it's good practice to unsubscribe manually.

I'm not aware of if the framework can detect any alive subscriptions and unsubscribe of them automatically when Components are destroyed, that would require more investigation of course.

I hope this clarifies a little about Async pipe.

这篇关于在角2 prevent内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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