没有存储提供者! ionic2 中的(DBService -> 存储)

No provider for Storage! (DBService -> Storage) in ionic2

这是我的app.ts

这是我的service.ts

我没有存储提供程序! (DBService -> 存储)错误。我相信我的 service.ts 有问题, 1)是我的构造函数没有参数吗? 2) 我不能在 getTasks() 函数中使用 this.storage,甚至不能在构造函数中使用 this.storage,这是为什么?

请帮助我,因为我已经卡住了 1 天了。

Working Plunker

使用this

您在 service.ts 中的函数似乎无法访问您在构造函数中设置的 storage 变量。

而不是 let storage 在你的构造函数中尝试 this.storage 这样的:

constructor() {
    this.storage = new Storage(SqlStorage);
}

然后你可以像这样

从同一个class中的任何函数访问它
setColor(newColor) {
  this.storage.set('color', newColor);
}

存储方法

此外,来自 Ionic docs I don't see a query method, but there is a get(key) and set(key, value) 应该有用的选项。

添加提供商

使用 Ionic 2/Angular 2 服务需要在组件的 providers 部分设置。这可以在全局 app.component 上,如果您希望该服务可用于整个应用程序,或者您可以只使其可用于单个组件。

在 Plunker 示例中,我在 home 组件中设置了它,它说 providers: [DBService] 是这样的:

// home.ts
import { Page } from 'ionic-angular/index';
import { DBService } from './service.ts';

@Page({
  templateUrl:"home.html",
  providers: [DBService]
})