1

The events By Filters method is used as a calculated property for the v-for loop in the component:

getters: {
        eventsByFilters(state) {
            var events = state.events
            if (state.search === '' && state.selected) {
                switch (state.selected) {
                    case 'month':
                        return events.filter(item => this.isMonthEqualNow(item))
                    case 'week':
                        return events.filter(item => this.isMonthEqualNow(item) && this.isWeekEqualNow(item))
                    case 'day':
                        return events.filter(item => this.isMonthEqualNow(item) && this.isWeekEqualNow(item)
                            && this.isDayEqualNow(item))
                    default:
                        return events
                }
            } else {
                return events.filter(item => item.title.indexOf(state.search) !== -1)
            }
        },

I need to use the isMonthEqual and other methods to find out if they belong to certain dates.But I find it difficult to access them:

if I define them in actions, I can't access them, since context is not present in getter;

if I define globally, too, there is no way to access them(this is undefined in this case, and without this, the error that the method is undefined is thrown);

2
  • Are you using vuex? Commented Jul 15, 2020 at 21:58
  • @AndresForonda, yeah Commented Jul 16, 2020 at 8:59

1 Answer 1

3

I'll assume you created your project using the Vue CLI, and are thus using webpack. You should be able to define these methods in a separate file, export them, and then import them into your store files.

/Helpers/GlobalFunctions.js

export function isMonthEqual(param1, param2) {
  // blah blah blah
}

store.js

// Top of the file
import { isMonthEqual } from '@/Helpers/GlobalFunctions.js'

// Store
const store = new Vuex.Store({
  getters: {
    myGetter: state => {
      // Use the function somewhere
      const isEqual = isMonthEqual(month1, month2)
      return isEqual
    }
  }
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.