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

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.

主站蜘蛛池模板: 师宗县| 原平市| 镇安县| 阿拉尔市| 南华县| 宜黄县| 西青区| 固始县| 博湖县| 扎赉特旗| 沾化县| 慈利县| 柞水县| 葵青区| 响水县| 重庆市| 格尔木市| 安丘市| 梧州市| 垫江县| 潼关县| 梁河县| 伊吾县| 漾濞| 新乐市| 南汇区| 长白| 盐源县| 宁南县| 万安县| 安徽省| 北票市| 尖扎县| 正安县| 金坛市| 扶风县| 尉氏县| 塘沽区| 夏邑县| 定兴县| 新郑市|