如何在服务角度使用cookie

时间:2018-08-29 13:15:23

标签: angular

我是angular的新手,并且想要使用我设置在新的BehaviorSubject中的cookie。 请帮助我,谢谢!

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');
    }
}

1 个答案:

答案 0 :(得分:1)

将初始化移至构造函数

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);
  }
}