如何订阅 angular-2-local-storage 中的 setItems$ Observable

How do I subscribe to the setItems$ Observable in angular-2-local-storage

我正在使用 angular-2-local-storage,它有一个 setItems$ 的可观察对象,我正在尝试...观察,在指令中使用以下代码:

/** app-theme.directive.ts **/

import { Directive, ElementRef, Input } from '@angular/core';
import { UserSettings } from './core/user-settings';
import { LocalStorageService } from 'angular-2-local-storage';

@Directive({
  selector: '[appTheme]'
})
export class AppThemeDirective {
  constructor(el: ElementRef, localStorageService: LocalStorageService) {
    localStorageService.setItems$.subscribe(function(x: any) {
      console.log('subscribed', x);
    });
  }

}

本地存储在我的代码中更新和保存得很好,但更新没有被观察到,也没有记录到控制台。我错过了任何明显的问题吗?

抱歉,一切顺利,但我需要阅读以下内容: https://github.com/phenomnomnominal/angular-2-local-storage/issues/31 并将 notifyOptions 添加到 app.module.ts imports

LocalStorageModule.withConfig({
    prefix: 'ng-starter',
    storageType: 'localStorage',
    notifyOptions : {setItem: true, removeItem:true}
}),

localStoragesessionStorage 已经存在于 lib 中,因此您可以使用 localStorage.setItem() 和 localStorage.getItem() 直接使用它们。

mycomponent.ts

import { Directive, ElementRef, Input } from '@angular/core';
import { UserSettings } from './core/user-settings';
import { LocalStorageService } from 'angular-2-local-storage';

@Directive({
  selector: '[appTheme]'
})
export class AppThemeDirective {
  constructor(el: ElementRef, localStorageService: LocalStorageService) {

    let data = { application: this.application };
    localStorage.setItem('applicationdata', JSON.stringify(data));
    var data = localStorage.getItem('applicationdata');
  }

}

希望对您有所帮助。