Google+

Friday, July 16, 2021

Using BehaviorSubject in Angular 12 : differences between Observable VS Subject VS BehaviorSubject

 In this article we review how to use BehaviorSubject in Angular 12 : and also the differences between Observable VS Subject VS BehaviorSubject.


Using  BehaviorSubject in Angular 12 : differences between Observable VS Subject VS BehaviorSubject 



A BehaviorSubject is a kind of observable so you can subscribe to messages sent by the PUBLISHER like any other observable. 

However, BehaviorSubject allows you to control the messages as well, and emit messages at observer's will!!

 In addition, BehaviorSubjects are multicast. A Subject or a BehaviorSubject are like an Observable, but can multicast to many Observers, while plain Observables are unicast (each subscribed Observer owns an independent execution of the Observable). 

That way, Subjects are like EventEmitters: they maintain a registry of many listeners.

Upon subscription, BehaviorSubject  returns the last value kept. However, a regular Observable or a Subject, only trigger when they receive an "NEXT" operation.
For this sake, BehaviorSubject  needs a DEFAULT value upon creation, as it must always return a value on SUBSCRIPTION TIME , even if it hasn't received a "NEXT" operation call.

We can retrieve the last value of the subject in a regular non-observable code by using the getValue() method.

The difference between an Observable and a Subject is that it is an observer in addition to being an observable : you can also edit and emit and send values from a Subject in addition to subscribing to it.

Behavior subject can be turned into observable  using the asObservable() method on BehaviorSubject.


This is a BehaviorSubject  example: DEFAULT VALUE + IMMEDIATE MESSAGE ON SUBSCRIPTION:

 

let bsnew BehaviorSubject("1"); 

bs.next("2");

bs.subscribe(value => {
  console.log("Value received : "value);   
  // Subscription got 2 !!!!!!!!!! However, an observable or subject wouldn's have send a message at this stage
});

bs.next("3"); // Subscription got 3
bs.next("4"); // Subscription got 4
  

And this is an example of Subject: NO DEFAULT, NO MESSAGE UNTIL "NEXT" method triggered:


let snew Subject(); 

s.next("2");

s.subscribe(value => {
  console.log("Value received :"value); 
  // Subscription wont get  any message at this point
});

s.next("3"); 
s.next("4");  

  



Example of BehaviorSubject use:


Following is an example of using BehaviorSubject:  here is the app to try
These TWO components pass data one another using a BehaviorSubject from a Service:


1) First, we define the BehaviorSubject on some Service:

import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';

@Injectable()
export class MyService {
  constructor() {}
  private bs = new BehaviorSubject<string>('default');
  obs$ = this.bs.asObservable();

  editData(newData) {
    this.bs.next(newData);
  }
}


2) Then, we use it on an OBSERVER Component, like this :

import { ComponentOnInit } from '@angular/core';
import { tap } from 'rxjs/operators';
import { MyService } from '../services/my.service';

@Component({
  selector: 'my-two',
  templateUrl: './two.component.html',
  styleUrls: ['./two.component.css']
})
export class TwoComponent implements OnInit {
  datastring;
  newDatastring;
  tapDataany;

  constructor(private svcMyService) {}

  ngOnInit() {
    this.svc.obs$
      .pipe(tap(d => (this.tapData = d)))
      .subscribe(d => (this.data = d));
  }

  UpdateData(user) {
    this.svc.editData(this.newData);
  }
}





3) Then, we use on the template HTML is as follows :

<p>
  Data received on Component TWO : {{data}}
</p>


JSON = {{data | json}}




4) The PUBLISHER will be the component ONE:

import { ComponentOnInit } from '@angular/core';
import { MyService } from '../services/my.service';
@Component({
  selector: 'my-one',
  templateUrl: './one.component.html',
  styleUrls: ['./one.component.css']
})
export class OneComponent implements OnInit {
  datastring;
  newDatastring;

  constructor(private svcMyService) {}

  ngOnInit() {
    // receive data back:
    this.svc.obs$.subscribe(d => (this.data = d));
  }
  UpdateData() {
    // send updated data
    this.svc.editData(this.newData);
  }
}


<p> Data sent from component ONE : <br />
  {{data}}
</p>
<input [(ngModel)]="newData"/>
<button (click)="UpdateData();">Update Data</button>

5) And both components will be used on the App.Component :


<p>
  Publish Data from Component ONE to Component TWO
</p>

<my-one></my-one>
<my-two></my-two>

This way, both components are interconnected and pass the data on the fly each another.
( See the code on   stackblitz.com )



That's All!!! 
This post was about BehaviorSubject in Angular 12 : differences between Observable VS Subject VS BehaviorSubject.
Enjoy Angular.....

      by Carmel Schvartzman

כתב: כרמל שוורצמן 

Friday, June 25, 2021

SOLVED : How to fix Angular 10 Ionic ERROR "NullInjectorError: No provider for Contacts!" on Ionic 5

 In this article we review how to  fix Angular 10 Ionic ERROR "NullInjectorError: No provider for Contacts!"  on Ionic 5,    looking like this :




 How to fix Angular 10 Ionic ERROR "NullInjectorError: No provider for Contacts!"  using Ionic 5


After you received this error, you'd probably take a look at the documentation on the official website of Ionic, and on the Github repository for the Contacts component:

https://www.npmjs.com/package/@ionic-native/contacts
https://ionicframework.com/docs/native/contacts#usage

But the problem is also there: the documentation is not up to date.













You can copy-paste all pieces of source code as you move forward through this Tutorial, or elsewhere you can download the entire Angular code from the following URL:

That's All!!! 
Enjoy Angular.....

      by Carmel Schvartzman

כתב: כרמל שוורצמן

Friday, June 11, 2021

SOLVED - how to solve the error " there is no package called ‘bslib’ " on Heroku deployment from GitHub repository

 In this tutorial we'll learn how to solve the  error " there is no package called ‘bslib’ " on Heroku deployment from GitHub repository  

This error occurs while deploying an app to Heroku from a GitHub repository . Your app works perfectly on your development machine, but it would fail on deployment, like this :


Step by step how to  solve the error " there is no package called ‘bslib’ " on Heroku deployment from GitHub repository  


First of all, notice that it seems to be an ENCODING problem : something does not look good with these symbols :  " â€˜bslib’ "

So probably you just have been trying of changing the encoding for your files, also at your machine or uploading again the files to your repository at GitHub.

However, nothing seems to help.

But if you take a closer look, the error states "...NO package called..."  : the PACKAGES are not there.

And inside the word  â€˜bslib’  you can realise the name of the unknown package .

So check whether you are loading this package :



In case you are not loading it, just add it to the setup installations : 



So, redeploy and take a look again: the app is now working:






That's all.... 
In this tutorial we've seen how to solve the  error " there is no package called ‘bslib’ " on Heroku deployment from GitHub repository  .
Happy programming.....
      By Carmel Shvartzman

כתב: כרמל שוורצמן