从这个repo,我已经成功配置了这个:
import {Component} from "angular2/core";
import {LocalStorageService} from "angular2-localstorage/LocalStorageEmitter";
@Component({
provider: [LocalStorageService]
})
export class AppRoot{
constructor(private storageService: LocalStorageService){}
...
}
如何使用 storageService 设置或获取本地存储?即使在文档中,我也无法在任何地方找到示例。
更新
经过一些测试,我设法让它通过 WebStorage 与 Decorator 一起工作:
import {LocalStorage, SessionStorage} from "angular2-localstorage/WebStorage";
@Component({})
export class LoginComponent implements OnInit {
@LocalStorage() public username:string = 'hello world';
ngOnInit() {
console.log('username', this.username);
// it prints username hello world
}
}
但是,当我使用 Chrome Dev 查看本地存储时,我什么也看不到:
在另一个组件中,
import {LocalStorage, SessionStorage} from "angular2-localstorage/WebStorage";
@Component({})
export class DashboardComponent implements OnInit {
@LocalStorage() public username:string;
ngOnInit() {
console.log(this.username);
// it prints null
}
}