添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

Replace all localStorage directives with this.storage in the angular-electron/src/app/providers/login.service.ts file.

Old Code New code
this.currentUserSubject = new BehaviorSubject<User>(JSON.parse(localStorage.get('user'))); this.currentUserSubject = new BehaviorSubject<User>(JSON.parse(this.storage.get('user')));
localStorage.setItem('user', JSON.stringify(user)); this.storage.save('user', user);
localStorage.removeItem('user'); this.storage.remove('user');
return localStorage.getItem('user'); return this.storage.get('user');

As seen above, the information returned by the server (if the user has been authenticated) will be saved in a .json file (in the electronjs context). We will save the « name of this file » in the configuration file specific to the environment used. Remember that we have chosen the 3 following environments:

import { Injectable } from '@angular/core';
import {ElectronService} from "./electron.service";
import { AppConfig } from '../../environments/environment';
@Injectable({
  providedIn: 'root'
export class StorageService  {
  isElectron: boolean;
  confExists: boolean;
  electron: ElectronService;
  configFile: string;
  constructor(electron: ElectronService) {
    this.electron = electron;
    if (this.electron.isElectron()) {
      this.initElectron();
  save(key: string, content: string) {
    if (this.isElectron) {
      let data =  {};
      data[key] =  content;
      this.electron.fs.writeFileSync(this.configFile, JSON.stringify(data));
      return;
    localStorage.setItem(key, JSON.stringify(content));
  remove(key: string) {
    if (this.isElectron) {
      // Remove file
      this.electron.fs.unlinkSync(this.configFile);
      return;
    localStorage.removeItem(key);
  get(key: string) {
    if (this.isElectron &amp;&amp; this.electron.fs.existsSync(this.configFile)) {
      let jsonContents = this.electron.fs.readFileSync(this.configFile, "utf8");
      jsonContents = JSON.parse(jsonContents); 
      return JSON.stringify(jsonContents[key]);
    return localStorage.getItem(key);
  private initElectron() {
    this.isElectron = true;
    this.configFile = this.electron.remote.app.getPath('userData') + '/' + AppConfig.configFile; 
    this.confExists = this.electron.fs.existsSync(this.configFile);
      let data =  {};
      data[key] =  content;
      this.electron.fs.writeFileSync(this.configFile, JSON.stringify(data));
      return;
    localStorage.setItem(key, JSON.stringify(content));

Line 2-7 checks if we are in the electronjs context. If so, the information returned by the server (including the token) will be recorded in the json C:\Users\~AppData\Roaming\angular-electron\token.json .This is possible thanks to the API of electronjs which makes it possible to save a file on the machine. This API simply uses the fs API of nodejs.

this.electron.fs.writeFileSync(this.configFile, JSON.stringify(data));

Line 9 is used in the web application context.

remove

The remove method is slightly different depending on the context: