I'm developing a small project and i want to be able to use namespaces to avoid getters, mutations, actions, with same names.
As described in docs, the modules must be imported to store, and maps must receive the path to the right module.
I can import everything by omitting the path, but it throws duplicate getter key, if i specify the path it throws module namespace not found in mapActions():
this errors happen in both getters and actions.
This is my store modules:
Stock.js
const state = {
stocks: [
{id: 1, name: 'BMW', price: 110},
{id: 2, name: 'Google', price: 200},
{id: 3, name: 'Apple', price: 250},
{id: 4, name: 'Twitter', price: 8}
]
};
const getters = {
getStocks: state => state.stocks
};
const mutations = {
setStocks: (state, data) => state.stocks = data
};
const actions = {
SETSTOCKS: (store, data) => {
store.commit('setStocks', data)
}
};
export default {
namespace: true,
state,
getters,
mutations,
actions
};
StocksCopy.js
const state = {
stocks: [
{id: 1, name: 'Fiat', price: 110},
{id: 2, name: 'Bing', price: 200},
{id: 3, name: 'Microsoft', price: 250},
{id: 4, name: 'Facebook', price: 8}
]
};
const getters = {
getStocks: state => state.stocks
};
const mutations = {
setStocks: (state, data) => state.stocks = data
};
const actions = {
SETSTOCKS: (store, data) => {
store.commit('setStocks', data)
}
};
export default {
namespace: true,
state,
getters,
mutations,
actions
}
store.js
import Vue from 'vue'
import Vuex from 'vuex'
import stocks from './modules/stocks'
import stocksCopy from './modules/stocksCopy'
Vue.use(Vuex);
const debug = process.env.NODE_ENV !== 'production'
export default new Vuex.Store({
namespace: true,
state: {
},
getters: {
},
mutations: {
},
actions: {
},
modules: {
stocks,
stocksCopy,
},
strict: true,
});
Stocks.vue
<template>
<div class="container">
<div class="row">
<button class="btn btn-primary" @click="setStocks({name: 'test', price: 100})">set stocks</button>
<button class="btn btn-primary" @click="setStocksCopy({name: 'test', price: 100})">set stocksCopy</button>
</div>
<div class="row">
<pre>Stocks: {{stocksList}}</pre>
</div>
<div class="row">
<pre>StocksCopy: {{stocks}}</pre>
</div>
<div class="row">
<app-stocks-stock v-for="(stock) in stocksList" :stock="stock"></app-stocks-stock>
</div>
</div>
</template>
<script>
import { mapGetters, mapActions} from 'vuex'
import Stock from './Stock.vue'
export default {
name: "Stocks",
components: {
'app-stocks-stock': Stock
},
computed: {
...mapGetters({
stocksList: 'getStocks',
stocks: 'stocks'
})
},
data() {
return {
}
},
methods: {
...mapActions('stocksCopy', {
setStocksCopy: 'SETSTOCKS'
}),
...mapActions('stocks', {
setStocks: 'SETSTOCKS'
}),
}
}
</script>
<style scoped>
</style>
Errors:
[vuex] duplicate getter key: getStocks
[vuex] module namespace not found in mapActions(): stocksCopy/
{"stocks":
{"stocks":[{"id":1,"name":"BMW","price":110},{"id":2,"name":"Google","price":200},{"id":3,"name":"Apple","price":250},{"id":4,"name":"Twitter","price":8}]},
"stocksCopy":{"stocks":[{"id":1,"name":"Fiat","price":110},{"id":2,"name":"Bing","price":200},{"id":3,"name":"Microsoft","price":250},{"id":4,"name":"Facebook","price":8}]}}
– Joel Rodrigues