我应该销毁什么?我该怎么办

时间:2019-01-21 10:50:26

标签: angular lifecycle

我查看了有角度的文档和其他资源,但没有找到任何东西。 我在Angular7上工作。如何进行销毁过程?

3 个答案:

答案 0 :(得分:1)

import { Component, OnDestroy } from '@angular/core';

@Component({...})
export class MyComponent implements OnDestroy {
    ngOnDestroy(): void {
        // called when the component gets destroyed
    }
}

您可以在此处了解有关生命周期的更多信息:https://angular.io/guide/lifecycle-hooks

答案 1 :(得分:0)

See this scenario


import { Subscription } from 'rxjs';
  currentUserSub: Subscription;


  getCurrentUser() {
    this.currentUserSub = this.authenticationService.currentUser.subscribe(x => {
      if (x) {
        this.currentUser = x[0].MemberId;
      } else { }
    });



 ngOnDestroy() {
    // unsubscribe to ensure no memory leaks
    this.currentUserSub.unsubscribe();
  }


  }

答案 2 :(得分:0)

一种取消订阅组件中所有可观察对象的干净方法:

new webpack.optimize.CommonsChunkPlugin({
   name: 'common',
   filename: `${configuration.webpack.filenameTemplate}.js`,
   minChunks: 3,
}),