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

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.

主站蜘蛛池模板: 中牟县| 永川市| 逊克县| 偏关县| 扎囊县| 青河县| 石狮市| 高淳县| 玛沁县| 新干县| 舟曲县| 诸暨市| 陆良县| 高平市| 高州市| 望谟县| 日土县| 陈巴尔虎旗| 内黄县| 普定县| 尼木县| 张家口市| 文昌市| 南木林县| 策勒县| 连山| 平定县| 尚义县| 哈巴河县| 景泰县| 张北县| 逊克县| 武清区| 浠水县| 景洪市| 高密市| 福鼎市| 北海市| 衡水市| 黄陵县| 成都市|