नष्ट किए गए दृश्य का उपयोग करने का प्रयास करें: पता लगाना


79

मैं Angular Meteor 2 का उपयोग करके एक साधारण UI बना रहा हूं।

1) मेरे पास एक शीर्ष नावबार घटक है जिसमें एक 'लॉगआउट' बटन है।
2) 'लॉगआउट' बटन पर क्लिक करने पर यह 'लॉगिन' पर रीडायरेक्ट हो जाता है।
3) तब मुझे कंसोल में यह त्रुटि दिखाई देती है:EXCEPTION: Attempt to use a destroyed view: detectChanges

अपवाद:

EXCEPTION: Attempt to use a destroyed view: detectChanges
browser_adapter.js:77 EXCEPTION: Attempt to use a destroyed view: detectChangesBrowserDomAdapter.logError @ browser_adapter.js:77BrowserDomAdapter.logGroup @ browser_adapter.js:87ExceptionHandler.call @ exception_handler.js:57(anonymous function) @ application_ref.js:265schedulerFn @ async.js:123SafeSubscriber.__tryOrUnsub @ Subscriber.js:225SafeSubscriber.next @ Subscriber.js:174Subscriber._next @ Subscriber.js:124Subscriber.next @ Subscriber.js:88Subject._finalNext @ Subject.js:128Subject._next @ Subject.js:120Subject.next @ Subject.js:77EventEmitter.emit @ async.js:112onError @ ng_zone.js:120onHandleError @ ng_zone_impl.js:66ZoneDelegate.handleError @ angular2-polyfills.js:394Zone.runTask @ angular2-polyfills.js:323ZoneTask.invoke @ angular2-polyfills.js:490
browser_adapter.js:77 STACKTRACE:BrowserDomAdapter.logError @ browser_adapter.js:77ExceptionHandler.call @ exception_handler.js:59(anonymous function) @ application_ref.js:265schedulerFn @ async.js:123SafeSubscriber.__tryOrUnsub @ Subscriber.js:225SafeSubscriber.next @ Subscriber.js:174Subscriber._next @ Subscriber.js:124Subscriber.next @ Subscriber.js:88Subject._finalNext @ Subject.js:128Subject._next @ Subject.js:120Subject.next @ Subject.js:77EventEmitter.emit @ async.js:112onError @ ng_zone.js:120onHandleError @ ng_zone_impl.js:66ZoneDelegate.handleError @ angular2-polyfills.js:394Zone.runTask @ angular2-polyfills.js:323ZoneTask.invoke @ angular2-polyfills.js:490
browser_adapter.js:77 Error: Attempt to use a destroyed view: detectChanges
    at ViewDestroyedException.BaseException [as constructor] (http://localhost:3000/packages/modules.js?hash=560db94ec01c0b3e8f499491ffcce7a2ec6c3c5e:3349:23)
    at new ViewDestroyedException (http://localhost:3000/packages/modules.js?hash=560db94ec01c0b3e8f499491ffcce7a2ec6c3c5e:10626:16)
    at DebugAppView.AppView.throwDestroyedError (http://localhost:3000/packages/modules.js?hash=560db94ec01c0b3e8f499491ffcce7a2ec6c3c5e:11277:72)
    at DebugAppView.AppView.detectChanges (http://localhost:3000/packages/modules.js?hash=560db94ec01c0b3e8f499491ffcce7a2ec6c3c5e:11230:18)
    at DebugAppView.detectChanges (http://localhost:3000/packages/modules.js?hash=560db94ec01c0b3e8f499491ffcce7a2ec6c3c5e:11321:44)
    at ViewRef_.detectChanges (http://localhost:3000/packages/modules.js?hash=560db94ec01c0b3e8f499491ffcce7a2ec6c3c5e:11011:65)
    at http://localhost:3000/app/app.js?hash=323b1216814e80ed467d95bcda255eb217d7c468:2224:23
    at ZoneDelegate.invokeTask (http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:4721:174)
    at Object.onInvokeTask (http://localhost:3000/packages/modules.js?hash=560db94ec01c0b3e8f499491ffcce7a2ec6c3c5e:9393:41)
    at ZoneDelegate.invokeTask (http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:4721:72)
  -------------   Elapsed: 80 ms; At: Wed Jun 15 2016 20:22:09 GMT-0700 (PDT)   -------------  
    at Object.onScheduleTask (http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:5734:30)
    at ZoneDelegate.scheduleTask (http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:4704:57)
    at Zone.scheduleMacroTask (http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:4652:47)
    at http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:4467:37
    at setTimeout (eval at createNamedFn (http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:5346:24), <anonymous>:3:37)
    at new TopNavbarComponent (http://localhost:3000/app/app.js?hash=323b1216814e80ed467d95bcda255eb217d7c468:2221:9)
    at DebugAppView._View_HomeComponent0.createInternal (HomeComponent.template.js:48:34)
    at DebugAppView.AppView.create (http://localhost:3000/packages/modules.js?hash=560db94ec01c0b3e8f499491ffcce7a2ec6c3c5e:11098:21)
  -------------   Elapsed: 2 ms; At: Wed Jun 15 2016 20:22:09 GMT-0700 (PDT)   -------------  
    at Object.onScheduleTask (http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:5734:30)
    at ZoneDelegate.scheduleTask (http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:4704:57)
    at Zone.scheduleMicroTask (http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:4649:47)
    at scheduleResolveOrReject (http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:4928:22)
    at resolvePromise (http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:4893:29)
    at http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:4930:25
    at ZoneDelegate.invokeTask (http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:4721:174)
    at Object.onInvokeTask (http://localhost:3000/packages/modules.js?hash=560db94ec01c0b3e8f499491ffcce7a2ec6c3c5e:9393:41)
  -------------   Elapsed: 0 ms; At: Wed Jun 15 2016 20:22:09 GMT-0700 (PDT)   -------------  
    at Object.onScheduleTask (http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:5734:30)
    at ZoneDelegate.scheduleTask (http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:4704:57)
    at Zone.scheduleMicroTask (http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:4649:47)
    at scheduleResolveOrReject (http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:4928:22)
    at ZoneAwarePromise.then (http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:5011:25)
    at RuntimeCompiler.resolveComponent (http://localhost:3000/packages/modules.js?hash=560db94ec01c0b3e8f499491ffcce7a2ec6c3c5e:40230:14)
    at DynamicComponentLoader_.loadNextToLocation (http://localhost:3000/packages/modules.js?hash=560db94ec01c0b3e8f499491ffcce7a2ec6c3c5e:10788:31)
    at RouterOutlet.activate (http://localhost:3000/packages/modules.js?hash=560db94ec01c0b3e8f499491ffcce7a2ec6c3c5e:26844:26)
  -------------   Elapsed: 0 ms; At: Wed Jun 15 2016 20:22:09 GMT-0700 (PDT)   -------------  
    at Object.onScheduleTask (http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:5734:30)
    at ZoneDelegate.scheduleTask (http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:4704:57)
    at Zone.scheduleMicroTask (http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:4649:47)
    at scheduleResolveOrReject (http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:4928:22)
    at resolvePromise (http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:4893:29)
    at http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:4877:21
    at ZoneDelegate.invoke (http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:4696:161)
    at Object.onInvoke (http://localhost:3000/packages/modules.js?hash=560db94ec01c0b3e8f499491ffcce7a2ec6c3c5e:9402:41)
  -------------   Elapsed: 0 ms; At: Wed Jun 15 2016 20:22:09 GMT-0700 (PDT)   -------------  
    at Object.onScheduleTask (http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:5734:30)
    at ZoneDelegate.scheduleTask (http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:4704:57)
    at Zone.scheduleMicroTask (http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:4649:47)
    at scheduleResolveOrReject (http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:4928:22)
    at resolvePromise (http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:4893:29)
    at http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:4877:21
    at ZoneDelegate.invoke (http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:4696:161)
    at Object.onInvoke (http://localhost:3000/packages/modules.js?hash=560db94ec01c0b3e8f499491ffcce7a2ec6c3c5e:9402:41)
  -------------   Elapsed: 0 ms; At: Wed Jun 15 2016 20:22:09 GMT-0700 (PDT)   -------------  
    at Object.onScheduleTask (http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:5734:30)
    at ZoneDelegate.scheduleTask (http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:4704:57)
    at Zone.scheduleMicroTask (http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:4649:47)
    at scheduleResolveOrReject (http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:4928:22)
    at resolvePromise (http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:4893:29)
    at http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:4930:25
    at ZoneDelegate.invokeTask (http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:4721:174)
    at Object.onInvokeTask (http://localhost:3000/packages/modules.js?hash=560db94ec01c0b3e8f499491ffcce7a2ec6c3c5e:9393:41)
  -------------   Elapsed: 1 ms; At: Wed Jun 15 2016 20:22:09 GMT-0700 (PDT)   -------------  
    at Object.onScheduleTask (http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:5734:30)
    at ZoneDelegate.scheduleTask (http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:4704:57)
    at Zone.scheduleMicroTask (http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:4649:47)
    at scheduleResolveOrReject (http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:4928:22)
    at ZoneAwarePromise.then (http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:5011:25)
    at http://localhost:3000/packages/modules.js?hash=560db94ec01c0b3e8f499491ffcce7a2ec6c3c5e:26895:53
    at http://localhost:3000/packages/meteor.js?hash=ae8b8affa9680bf9720bd8f7fa112f13a62f71c3:1105:22
    at ZoneDelegate.invoke (http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:4696:161)
  -------------   Elapsed: 0 ms; At: Wed Jun 15 2016 20:22:09 GMT-0700 (PDT)   -------------  
    at Object.onScheduleTask (http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:5734:30)
    at ZoneDelegate.scheduleTask (http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:4704:57)
    at Zone.scheduleMicroTask (http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:4649:47)
    at scheduleResolveOrReject (http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:4928:22)
    at resolvePromise (http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:4893:29)
    at http://localhost:3000/packages/barbatus_angular2-runtime.js?hash=fda9b73362c52e988ad030102a9f58e4d584cda3:4877:21
    at http://localhost:3000/packages/meteor.js?hash=ae8b8affa9680bf9720bd8f7fa112f13a62f71c3:1105:22BrowserDomAdapter.logError @ browser_adapter.js:77ExceptionHandler.call @ exception_handler.js:60(anonymous function) @ application_ref.js:265schedulerFn @ async.js:123SafeSubscriber.__tryOrUnsub @ Subscriber.js:225SafeSubscriber.next @ Subscriber.js:174Subscriber._next @ Subscriber.js:124Subscriber.next @ Subscriber.js:88Subject._finalNext @ Subject.js:128Subject._next @ Subject.js:120Subject.next @ Subject.js:77EventEmitter.emit @ async.js:112onError @ ng_zone.js:120onHandleError @ ng_zone_impl.js:66ZoneDelegate.handleError @ angular2-polyfills.js:394Zone.runTask @ angular2-polyfills.js:323ZoneTask.invoke @ angular2-polyfills.js:490
Subscriber.js:229 Uncaught Attempt to use a destroyed view: detectChanges

top-navbar.component.ts

"use strict";
import {Logger} from "../services/logger.service";
import {Component, ChangeDetectionStrategy, ChangeDetectorRef} from '@angular/core';
import {User} from "../models/user";
import {Router} from '@angular/router-deprecated';
import {UserService} from "../services/user.service";
import {CORE_DIRECTIVES} from '@angular/common';
import {DROPDOWN_DIRECTIVES} from '../../node_modules/ng2-bootstrap';

@Component({
    selector: 'top-navbar',
    templateUrl: 'client/top-navbar/top-navbar.html',
    bindings: [UserService, Logger],
    directives: [CORE_DIRECTIVES, DROPDOWN_DIRECTIVES]
})

export class TopNavbarComponent {

    public user:User;

    public statusDropdown = {
        isOpen: false
    };

    constructor(private userService:UserService, private router:Router, private logger:Logger, private ref:ChangeDetectorRef) {
        setTimeout(() => {
            this.ref.markForCheck();
            this.user = this.userService.getLoggedInUser();
            this.ref.detectChanges();
        }, 0)
    }

    logout() {
        this.logger.warn('[Top Navbar] Logging out the user.');
        localStorage.clear();
        this.router.navigateByUrl('/login');
    }
}

और यह मेरा login.component.ts है

"use strict";
import { Component, ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core';
import { FormBuilder, ControlGroup, Validators } from '@angular/common';
import { MeteorComponent } from 'angular2-meteor';
import { Router } from '@angular/router-deprecated';
import { Logger } from "../services/logger.service";

@Component({
    selector: 'login',
    templateUrl: 'client/login/login.html',
    changeDetection: ChangeDetectionStrategy.OnPush,
    bindings: [Logger]
})

export class LoginComponent extends MeteorComponent {

    loginForm:ControlGroup;
    loginFailed = false;

    constructor(private _logger:Logger, private _router:Router, private ref:ChangeDetectorRef) {
        super();
        let fb = new FormBuilder();
        this.loginForm = fb.group({
            username: ["", Validators.required],
            password: ["", Validators.required]
        });
    }

    login() {


        this.call('authenticateUser', this.loginForm.value.username, this.loginForm.value.password, (err, data) => {

            if (err) {
                this._logger.error(err);

            } else {
                this._logger.info('[Authentication API] ', data);

                if (data.status != 'LOGIN_SUCCESS') {
                    this.loginFailed = true;

                } else {
                    this.loginFailed = false    ;
                    var user = {
                        id: data.id,
                        name: data.name,
                        role: data.role
                    }
                    localStorage.setItem('user', JSON.stringify(user));
                    this._router.navigate(['Home'])
                }
                //This is required for letting Angular know that something has changed.
                //Because this part of code runs out of Angular zone.
                this.ref.markForCheck();  // Mark this component and its children for change detection in next detecting cycle.
                this.ref.detectChanges(); // Trigger change detection.

            }

        });
    }
}

पता लगाने के लिए कॉल टिप्पणी (); फ़ंक्शन और जांचें कि इसकी कुछ अन्य त्रुटि कहां उत्पन्न होती है
mayur

मैं एक ही परिदृश्य के साथ एक ही समस्या है।
अल-मोताफ़र

जवाबों:


97

केवल समाधान जो मेरे लिए काम करता था:

if (!this.changeDetectionRef['destroyed']) {
    this.changeDetectionRef.detectChanges();
}

1
यहाँ भी, धन्यवाद! हालांकि मैंने isDestroyedप्रत्येक घटक में एक निजी क्षेत्र को जोड़ने का विकल्प चुना, जहां इसकी आवश्यकता थी (यानी जहां चीजें अनसब्सक्राइब करना पर्याप्त नहीं था)। मैंने एनजीऑनेस्ट्रो में क्षेत्र को सही करने के लिए सेट किया, और परिवर्तन डिटेक्टर रेफरी के आंतरिक क्षेत्र तक पहुंचने की कोशिश करने के बजाय इसका उपयोग किया।
dbandstra

2
हालांकि यह उत्तर आपके ऐप को उड़ाने से त्रुटि को रोकता है, यह मूल कारण को कवर कर रहा है। अल-Mothafar सही समाधान यहाँ जो करने के लिए है घटक है कि अब और नहीं प्रदान की गई है। detachcdr
मैट वेस्टलेक

5
यह वास्तव में काम करता है! दूसरी ओर ऐसा प्रतीत होता destroyedहै कि changeDetectionRefवस्तु की सार्वजनिक एपीआई का हिस्सा नहीं है । वहाँ के माध्यम से यह आरोप लगाने के बिना इसे लागू करने के लिए एक रास्ता है ['destroyed']?
lealceldeiro

2
@Lealceldeiro से सहमत हैं। टाइपस्क्रिप्ट के नए संस्करणों के साथ, हम अब ऐसा करने में सक्षम नहीं हैं।
क्रिलगर

1
आप !(this.changeDetectionRef as ViewRef).destroyedइसे थोड़ा कम हैकी बनाने के लिए उपयोग कर सकते हैं, लेकिन यह इन दिनों अभी भी बहुत हैकी है
इलेक्ट्रोवायर

86

मैंने आपका एक ही मुद्दा हल किया है, लेकिन बहुत छोटे कोड के साथ, मैं आपको वह बिंदु बताऊंगा जो आपको समस्या को हल करने में मदद कर सकता है।

मुद्दा स्पष्ट रूप से आता है detectChanges() क्योंकि परिवर्तन किए गए थे और घटक के विनाश चरण के दौरान कहा गया था।

इसलिए आपको अपने घटक को बनाने की आवश्यकता है implements OnDestroy, फिर आपको उन परिवर्तनों को रद्द करने की आवश्यकता this.ref.detectChanges()है जिन्हें कॉल किया जाना है। तो आपका TopNavbarComponentसमान होना चाहिए:

export class TopNavbarComponent implements OnDestroy {
  // ... your code

  ngOnDestroy() {
    this.cdRef.detach(); // do this

    // for me I was detect changes inside "subscribe" so was enough for me to just unsubscribe;
    // this.authObserver.unsubscribe();
  }
}

पुनश्च: unsubscribe()सभी पर्यवेक्षकों कि आप अपने घटक में है मत भूलना ! वैसे भी आपको ऐसा करना होगा, बिना सदस्यता के सदस्यताएँ इस सहित सैकड़ों मुद्दों का मुख्य कारण हो सकता है, कोणीय / RxJs देखें जब मुझे `सदस्यता 'से सदस्यता समाप्त करनी चाहिए

संपादित करें : मैं वेब में चारों ओर अन्य समाधान जानता हूं, केवल त्रुटि को संभालकर समस्या को हल करने की कोशिश कर रहा हूं, जबकि सबसे अच्छा अभ्यास मुद्दे की जड़ को जानना है, जांचें कि क्या दृश्य नष्ट हो गया है या नहीं यह एक अच्छा समाधान है लेकिन मूल कारण मेमोरी लीक होने के पीछे एक समस्या हो सकती है, इसलिए समस्या की जड़ यह है कि रनिंग सर्विस को मारने की आवश्यकता है न कि केवल रूट के बिना त्रुटि को मारने का प्रयास करें, उदाहरण के लिए, रनिंग सब्सक्रिप्शन (विशेष रूप से आपका कस्टम एक) बंद होना चाहिए।

बेहतर प्रदर्शन के लिए एसओ हाउसकीपिंग सामान आवश्यक है, यह हमेशा एक आसान और तेज समाधान नहीं होता है, यदि आप कालीनों के नीचे गंदगी छिपाते हैं तो इसका मतलब यह नहीं है कि आपने अपने कमरे को साफ किया है :)


जब मैंने ऐसा किया, तो मुझे ngOnDestory फंक्शन पर यह त्रुटि मिली: कंसोल.जेएस: 26 त्रुटि त्रुटि: अनचेक (वादे में):
टाइपर्रर:

इसका मतलब है कि आपकी ChangeDetectorRefसेवा का नाम गलत है? यदि आपके पास private cdRef: ChangeDetectorRefकंस्ट्रक्टर में है तो यह काम करना चाहिए।
अल-मोताफ़र

1
detectChangesविध्वंस चरण के दौरान विधि ( ) को क्यों कहा जाएगा?
डोंगबिन किम

1
@DongBinKim यदि आपने चालू किया detectChangesऔर चरण को नष्ट करने के दौरान परिवर्तन हुए, तो यह फ़ंक्शन सेवाओं के साथ ही मरने से पहले मृत घटक पर परिवर्तन लागू करना शुरू कर देगा, इसलिए मुद्दा यह होगा, यह कोणीय के लिए दृश्य जीवनचक्र से संबंधित कुछ है, वैसे भी आप कर सकते हैं इस लिंक को देखें angular.io/api/core/ChangeDetectorRef#detectChanges और angular.io/guide/lifecycle-hooks जैसा कि आप देख सकते हैं DOM स्टे और मेमोरी में सर्विस बदलाव को लागू करने की कोशिश करेगी, जबकि कंपोनेंट खुद मारा गया।
अल-मोतफर

2
यह समाधान मेरे काम नहीं आया। वास्तव में पता लगाने की विधि के लिए कोड टिप्पणी से पता चलता है कि पता लगाने में परिवर्तन डिटेक्टर को फिर से जोड़ना होगा। मेरे लिए एकमात्र काम करने वाला समाधान टोमसज़बाक विधि थी, या हमेशा अनसब्सक्राइब / क्लियर टाइमआउट / आदि।
dbandstra

15

आप उपयोग कर सकते हैं

this.cdref.markForCheck();

बजाय this.cdref.detectChanges(); कई मामलों में। लेकिन सबसे अच्छा तरीका @ अल-मोताफ़र सुझावों का पालन करना है



6

बस OnDestroy जीवन चक्र हुक पर ChangeDetectorRef को अलग करें और CheckCetectorRef का पता लगाने की विधि को निष्पादित करने से पहले नष्ट कर दिया गया है या नहीं इसकी जाँच करें।

    constructor(private cd: ChangeDetectorRef){}

    someFunction(){
      if(!this.cd['destroyed']){
        this.cd.detectChanges();
      }
    }

    ngOnDestroy(){
      this.cd.detach();
    }

क्या यह .cd.detach () आवश्यक नष्ट करने पर है?
किंगमेरियो

जब घटक अब प्रदान नहीं किया जाता है ChangeDetectorRef को अलग किया जा सकता है। यह आवश्यक नहीं है, लेकिन ऐसा करने से हम सुनिश्चित करते हैं कि नष्ट हुए दृश्य में कोई परिवर्तन नहीं हो रहा है। दूसरी ओर, यदि आप वास्तव में आश्वस्त हैं कि घटक में परिवर्तन का पता लगाने को अक्षम करने या बदलने की आवश्यकता नहीं है, तो आप किसी भी बिंदु पर परिवर्तन डिटेक्टर को अलग कर सकते हैं।
थिलिना पियादासुन

धन्यवाद थिलिना। मुझे लगता है कि this.cdनष्ट होने पर स्वचालित रूप से अलग हो जाएगा। और यदि ऐसा नहीं है, क्योंकि !this.cd['destroyed']पहले परीक्षण किया this.cd.detectChanges()गया है someFunction, तो आप someFunctionकिसी भी परिस्थिति में कॉल करने के लिए आश्वस्त होंगे ।
KingMario

5

आपको एक चर में ग्राहक का मूल्य प्राप्त करना होगा और एक ही संस्करण द्वारा सदस्यता समाप्त करनी होगी। कृपया समान कोड का अनुसरण करें

import { Component, OnInit, ChangeDetectionStrategy, ChangeDetectorRef, OnDestroy } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';
import { Cartservice } from './../cartservice.service';
import { ISubscription } from 'rxjs/Subscription';



export class CartComponent implements OnInit, OnDestroy {

private subscription: ISubscription;
ngOnInit() {
    this.subscription = this.cartservice.productsObservable.subscribe(cart => {
      this.cartProducts = cart.products;
      this.cartTotal = cart.cartTotal;
      this.changeDetectorRef.detectChanges();
    });
  }

 ngOnDestroy() {
    this.subscription.unsubscribe();
  }
}

ध्यान दें कि मेरे पास विधि ngOnDestroy () में परिवर्तन अनसब्सक्राइब हैं।


4

मेरा समाधान सभी पर्यवेक्षकों को सदस्यता समाप्त करना था।

अंशदान:

ngOnInit() {
     this._currentUserSubscription = this._auth.currentUser.subscribe(currentUser => {});
}

ChangeDetector.detach के साथ सदस्यता रद्द करें ():

ngOnDestroy() {
    this._currentUserSubscription.unsubscribe();
    this._cdRef.detach();
}

मेरे कोड पर यह गलत था, मुझे भी ChangeDetectorRef कार्यक्षमता का उपयोग करना चाहिए, केवल उन दो चीजों को त्रुटियों के बिना मेरे कोड से अलग किया गया।


2

मेरे मामले में, यह घटक विन्यास और संकलन के async परीक्षण सेटअप को गलत करने वाला मामला था।

  1. असफल कोड में टाइपस्क्रिप्ट Async / Await का उपयोग करने से पहले एक विलक्षण async () था
  2. यह (कार्य करने के लिए, मैं दो beforeEach उपयोग कर रहा हूँ) है, जहां का उपयोग कर Async पहले हैंडल कोणीय के async () , और दूसरा beforeEach () सिंक है

कोड के कारण त्रुटियां ..

beforeEach(async () => {
    await TestBed.configureTestingModule({
        imports: [
            BrowserAnimationsModule
        ],
        providers: [
            { provide: ComponentFixtureAutoDetect, useValue: true },
            { provide: OptionsService, useValue: optionServiceMock },
        ],
        declarations: [EventLogFilterComponent],
        schemas: [NO_ERRORS_SCHEMA]
    }).compileComponents();
    fixture = TestBed.createComponent(EventLogFilterComponent);
    component = fixture.componentInstance;
    optionsService = TestBed.get(OptionsService);
    component.filterElem = jasmine.createSpyObj('filterElem', ['close']);
    fixture.detectChanges();
});

के साथ तय किया गया था ...

beforeEach(async(() => {
    TestBed.configureTestingModule({
        imports: [
            BrowserAnimationsModule
        ],
        providers: [
            { provide: ComponentFixtureAutoDetect, useValue: true },
            { provide: OptionsService, useValue: optionServiceMock },
        ],
        declarations: [EventLogFilterComponent],
        schemas: [NO_ERRORS_SCHEMA]
    }).compileComponents();
}));

beforeEach(() => {
    fixture = TestBed.createComponent(EventLogFilterComponent);
    component = fixture.componentInstance;
    optionsService = TestBed.get(OptionsService);
    component.filterElem = jasmine.createSpyObj('filterElem', ['close']);
    fixture.detectChanges();
});

2

विशिष्ट प्रश्न से बहुत अधिक संबंधित नहीं है, लेकिन मैं उसी त्रुटि को भूलकर यहां पहुंच गया हूं, इसलिए मैं अपना समाधान साझा करूंगा। समस्या यह थी कि मैं एक फ़ंक्शन के भीतर परीक्षण लपेटे बिना fixture.detectChanges()अंदर बुला रहा था ।fixture.whenStable().then(() => {})async

इससे पहले:

it('should...', () => {
  fixture.whenStable().then(() => {
    fixture.detectChanges();
  });
});

उपरांत:

it('should...', async(() => {
  fixture.whenStable().then(() => {
    fixture.detectChanges();
  });
}));

1

सरल:

import { OnDestroy } from '@angular/core';
import { Subject } from 'rxjs/Subject';

export class Component implements OnDestroy {
    componentDestroyed: Subject<boolean> = new Subject();

constructor() { }

function() {
   this.service.serviceFunction
     .takeUntil(this.componentDestroyed)
     .subscribe((incomingObservable: Type) => {
       this.variable = incomingObservable;
     });
  }

ngOnDestroy() {
   this._cdRef.detach(); //If you use change dectector
   this.componentDestroyed.next(true);
   this.componentDestroyed.complete();
}

1

मेरा कारण तब हुआ जब मैं NgbActiveModal के साथ एक मोडल संवाद खोलने की कोशिश कर रहा था। जाहिरा तौर .dismiss()पर अपने स्वयं के कॉल से ngOnInit()यह कारण बनता है।

export class MyModal {
    constructor(private modalInstance: NgbActiveModal) {
    }

    ngOnInit() {
       if (foo) this.modalInstance.dismiss();
    }

समाधान बर्खास्तगी से पहले एक टिक इंतजार करना था।

if (foo) setTimeout(() => this.modalInstance.dismiss());


1

खैर इस जवाब से मुझे कोई फायदा नहीं हुआ। मुझे अन्य समाधान मिला।

चाइल्ड कंपोनेंट में आउटपुट होता है, जब क्लोज़ बटन पर क्लिक किया जाता है

<child-component
    *ngIf="childComponentIsShown"
    (formCloseEmitter)="hideChildComponent()"
></child-component>

और मूल घटक में "HideChildComponent ()" विधि परिवर्तन का पता लगाती है।

hideChildComponent() {
  this.childComponentIsShown = false;
  this.cdr.detectChanges();
}

आशा है कि यह किसी की मदद करेगा।


1

मेरे लिए, एकमात्र समाधान जो काम करता है वह निम्नलिखित है:

ngOnInit() {
    if (this.destroyedComponent) this.changeDetector.reattach();

this.destroyedComponent = false;
this.subscription = this.reactive.channel$.subscribe(msg => {
  switch (msg) {
    case "config:new_data":
      if (!this.destroyedComponent) {
        this.table.initTable();
        this.changeDetector.detectChanges();
      }
  }
})
}

ngOnDestroy() {
   this.subscription = null;
   this.destroyedComponent = true;
   this.changeDetector.detach();
}

स्पष्टीकरण:

  1. यदि घटक को पहले से नष्ट कर दिया गया है, तो डिटेक्टर को रीटेट करें।
  2. उस पिछले झंडे को नकली मूल्य पर सेट करें।
  3. RxJs सदस्यता को सहेजें, और अपने इच्छित तर्क को अंदर सेट करें।
  4. उस तर्क को एक सशर्त में लपेटें जो यह जांचता है कि घटक को पहले से घोषित ध्वज द्वारा नष्ट कर दिया गया है या नहीं।
  5. उस ब्लॉक के अंदर परिवर्तनों की अपनी वांछित पहचान करें।
  6. NgOnDestroy () विधि सेट करें और सदस्यता को रद्द करें, नष्ट कर दिया गया फ्लैगशिप फ्लैगशिप को एक सत्य मान पर सेट करें, और चेंज डिटेक्टर को अलग करें।

1

इस त्रुटि से बचने के लिए, कॉलचेजेज को कॉल करने के बजाय (मॉडल कोड बदलने की कोशिश करें:

this.ngZone.run(() => {
      ...
});
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.