Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular是否清除Ngondestory上的属性或数组以防止内存泄漏?_Angular_Typescript_Angular9 - Fatal编程技术网

Angular是否清除Ngondestory上的属性或数组以防止内存泄漏?

Angular是否清除Ngondestory上的属性或数组以防止内存泄漏?,angular,typescript,angular9,Angular,Typescript,Angular9,我有以下代码:(9) employee.component.ts name:string; arr = []; ngOnInit() { this.name = "abc"; for (let i = 0; i < 1000; i++) { this.arr.push(i); } } 或者angular将清除Ngondestory上的数组和属性?您不需要这样做,因为这些变量将被垃圾收集您

我有以下代码:(9)

employee.component.ts

  name:string;
  arr = [];

  ngOnInit() {
    this.name = "abc";
    for (let i = 0; i < 1000; i++) {
            this.arr.push(i);
          }
    }

或者angular将清除Ngondestory上的数组和属性?

您不需要这样做,因为这些变量将被垃圾收集

您不需要这样做,因为这些变量将被垃圾收集

您不需要这样做,垃圾收集器将负责处理。只要确保您的组件没有订阅任何可观察的对象等。如果订阅/组件仍然有一些对根的引用,它将不会被垃圾收集

要处理订阅,您应使用
ngondestory
取消订阅所有订阅。以下是一个例子:

    @Component({...})
    export class AppComponent implements OnInit, OnDestroy {
        subscriptions: Subscription[] = [];
        ngOnInit () {
            var observable = Rx.Observable.interval(1000);
            var observable2 = Rx.Observable.interval(1000);
            this.subscriptions.push(observable.subscribe(x => console.log(x)));
            this.subscriptions.push(observable2.subscribe(x => console.log(x)));
        }
        ngOnDestroy() {
            this.subscriptions.forEach((subscription) => subscription.unsubscribe())
        }
    }

你不需要,垃圾收集器会处理的。只要确保您的组件没有订阅任何可观察的对象等。如果订阅/组件仍然有一些对根的引用,它将不会被垃圾收集

要处理订阅,您应使用
ngondestory
取消订阅所有订阅。以下是一个例子:

    @Component({...})
    export class AppComponent implements OnInit, OnDestroy {
        subscriptions: Subscription[] = [];
        ngOnInit () {
            var observable = Rx.Observable.interval(1000);
            var observable2 = Rx.Observable.interval(1000);
            this.subscriptions.push(observable.subscribe(x => console.log(x)));
            this.subscriptions.push(observable2.subscribe(x => console.log(x)));
        }
        ngOnDestroy() {
            this.subscriptions.forEach((subscription) => subscription.unsubscribe())
        }
    }

什么是自动收集的垃圾?示例中的属性。点击链接。在那里,您可以看到哪些资源不会自动释放。(通常是添加到应用程序其他部分的流和引用,这些内容不包含在组件本身中。)您能给出添加到应用程序其他部分的引用的示例吗?@chirag一个示例(本文中也提到)是,如果您将组件中的函数注册为另一个组件中的回调函数。在这种情况下,您必须注销以清除引用。什么是自动垃圾收集?示例中的属性。点击链接。在那里,您可以看到哪些资源不会自动释放。(通常是添加到应用程序其他部分的流和引用,这些内容不包含在组件本身中。)您能给出添加到应用程序其他部分的引用的示例吗?@chirag一个示例(本文中也提到)是,如果您将组件中的函数注册为另一个组件中的回调函数。在这种情况下,您必须取消注册以清除引用。首先,您需要初始化数组,否则将出现未定义的错误。其次,您可以将其声明为
subscriptions:Subscription=newsubscription()
。然后使用
add
而不是push,在destroy中,它只是
this.subscriptions.unsubscribe()
。首先需要初始化数组,否则会出现未定义的错误。其次,您可以将其声明为
subscriptions:Subscription=newsubscription()
。然后使用
add
而不是push,在destroy中,它只是
this.subscriptions.unsubscribe()