Reputation: 11
I am new to angular and want to use the cookie I set into new BehaviorSubject. Please help me, thanks!
data.service.ts
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
import { CookieService } from 'ngx-cookie-service';
@Injectable()
export class DataService {
a: any;
private messageSource = new BehaviorSubject(this.a);
currentMessage = this.messageSource.asObservable();
constructor(private cookie: CookieService) {
this.a = this.cookie.get('email');
}
}
Upvotes: 0
Views: 433
Reputation: 1918
Moving the initialization to the constructor
export class DataService {
private messageSource;
public getCurrentMessage(){
return this.messageSource.asObservable();
}
constructor(private cookie: CookieService) {
let email = this.cookie.get('email');
this.messageSource = new BehaviorSubject(email);
}
}
Upvotes: 1