问题描述
我正在尝试在Ionic 2中使用Cordova插件sqlite作为服务。
I am trying to use the Cordova plugin sqlite in Ionic 2 as a service.
项目构建但运行空白页。
The project builds but it runs a blank page.
以下是我在android logcat中看到的相关内容:
Here is what I see in the android logcat related to that:
10-29 02:15:11.082 8010-8010/? I/chromium: [INFO:CONSOLE(5)] "EXCEPTION: No provider for t!", source: file:///android_asset/www/build/main.js (5)
10-29 02:15:11.082 8010-8010/? I/chromium: [INFO:CONSOLE(5)] "ORIGINAL STACKTRACE:", source: file:///android_asset/www/build/main.js (5)
10-29 02:15:11.082 8010-8010/? I/chromium: [INFO:CONSOLE(5)] "Error: DI Error
at e.Error (<anonymous>)
at e (file:///android_asset/www/build/main.js:5:5322)
at e (file:///android_asset/www/build/main.js:5:10802)
at new e (file:///android_asset/www/build/main.js:5:11130)
at t._throwOrNull (file:///android_asset/www/build/main.js:5:28341)
at t._getByKeyDefault (file:///android_asset/www/build/main.js:5:28697)
at t._getByKey (file:///android_asset/www/build/main.js:5:28252)
at t.get (file:///android_asset/www/build/main.js:5:24608)
at e.get (file:///android_asset/www/build/main.js:7:16300)
at e.get (file:///android_asset/www/build/main.js:8:3564)", source: file:///android_asset/www/build/main.js (5)
10-29 02:15:11.092 8010-8010/? I/chromium: [INFO:CONSOLE(5)] "EXCEPTION: No provider for t!", source: file:///android_asset/www/build/main.js (5)
10-29 02:15:11.092 8010-8010/? I/chromium: [INFO:CONSOLE(5)] "ORIGINAL STACKTRACE:", source: file:///android_asset/www/build/main.js (5)
10-29 02:15:11.092 8010-8010/? I/chromium: [INFO:CONSOLE(5)] "Error: DI Error
at e.Error (<anonymous>)
at e (file:///android_asset/www/build/main.js:5:5322)
at e (file:///android_asset/www/build/main.js:5:10802)
at new e (file:///android_asset/www/build/main.js:5:11130)
at t._throwOrNull (file:///android_asset/www/build/main.js:5:28341)
at t._getByKeyDefault (file:///android_asset/www/build/main.js:5:28697)
at t._getByKey (file:///android_asset/www/build/main.js:5:28252)
at t.get (file:///android_asset/www/build/main.js:5:24608)
at e.get (file:///android_asset/www/build/main.js:7:16300)
at e.get (file:///android_asset/www/build/main.js:8:3564)", source: file:///android_asset/www/build/main.js (5)
10-29 02:15:11.102 8010-8010/? I/chromium: [INFO:CONSOLE(3)] "Error: No provider for t!", source: file:///android_asset/www/build/polyfills.js (3)
我的离子文件是这样的:
[我的项目] \src\services\sql-settings \ SQL -settings.ts:
My ionic files are like that:"[My project]\src\services\sql-settings\sql-settings.ts":
import { Injectable } from '@angular/core';
import { SQLite } from 'ionic-native';
@Injectable()
export class SqlSettingsService {
constructor(
public db: SQLite
) {
this.db = new SQLite();
}
openDb(){
return this.db.openDatabase({
name: 'data.db',
location: 'default' // the location field is required
});
}
}
[我的项目] \ src\app\app.module.ts:
"[My project]\src\app\app.module.ts":
import { NgModule } from '@angular/core'; import { IonicApp, IonicModule } from 'ionic-angular'; import { MyApp } from './app.component'; import { HomePage } from '../pages/home/home'; import { SqlSettingsService } from '../services/sql-settings/sql-settings';
@NgModule({ declarations: [
MyApp,
HomePage ], imports: [
IonicModule.forRoot(MyApp) ], bootstrap: [IonicApp], entryComponents: [
MyApp,
HomePage ], providers: [SqlSettingsService] }) export class AppModule {}
最后[我的项目] \ src\app\app.component.ts:
And finaly "[My project]\src\app\app.component.ts":
import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { StatusBar, Splashscreen } from 'ionic-native';
import { HomePage } from '../pages/home/home';
import { SqlSettingsService } from '../services/sql-settings/sql-settings';
@Component({
template: `<ion-nav [root]="rootPage"></ion-nav>`,
providers: [SqlSettingsService]
})
export class MyApp {
rootPage = HomePage;
constructor(platform: Platform, sqlSettingsService: SqlSettingsService) {
platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
StatusBar.styleDefault();
Splashscreen.hide();
sqlSettingsService.openDb().then(
()=>{alert("OPEN DB IS SUCCESSFUL")},
(err)=>{alert('Unable to open database: ' + err)}
);
});
}
}
推荐答案
为什么你这样做了吗?
constructor(public db: SQLite) {
this.db = new SQLite();
}
这是毫无意义的。 1.没有提供者,因此无法注入。即使它是注射的,你也要给它一些新东西。无意义。您可能想要做的是
This is pointless. 1. There is no provider, so it can't be injected. 2. Even if it were injected, you're assigning it something new. Pointless. What you probably want to do is
db: SQLite;
constructor() {
this.bd = new SQLite();
}
这篇关于Ionic2 cordova-sqlite-storage:如何使用SQLite作为服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!