In my project, I am currently focused on saving user form responses. I have set up a pinia store and defined each form as an object within it. By integrating this store into my component and assigning the getter function of that store to a variable,
I am using this variable as a v-model. However, even after providing input, nothing is being returned. Strangely, there are no error messages either. Could it be possible that I have taken the wrong approach?
Userstore.js
import { defineStore } from 'pinia';
export const userResponses = {
formsResponses: {
form1: {
input1: '',
},
},
};
export default defineStore('userStore', {
state() { return userResponses; },
getters: {
getFormsInput: (state) => state.formsResponses.form1,
},
actions: {
setFormsResponses(formResponses) {
this.formsResponses.form1 = formResponses;
},
},
});
Form1.vue
<template>
<input type="text" name="input_form" v-model="input"/>
<button type="Primary" @click="submit">submit</button>
</template>
<script setup>
import { computed, ref } from 'vue';
import useUserStore from '@/store/userStore';
const userStore = useUserStore();
const input = ref('');
const responses = computed(() => userStore.getFormsInput);
reponses.value.input1 = input.value;
function submit() {
console.log(reponses.value.input1); // it is giving nothing
}
</script>
Why am I struggling to utilize getters or update the value? Would it work if I directly used getters in the v-model?