My app work fine in web, but don't when run in android studio.
the next code in a component is has problem:
setup() {
const app = getCurrentInstance()
const sqlite: SQLiteHook = app?.appContext.config.globalProperties.$sqlite; // always return null
.......
}
cont app return ever null
this is my main.ts is
...
/* SQLite imports */
import { defineCustomElements as jeepSqlite, applyPolyfills } from "jeep-sqlite/loader";
import { Capacitor } from '@capacitor/core';
import { CapacitorSQLite, SQLiteConnection, SQLiteDBConnection } from '@capacitor-community/sqlite';
import { tablasOffile } from './utils/utils-db-no-encryption-offile';
import { useState } from './composables/state';
applyPolyfills().then(() => {
jeepSqlite(window);
});
window.addEventListener('DOMContentLoaded', async () => {
const platform = Capacitor.getPlatform();
const sqlite: SQLiteConnection = new SQLiteConnection(CapacitorSQLite)
const app = createApp(App)
.use(IonicVue)
.use(router)
.use(Store)
.use(VueAxios, axios);
/* SQLite Global Variable */
const [existConn, setExistConn] = useState(false);
app.config.globalProperties.$existingConn = {existConn: existConn, setExistConn: setExistConn};
app.config.globalProperties.$sqlite = sqlite;
try {
if(platform === "web") {
const jeepSqlite = document.createElement('jeep-sqlite');
document.body.appendChild(jeepSqlite);
await customElements.whenDefined('jeep-sqlite');
await sqlite.initWebStore();
}
const ret = await sqlite.checkConnectionsConsistency();
const isConn = (await sqlite.isConnection("DATABASE_NAME")).result;
let db: SQLiteDBConnection
if (ret.result && isConn) {
db = await sqlite.retrieveConnection("DATABASE_NAME");
} else {
db = await sqlite.createConnection("DATABASE_NAME", false, "no-encryption", 1);
}
await db.open();
const res = await db.execute(tablasOffile);
if (res.changes.changes < 0) {
console.log("Error: execute failed");
}
await sqlite.closeConnection("DATABASE_NAME");
router.isReady().then(() => {
app.mount('#app');
});
} catch (err) {
console.log("Error init app: ",err);
}
});
the database has create fine in main.ts
I take part of code from this git
---- RESOLVE ---- I resolve the problem using inject
add next line main.js
app.provide("$sqlite", sqlite);
in de components call
import {defineComponent, inject} from 'vue';
...
setup() {
const sqlite :SQLiteHook = inject('$sqlite');
...
}
...