S
S
Sergei Abramov2020-06-03 16:56:34
Vue.js
Sergei Abramov, 2020-06-03 16:56:34

How to create a function in vuex?

In vuex I added data and a getter to them:

state: {
  tree: null, // Json data
},
getters: {
  getNode: (state, text) => {
    return this.findByText(text);
  }
}


How to create a recursive findByText function in vuex:
getNodeBySlug(slugName, nodes = this.tree) {
      const filterFn = ({slug = ""} = {}) => slug.includes(slugName);

      for (const node of nodes) {
        if (!node.type) {
          continue;
        }
        if (node.type.includes("leaf")) {
          if (filterFn(node)) {
            return node;
          }
        }
        if (node.type.includes("folder")) {
          const result = this.getNodeBySlug(filterFn, node.children);
          if (result) {
            return result;
          }
        }
      }
    },


Throw in methods does not work - it is not visible, and there is no access to this.tree. This is not described in the documentation. Perhaps there should not be such logic in vuex, but so far I haven’t come up with anything better. How to implement the idea?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya, 2020-06-03
@rpsv

Add it anywhere in the project (for good, in a separate file / class), and connect via import.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question