无法在 ionic 2 中构建导入服务的实例

Fail to construct instance of imported services in ionic 2

我创建了服务并希望在离子模式中使用它。当我想构建服务实例时,它在第二张图片中显示了突出显示的错误。任何人请切碎我一些光,谢谢!

您忘记在构造函数中将关键字 private 添加到 DBService 中,这会隐式将其添加为 class 成员,因此您无需执行 this.dbservice = dbservice.

constructor(private viewCtrl: ViewController, 
            private nav: NavController, 
            private dbservice: DBService) {
    this.priority = "high";
}

您需要将 DBService 添加到提供程序数组中。在您的 bootstrap 电话中:

bootstrap(App, [/* ..., */ DBService]);

或在您的根组件(应用程序?)上:

@Component({
    providers: [/* ..., */ DBService],
    templateUrl: ...
})
export class App {

在您的 DBServive 中,将 Storage 变量移出构造函数,否则 Angular 将尝试使用 DI:

解析它
@Injectable()
export class DBService {
    private storage: Storage;

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