0

I'm using Nuxt 2.11 with "bootstrap-vue" 2.5.0

I created a store with a simple list and a mutator to add an element to this list.

// store/issues.js
export const state = () => ({
  list: [],
})

export const mutations = {
  add(state, issue) {
    state.list.push(issue)
  },
}

Then I created a page with a for-loop over all entries and a computed property to load the data from the store.

computed: {
  issues() {
    return this.$store.state.issues.list
  },
},

If I add a simple button to explicit add an item, it will be added everytime and everythings works fine

<b-btn @click="test">Add</b-btn>

test() {
  this.$store.commit('issues/add', {
    title: 'title',
  })
},

But when I use a bootstrap-vue-form to submit new items to the store, the item will be added at first, but after a second, the whole store is removed and the list on my page is empty.

<b-form @submit="onSubmit" @reset="onReset">
  ...
</b-form>


onSubmit() {
  this.$store.commit('issues/add', this.issue)
},
Sebus
  • 3,752
  • 3
  • 14
  • 22

1 Answers1

1

If you do not want the page to reload on submit of the form, you need to prevent the native form submit from occuring:

<b-form @submit="onSubmit" @reset="onReset">
  ...
</b-form>


onSubmit(evt) {
  evt.preventDefault()
  this.$store.commit('issues/add', this.issue)
},
Troy Morehouse
  • 5,320
  • 1
  • 27
  • 38
  • I removed `preventDefault`, because I wanted the modal to close after submit. I forgot that I have to close the modal with `this.$bvModal.hide()`. Now it works! – Sebus Feb 23 '20 at 11:20