vue.js - Pass data to a store? -


how can pass data vue component store?

here's component:

methods: {     ...mapactions('navbar', [         'fix',     ]),     onclick: function() {         this.fix('my-data');     },     .... 

and on store:

actions: {               fix: ({ commit }) => {     //get data here?    }, }, 

actions mutations in vuejs 2.x can take 1 additional argument (often referred payload) additional data.

from vuejs documentation on mutations:

you can pass additional argument store.commit, called payload mutation:

mutations: {   increment (state, n) {     state.count += n   } } store.commit('increment', 10) 

in cases, payload should object can contain multiple fields, , recorded mutation more descriptive:

mutations: {   increment (state, payload) {     state.count += payload.amount   } } store.commit('increment', {   amount: 10 }) 

and actions:

actions support same payload format , object-style dispatch:

// dispatch payload store.dispatch('incrementasync', {   amount: 10 })  // dispatch object store.dispatch({   type: 'incrementasync',   amount: 10 }) 

the documentation seems not clear on how define action, looks following should work:

actions: {   incrementasync ({ commit, state }, payload) { ... } } 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -