Where should vuex write a callable public method?

premise:
1, the state data of state needs to be used inside the method
2, the method needs to be called multiple times or called in a loop
3, this method must not be written in action, it does not modify the state in state, it just uses state data and parameters to do some comparative calculations, and returns new results

what I"m doing now is to define this public function externally, and then pass state as an argument every time

export function (state {
    //
}

although this can achieve my intention, but I always feel inappropriate? I would like to ask if there is a better way.

Mar.28,2021

feels that your requirement is more appropriate to be written in Getter . Take a look at the document ide/getters.html-sharp%E9%80%9A%E8%BF%87%E6%96%B9%E6%B3%95%E8%AE%BF%E9%97%AE" rel=" nofollow noreferrer "> Getter visit


Mutations Big Brother ~
ide/mutations.html" rel= "nofollow noreferrer" > vuex document


Public function files can be directly

import store from '@/store'

export function func1() {
  const state1 = store.state.module_name.state1
  return state1
}

clipboard.png


you can register a global method in main.js

Vue.prototype.someMethod = function(state){
    // do something...
}

then call

within the component
this.someMethod(state)

generally speaking, frequently used methods are registered in this way, such as functions that deal with the results returned by the background, and self-written formatTime methods

Menu