name : "ObjectUnsubscribedError" using angular

30 Views Asked by At

I have this bug on my code

ObjectUnsubscribedErrorImpl {message: 'object unsubscribed', name: 'ObjectUnsubscribedError', ngDebugContext: DebugContext_, ngErrorLogger: ƒ} message : "object unsubscribed" name : "ObjectUnsubscribedError"

I have this code:

 clickSubject: Subject<any> = new Subject();

  l_getClickButtonCancella(event: boolean) {
    if (event) {
      this.clickSubject.next(1);
    }
  }

Can you share with me any idea how to fix?

1

There are 1 best solutions below

1
Naren Murali On

Check your code where you are unsubscribing the subject anywhere ( Remove the unsubscribe on the subject to fix the error! ), that is the reason, please check the below stackblitz where I replicated your scenario!

import { Component } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import { Subject } from 'rxjs';
import 'zone.js';

@Component({
  selector: 'app-root',
  standalone: true,
  template: `
    <h1>Hello from {{ name }}!</h1>
    <a target="_blank" href="https://angular.dev/overview">
      Learn more about Angular
    </a>
  `,
})
export class App {
  clickSubject: Subject<any> = new Subject();
  name = 'Angular';

  ngOnInit() {
    this.clickSubject.next(1);
    this.clickSubject.unsubscribe();
    this.clickSubject.next(1);
  }
}

bootstrapApplication(App);

stackblitz