I’m trying to use Ionic Storage v3 in my Vuejs Ionic project but I am having issues rendering components after retrieving values from storage. I am pretty new to both VueJs and Ionic and definitely struggle with async functions and setup. With the setup below I am able to add items to storage and retrieve them but the array does not render in the dom. Any help would be appreciated.
<template>
<ion-page>
<ion-header>
<ion-toolbar>
<ion-title>Test</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<div class="grid">
<div class="grid-tile" v-for="project in projects" :key="project.id">
<project-item :id="project.id" :title="project.title"></project-item>
</div>
</div>
<ion-fab vertical="bottom" horizontal="end" slot="fixed">
<ion-fab-button @click="projectModal(null,null)">
<ion-icon :icon="addCircle"></ion-icon>
</ion-fab-button>
</ion-fab>
</ion-content>
</ion-page>
</template>
<script>
import {defineComponent} from 'vue';
import {IonPage, IonHeader, IonToolbar, IonTitle, IonContent, IonIcon, modalController, IonFabButton, IonFab} from '@ionic/vue';
import {addCircle} from 'ionicons/icons';
import ProjectItem from "@/components/ProjectItem";
import HawkProjectModal from "@/components/HawkProjectModal";
import {Drivers, Storage} from '@ionic/storage';
import * as CordovaSQLiteDriver from 'localforage-cordovasqlitedriver';
export default defineComponent({
name: 'Project',
components: {
ProjectItem,
IonPage,
IonHeader,
IonToolbar,
IonTitle,
IonContent,
IonIcon,
IonFabButton,
IonFab
},
setup() {
const projectDB = new Storage({
name: 'projects',
storeName: 'projects',
driverOrder: [CordovaSQLiteDriver._driver, Drivers.IndexedDB, Drivers.LocalStorage]
});
return {
projectDB,
projects: [],
addCircle,
}
},
mounted() {
this.initializeDatabase(this.projectDB);
this.project = this.getDatabaseValues(this.projectDB)
},
methods: {
async defineDriver(database) {
await database.defineDriver(CordovaSQLiteDriver);
},
async createDatabase(database) {
await database.create();
},
async initializeDatabase(database) {
this.createDatabase(database)
.then(this.defineDriver(database))
.then(console.log(String(database._config.name) + ' database initialized'));
},
getDatabaseValues(database) {
let values = [];
database.forEach((value) => {
values.push(value);
});
return values;
},
async setDatabaseEntry(database, key, value) {
await database.set(key, value);
return this.getDatabaseValues(database);
},
async projectModal(id, title) {
const modal = await modalController
.create({
component: HawkProjectModal,
cssClass: 'project-modal',
componentProps: {
id: id,
title: title
}
});
modal.onDidDismiss()
.then((data) => {
if (data.role === 'create' || data.role === 'edit') {
const project = Object.assign({}, data.data);
this.setDatabaseEntry(this.projectDB, project.id, {id: project.id, title: project.title}).then((newArray) => {
this.projects = newArray;
});
}
});
return modal.present();
}
}
});
</script>