V
V
VueProgrammer2021-01-15 22:30:43
Vue.js
VueProgrammer, 2021-01-15 22:30:43

How to call a Vue.JS function?

Hello everyone,
I am a beginner programmer in this field, I have a question,
is there a javascript file that contains the following function:

export const actions = {
  async loadFunction({ commit }) {
    try {
      const plans = await Parse.Cloud.run('getFunction', {}, {});
      commit('setFunction', plans);
    } catch (error) {
      commit('setFunction', null);
      return null;
    }
  },
};


How to call this function and get the result in the index.vue file in this section of the loop:
<TItem
          v-for="
     (index, item) in ?????
          :key="index"
          :tplan="item"
          @delete="deleteItem(index)"
        />


I did the import of the function in the index.vue file,
thanks in advance!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dima Pautov, 2021-01-15
@bootd

// store
export const state = () => ({
  functions: []
});

export const actions = {
  async loadFunction({ commit }) {
    try {
      const plans = await Parse.Cloud.run('getFunction', {}, {});
      commit('setFunction', plans);
    } catch (error) {
      commit('setFunction', null);
      return null;
    }
  },
};

export const mutations = {
  setFunction: (state, payload) => {
    state.functions = payload;
  }
}

// Ваш копонент, где вы отображаете TItem
<template>
  <div>
    <TItem
          v-for="(item, index) in functions"
          :key="index"
          :tplan="item"
          @delete="deleteItem(index)"
        />
  </div>
</template>

<script>
import { mapState } from 'vuex'

export default {
  //....
  computed: {
    ...mapState([
      'functions'
    ])
  }
  //...
}
</script>

D
Dmitry, 2021-01-15
@DKWLB

Apparently it was not you who wrote this and it was taken out of context, or rather the vuex store.
In addition to actions, there should be state, mutations.
This function will not work in this form.
If it receives data, then a setFunction mutation is created that changes the state. Next, it is desirable to create a getter (conditional getData) that will return data to you.
And in the component, you create a calculated property with a suitable name and use the getter to get the data.
And this property is specified in the loop.
Everything is just
P.S. while writing - Dmitry just depict everything)

A
Alex, 2021-01-15
@Kozack Vue.js

https://vuex.vuejs.org/ru/guide/state.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question