https://stackblitz.com/edit/vue-script-setup-with-vuex-gfjaff?file=src/components/UserProfile.vue
I'm trying to be able to enter a name into the dispatch function and add it to an array. (Eventually I'd like to add a text input field, and enter it from there. Baby steps)
Then I'd like to be able to click a delete button near those names to delete it. I'm having a problem trying to implement "event.target.value". I think that's the problem anyways.
What am I doing wrong in this code? What part am I getting wrong?
store.js
import { createStore } from 'vuex'
export default createStore({
state: () => ({
list: ['bob', 'joe', 'harry'],
}),
getters: {
listStatus: (state) => state.list,
},
actions: {
addName({ commit }, { name }) {
commit('ADD_NAME', name)
},
removeName({commit}, {name}) {
commit('REMOVE-NAME', name)
}
},
mutations: {
ADD_NAME(state, name) {
state.list.push(name)
},
REMOVE_NAME(state, event_target_name){
state.list.filter(element=>element !== event_target_name)
}
},
})
userList.vue
<template>
<div v-for="name in listStatus">
<li>{{ name }}</li>
<button @click="removeName(event.target.value)">Remove</button>
</div>
<button @click="addName">ADD NAME</button>
</template>
<script setup>
import { computed } from 'vue'
import { useStore } from 'vuex'
const store = useStore()
const listStatus = computed(() => store.getters.listStatus)
// const addName = (input_name) => store.dispatch('addName', { name: input_name }) --------- DOESN'T WORK
const addName = () => store.dispatch('addName', { name: 'tommy' })
const removeName = (event) => store.dispatch('remove-name', { name: event.target.value })
</script>