离子存储不适用于多个服务
Ionic Storage not working with multiple Services
因此,当我在一项服务中使用存储时,一切都完美无缺。现在我用一个新的 Ini-Service 重组了我的代码,我也想在其中使用 Storage 但它不起作用。它没有设置属性。所以我基本上采用了 app.component.ts 文件的逻辑并将其放入 Init-Service,我在 app.component.ts 上的 ngOnInit()- 函数中调用它。
我认为这与在 Ini-Service 中使用 Storage 有关,它调用我的 Webservice,它也使用 Storage ...
也许我需要使用 forwardRef?我尝试使用它,但它似乎没有帮助......(也许我用错了......)
ini.service.ts:
@Injectable({
providedIn: 'root'
})
export class IniService {
constructor(
private storage: Storage,
private webservice: WebserviceService
) { }
...
async someFunction() {
console.log(await this.webservice.doStuff()); // works
this.storage.set("foo", "someValue"); // doesn't work
console.log(await this.storage.get("foo")); // doesn't work
}
}
webservice.service.ts:
@Injectable({
providedIn: 'root'
})
export class WebserviceService {
constructor(
private storage: Storage
) { }
...
async doStuff() {
return await this.storage.get("foo"); // works
}
}
我认为最好的做法是创建一个中央存储服务,然后将该存储服务注入到其他需要与存储引擎交互的服务中。
例如,您可以像这样创建中央存储服务:
import { Injectable } from '@angular/core';
import { Storage } from '@ionic/storage-angular';
@Injectable({
providedIn: 'root'
})
export class StorageService {
private _storage: Storage | null = null;
constructor(private storage: Storage) {
this.initStorage();
}
async initStorage() {
const storage = await this.storage.create();
this._storage = storage;
}
public set(key: string, value: any) {
this._storage?.set(key, value);
}
public get(key: string){
return this._storage.get(key)
}
}
然后,在你的网络服务中
@Injectable({
providedIn: 'root'
})
export class WebserviceService {
constructor(
private storageService: StorageService
) { }
...
async doStuff() {
await this.storageService.set("foo", "somevalue");
return await this.storageService.get("foo");
}
}
因此,当我在一项服务中使用存储时,一切都完美无缺。现在我用一个新的 Ini-Service 重组了我的代码,我也想在其中使用 Storage 但它不起作用。它没有设置属性。所以我基本上采用了 app.component.ts 文件的逻辑并将其放入 Init-Service,我在 app.component.ts 上的 ngOnInit()- 函数中调用它。 我认为这与在 Ini-Service 中使用 Storage 有关,它调用我的 Webservice,它也使用 Storage ... 也许我需要使用 forwardRef?我尝试使用它,但它似乎没有帮助......(也许我用错了......)
ini.service.ts:
@Injectable({
providedIn: 'root'
})
export class IniService {
constructor(
private storage: Storage,
private webservice: WebserviceService
) { }
...
async someFunction() {
console.log(await this.webservice.doStuff()); // works
this.storage.set("foo", "someValue"); // doesn't work
console.log(await this.storage.get("foo")); // doesn't work
}
}
webservice.service.ts:
@Injectable({
providedIn: 'root'
})
export class WebserviceService {
constructor(
private storage: Storage
) { }
...
async doStuff() {
return await this.storage.get("foo"); // works
}
}
我认为最好的做法是创建一个中央存储服务,然后将该存储服务注入到其他需要与存储引擎交互的服务中。
例如,您可以像这样创建中央存储服务:
import { Injectable } from '@angular/core';
import { Storage } from '@ionic/storage-angular';
@Injectable({
providedIn: 'root'
})
export class StorageService {
private _storage: Storage | null = null;
constructor(private storage: Storage) {
this.initStorage();
}
async initStorage() {
const storage = await this.storage.create();
this._storage = storage;
}
public set(key: string, value: any) {
this._storage?.set(key, value);
}
public get(key: string){
return this._storage.get(key)
}
}
然后,在你的网络服务中
@Injectable({
providedIn: 'root'
})
export class WebserviceService {
constructor(
private storageService: StorageService
) { }
...
async doStuff() {
await this.storageService.set("foo", "somevalue");
return await this.storageService.get("foo");
}
}