Javad Ghavidel
Javad Ghavidel

Reputation: 11

how use cookie in service angular

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

Answers (1)

hamilton.lima
hamilton.lima

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

Related Questions