Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

209
Views
Los módulos Vuex 4 no funcionan al refactorizar módulos para separar archivos

He estado aprendiendo vuex4 y allí escribí todos los estados, acciones, captadores... en main.js

Ahora llegué a los módulos y traté de refactorizar mi código a módulos en archivos separados. Todo funciona excepto por un bucle v-for donde los números deberían aparecer pero no lo hacen. El número debería aparecer cuando el número cambia en la aplicación en "historial".

Estoy actualizando la matriz de historial con las mutaciones "addToCounter" y "minToCounter" que se encuentran en counter.js

History.vue (aquí es donde el bucle v-for no funciona)

 <div class="container"> <h4>History</h4> <div class="flex"> <p v-for="(number,index) in history" :key="index" :class="activeIndexes(parseInt(value)).includes(index) && 'bold'" > {{number}} </p> </div> <input type="text" placeholder="search by index" v-model="value" > </div> </template> <script> import {mapState, mapGetters} from "vuex"; export default { data(){ return{ value: 0, } }, computed: { ...mapState(['history']), ...mapGetters(['activeIndexes']), } } </script> <style>...</style>

counter.js (el módulo se guarda en módulos/counter.js)

 import axios from "axios"; const state = { counter: 0, history: [0], }; const mutations = { addToCounter(state, payload){ state.counter = state.counter + payload; state.history.push(state.counter) }, minToCounter(state, payload){ state.counter = state.counter - payload; state.history.push(state.counter) } }; const actions = { //async je takrt k zahtevamo nek http request npr od api-ja spodnji primer: async addRandomNumber(context) { let data = await axios.get('https://www.random.org/integers/?num=1&min=-1000&max=1000&col=1&base=10&format=plain&rnd=new'); context.commit("addToCounter",data.data); } }; const getters = { activeIndexes: (state) => (payload) => { let indexes = []; state.history.forEach((number, index) => { if(number === payload) { indexes.push(index) } }); return indexes } }; export default { state,mutations,actions,getters }

principal.js

 import { createApp } from 'vue' import { createStore } from 'vuex'; import counter from "./modules/counter"; import App from './App.vue' const store = createStore({ modules: { counter: counter, } }) createApp(App).use(store).mount('#app')
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Lo averigué :).

en Historia.vue

Cambié:

 computed: { …mapState(['history']), → …mapState(['counter']) //as module name is counter …mapGetters(['activeIndexes']), }

y luego para v-para

 v-for="(number,index) in history" → v-for="(number,index) in counter.history"
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!