2

I am trying to add the Actions I created in my Vuex module to the Methods of my component.

My component "HomePage.vue" looks like this

import { PlusIcon } from "vue-feather-icons";
import TaskItem from "../TaskItem";
import moment from "moment";
import { mapGetters, mapActions } from "vuex";

export default {
  name: "home-page",
  components: {
    PlusIcon,
    TaskItem,
  },
  methods: {
    ...mapActions(['fetchTasks'])
  },
  computed: mapGetters(['allTasks']),
  created() {
    setInterval(() => {
      document.getElementById("time").innerHTML = moment().format("h:mm:ss a");
    }, 1000);
  },
};

My Vuex module "tasks.js" looks like this

import fs from 'fs'

const state = {
  tasks: []
}
const getters = {
  allTasks: (state) => state.tasks
}

const actions = {
  fetchTasks({commit}) {
    let rawdata = fs.readFileSync('../backend/tasks.json')
    console.log(JSON.parse(rawdata))
    commit('setTasks', JSON.parse(rawdata))
  }
}

const mutations = {
  setTasks: (state, passedTasks) => (state.tasks.push(passedTasks))
}

export default {
  state,
  getters,
  actions,
  mutations,
}

When attempting to use this.fetchTasks() in created(), nothing happens. When console logging this.fetchTasks() it returns as undefined

YaboiJ
  • 73
  • 4

1 Answers1

0

Considering your tasks is a module of your vuex store, you should call your mapActions, mapGetters this way :

methods: {
  ...mapActions('tasks', ['fetchTasks'])
},
computed: {
  ...mapGetters('tasks', ['allTasks'])
},
BTL
  • 4,311
  • 3
  • 24
  • 29