I'm trying to add new variables, getters and mutations in the 'created' lifecycle hook in one of my Vuejs components. The variables work fine. But with getters/mutations, it seems it's not as easy as adding new functions to the vuex store object. The problem I'm trying to solve is creating "shared" reusable components.
Here's the component:
<template>
<div>
<h2 class="headline">Number of items:</h2>
<v-select
:items="limits"
item-value="value"
item-text="text"
label="Select"
v-model="selected_limit"/>
</div>
</template>
<script>
import {selectLimitComponentVariables} from './index';
import {selectLimitComponentGetters} from './getters';
import {selectLimitComponentMutations} from './mutations';
export default {
created: function () {
// Add the variables
Object.assign(this.$store.state[this.ns], selectLimitComponentVariables(this.var_prefix));
// Add the getters
Object.assign(this.$store.getters, selectLimitComponentGetters(this.ns, this.var_prefix));
// Add the mutations
Object.assign(this.$store._mutations, selectLimitComponentMutations(this.ns, this.var_prefix));
},
name: 'SelectLimitComponent',
props: {
ns: {
type: String,
default: null
},
var_prefix: {
type: String,
default: ''
}
},
computed: {
selected_limit: {
get () {
return this.$store.state[this.ns].selected_limit;
},
set (value) {
this.$store.commit(`${this.ns}/update${this.var_prefix}selected_limit`, value)
}
},
limits: {
get() {
return this.$store.state[this.ns].limits;
}
}
}
}
</script>
And here are the imported functions. selectLimitComponentVariables:
export const selectLimitComponentVariables = function (prefix) {
return {
[`${prefix}selected_limit`]: '100',
[`${prefix}limits`]: [
{'text': '10', 'value': '10'},
{'text': '50', 'value': '50'},
{'text': '100', 'value': '100'},
{'text': '250', 'value': '250'},
{'text': 'No limit', 'value': '-1'}]}
};
selectLimitComponentGetters:
export const selectLimitComponentGetters = function(namespace, prefix){
return {
[`${namespace}/${prefix}selected_limit`]: state => state[`${prefix}selected_limit`]
}
};
selectLimitComponentMutations:
export const selectLimitComponentMutations = function (namespace, prefix){
return {
[`${namespace}/update${prefix}selected_limit`]: (state, newLimit) => state[`${prefix}selected_limit`] = newLimit
}
};
Is there a way to manually add or register new getters/mutations, after the vuex store is created?