-1

I am sharing data across two sibling components using an injectible. In my injectible I am using an observable like this:

@Injectable()
export class DatasService { 
    message: Observable<string> = new Observable<string>();
    changeMessage(){
        this.message.next('Arpita'); //Property next doesn't exist
    }

    changeMessage2(){
        this.message.next('Ankan');
    }
}

But then when I am using Subject in place of Observable it works fine. I want to use Observable instead of Subject, so is it possible? I am new to reactive programming, and I am confused.

Ankan Kumar Giri
  • 243
  • 1
  • 3
  • 12
  • *"I want to use Observable instead of Subject"* - why?! – jonrsharpe Sep 18 '17 at 06:30
  • @jonrsharpe great catch i get it why i get the dv now – Rahul Singh Sep 18 '17 at 06:31
  • Because when I am using Subject I cannot set a default value. I know, I can use BehaviorSubject for that, but since am new to reactive, I want to stick to observables for the time being – Ankan Kumar Giri Sep 18 '17 at 06:34
  • I was just wondering if there is any work around for this without using Subject – Ankan Kumar Giri Sep 18 '17 at 06:35
  • 1
    So you want to use an observable because you can't set a default value for a subject, except that you already know that you *can* create a subject with a default value? Your question is very confusing: if you could use an observable as a subject *why would the subject exist*? – jonrsharpe Sep 18 '17 at 06:37
  • Ok, so I get it there's no work around and I have to use Subject or BehaviorSubject to use next(). Thanks I just wanted to know whether it was possible. – Ankan Kumar Giri Sep 18 '17 at 06:48

1 Answers1

-3

use a global variable instead of injectible. so that you can access them through out your project. 1.create a property in DTO. 2.import in that component where you want to use. 3.create a variable of that type in constructor's parameter. 4.access with the use of that variable.