我有這個Vuex 2店:無法添加到Vuex商店正確
const datastore = new Vuex.Store({
state: {
socketcluster: {
connection: false,
channels: []
},
selected_offers: []
},
mutations: {
addOffer: function(offer) {
datastore.state.selected_offers.push(offer) // first problem: cannot just use state.offers as it throws an undefined
}
},
getters: {
getOffers: function(){
return datastore.state.selected_offers;
}
}
});
而一個Vue公司2組件內我做的:
methods: {
clicked: function(){
console.log("Toggle Offer");
if (datastore.getters.getOffers.filter(function(o){ o.id == this.offer.id }).length == 0) {
// add it
datastore.commit('addOffer', this.offer)
} else {
// TODO remove it
}
}
}
會發生什麼事是,當我觸發法,店內變化如下:
我在做什麼錯?
真棒。不知道我怎麼錯過了。猜猜我從來沒有看過實際的參數。 –