官术网_书友最值得收藏!

Accessing the Vue instance

Inside the methods, we can access the Vue instance with the this keyword. For example, we could call another method:

methods: {
  saveNote (val) {
    console.log('saving note:', val)
    localStorage.setItem('content', val)
    this.reportOperation('saving')
  },
  reportOperation (opName) {
    console.log('The', opName, 'operation was completed!')
  },
},

Here, the saveNote method will be called from the contentChanged method.

We can also access the other properties and special functions of our Vue instance through this. We could remove the saveNote argument and access the content data property directly:

methods: {
  saveNote () {
    console.log('saving note:', this.content)
    localStorage.setItem('content', this.content)
  },
},

This also works in the watcher handler we created in the Watching changes section:

watch: {
  content (val, oldVal) {
    console.log('new note:', val, 'old note:', oldVal)
    console.log('saving note:', this.content)
    localStorage.setItem('content', this.content)
  },
},

Basically, you can access the Vue instance with this in any function bound to it: methods, handlers, and other hooks.

主站蜘蛛池模板: 通道| 阿合奇县| 天峨县| 佳木斯市| 淅川县| 竹溪县| 水城县| 唐河县| 彰化市| 安宁市| 临高县| 柏乡县| 澳门| 岱山县| 宝兴县| 梧州市| 大化| 桐乡市| 平罗县| 新化县| 延庆县| 田东县| 陵川县| 华容县| 沛县| 南投县| 枞阳县| 镇巴县| 天峨县| 鄂托克旗| 红河县| 夏河县| 山西省| 桐城市| 台山市| 攀枝花市| 句容市| 桓台县| 江门市| 贺州市| 德兴市|