Answers for "behaviorsubject vs subject Angular"

0

behaviorsubject in angular 10

@Injectable()
export class TodoService {
  private _todos = new BehaviorSubject<Todo[]>([]);
  private baseUrl = 'https://56e05c3213da80110013eba3.mockapi.io/api';
  private dataStore: { todos: Todo[] } = { todos: [] };
  readonly todos = this._todos.asObservable();

  constructor(private http: HttpClient) {}

  get todos() {
    return this._todos.asObservable();
  }

  loadAll() {
    this.http.get(`${this.baseUrl}/todos`).subscribe(
      data => {
        this.dataStore.todos = data;
        this._todos.next(Object.assign({}, this.dataStore).todos);
      },
      error => console.log('Could not load todos.')
    );
  }
}
Posted by: Guest on July-27-2020
0

behaviorsubject vs subject Angular

A BehaviorSubject holds one value. When it is subscribed it emits the value immediately.
 A Subject doesn't hold a value.

Subject example (with RxJS 5 API):

const subject = new Rx.Subject();
subject.next(1);
subject.subscribe(x => console.log(x));

BehaviorSubject example:
const subject = new Rx.Subject();
subject.next(1);
subject.subscribe(x => console.log(x));
Posted by: Guest on October-25-2021
0

behaviorsubject in angular 10

@Injectable()
export class TodoService {
  private _todos = new BehaviorSubject<Todo[]>([]);
  private baseUrl = 'https://56e05c3213da80110013eba3.mockapi.io/api';
  private dataStore: { todos: Todo[] } = { todos: [] };
  readonly todos = this._todos.asObservable();

  constructor(private http: HttpClient) {}

  get todos() {
    return this._todos.asObservable();
  }

  loadAll() {
    this.http.get(`${this.baseUrl}/todos`).subscribe(
      data => {
        this.dataStore.todos = data;
        this._todos.next(Object.assign({}, this.dataStore).todos);
      },
      error => console.log('Could not load todos.')
    );
  }
}
Posted by: Guest on July-27-2020
0

behaviorsubject vs subject Angular

A BehaviorSubject holds one value. When it is subscribed it emits the value immediately.
 A Subject doesn't hold a value.

Subject example (with RxJS 5 API):

const subject = new Rx.Subject();
subject.next(1);
subject.subscribe(x => console.log(x));

BehaviorSubject example:
const subject = new Rx.Subject();
subject.next(1);
subject.subscribe(x => console.log(x));
Posted by: Guest on October-25-2021

Browse Popular Code Answers by Language