user2191247
user2191247

Reputation:

How to call custom functions within watch?

I'd like to call a function isChanged from watch:

 watch: {
    items: {
        handler: function(revised, original) {
            for (let i = 0; i < revised.length; i++) {
                if (isChanged(original, revised[i])) {
                    // update
                }
            }
        }
    }
  },
  methods: {
      isChanged(original, item) {
          // code here to check if item is changed
      }
    }
  }

Wherever I put isChanged, it indicates:

[Vue warn]: Error in callback for watcher "items": "ReferenceError: isChanged is not defined"

Where can I put isChanged so it can be seen? What if I wanted to use an imported function in another module?

Upvotes: 0

Views: 58

Answers (1)

Toodoo
Toodoo

Reputation: 8760

You have to use this, as the method is part of your component.

Like this :

watch: {
    items: {
        handler: function(revised, original) {
            for (let i = 0; i < revised.length; i++) {
                if (this.isChanged(original, revised[i])) {
                    // update
                }
            }
        }
    }
  },

Upvotes: 1

Related Questions