· 6 years ago · Aug 15, 2019, 08:02 PM
1localStorage.getItem("oauth_token")...
2
3import { Injectable } from '@angular/core';
4import { HttpClient, HttpHeaders } from '@angular/common/http';
5import { BehaviorSubject, Observable } from 'rxjs';
6
7@Injectable({
8 providedIn: 'root'
9})
10export class CommonService {
11 data: any;
12 private items: BehaviorSubject<any> = new BehaviorSubject<any>(this.data);
13 items$: Observable<any> = this.items.asObservable();
14 constructor(private http: HttpClient) { }
15 setLocalStorageData() {// Call this function to set localstorage
16 localStorage.setItem("oauth_token", oauth_token)
17 this.data = oauth_token;
18 }
19}
20
21constructor(private commonService: CommonService) { }
22 items: any;
23 subscription: Subscription; //import { Subscription } from 'rxjs';
24
25 ngOnInit() {
26 this.subscription = this.commonService.items$.subscribe(items => {
27 this.items = JSON.stringify(items));
28 console.log(localStorage.getItem("oauth_token"));
29 }
30 }
31
32 ngOnDestroy() {
33 this.subscription && this.subscription.unsubscribe();
34 }