Vue-class-component: Watching a property and call a method

Created on 11 Jun 2017  路  3Comments  路  Source: vuejs/vue-class-component

How can watch a property and invoke a function? I tried something like below but the reference to this is not the class object.

@Component({
    components: {
      Multiselect
    },
    watch: {
      host: () => {
        // this.analytics()
      },
      interval: () => {
        // this.analytics()
      }
    }
  })

Most helpful comment

Changing arrow function to plain function is enough.

@Component({
    components: {
      Multiselect
    },
    watch: {
      host: function () {
        this.analytics()
      },
      interval: function () {
        this.analytics()
      }
    }
  })

http://exploringjs.com/es6/ch_arrow-functions.html

All 3 comments

Changing arrow function to plain function is enough.

@Component({
    components: {
      Multiselect
    },
    watch: {
      host: function () {
        this.analytics()
      },
      interval: function () {
        this.analytics()
      }
    }
  })

http://exploringjs.com/es6/ch_arrow-functions.html

Thanks for your prompt response.

Changing arrow function to plain function is enough.

@Component({
    components: {
      Multiselect
    },
    watch: {
      host: function () {
        this.analytics()
      },
      interval: function () {
        this.analytics()
      }
    }
  })

http://exploringjs.com/es6/ch_arrow-functions.html

This works for me. Thank you bro

Was this page helpful?
0 / 5 - 0 ratings

Related issues

liudonghua123 picture liudonghua123  路  5Comments

nqthiep picture nqthiep  路  6Comments

hesi726 picture hesi726  路  5Comments

dmitrykurmanov picture dmitrykurmanov  路  4Comments

tonypee picture tonypee  路  5Comments