I have created an app, which works pretty good but I am (I think) doing stuff on places where it's not the best practice to do. I have a component and I use this piece of code for my data method:
data() {
var versionThing = "v1";
if(this.$store.getters.version !=''){
versionThing = this.$store.getters.version;
}
var settings = this.$store.getters.settings;
var checkboxesThing = [
{ val: "val1", text:"Text 1", isChecked: false },
{ val: "val2", text:"Text 2", isChecked: false },
{ val: "val3", text:"Text 3", isChecked: false },
];
if(settings.checkboxes != ''){
checkboxesThing = settings.checkboxes;
}
return {
settings: {
key: settings.key,
version: versionThing,
checkboxes: checkboxesThing,
}
};
},
This way I check whether or not there is a input in my VueX store. But i have a feeling this is probably not the way to do it properly...
This is my VueX code index.js file:
import { createStore } from 'vuex';
import { Storage } from '@ionic/storage';
const ionicStorage = new Storage();
ionicStorage.create();
const store = createStore({
state() {
return {
settings:{
key:"",
version:'',
checkboxes:"",
},
}
},
getters: {
key(state){
return state.settings.key;
},
version(state){
return state.settings.version;
},
settings(state){
return state.settings;
},
// Ionic/Storage getters
async getStorageAandoening(state){
const myAwesomeValue = await ionicStorage.get('key')
if(myAwesomeValue != ''){
state.settings.key = myAwesomeValue;
}
return state.settings.key;
},
async getStorageSettings(state){
const myAwesomeValue = await ionicStorage.get('settings');
var objectMyAwesomeValue = JSON.parse(myAwesomeValue);
if(objectMyAwesomeValue.key != ''){
state.settings = objectMyAwesomeValue;
}
return state.settings;
}
},
mutations: {
changeKey (state, payload) {
ionicStorage.set('key', payload)
state.settings.key = payload
},
changeVersion(state, payload){
ionicStorage.set('version', payload)
state.settings.version = payload
},
changeSettings(state,payload){
ionicStorage.set('settings', JSON.stringify(payload));
state.settings = payload;
}
},
actions: {}
}
);
export default store;
$store.getters.settingsfrom anywhere in your component :)