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

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.

主站蜘蛛池模板: 耒阳市| 云霄县| 深州市| 韶关市| 吴桥县| 会泽县| 吉隆县| 黄龙县| 巢湖市| 廊坊市| 湛江市| 虹口区| 垣曲县| 九江市| 玉门市| 石门县| 岳普湖县| 万盛区| 铁岭市| 淄博市| 韩城市| 临朐县| 文山县| 溧阳市| 招远市| 大方县| 甘泉县| 苗栗县| 长兴县| 白朗县| 清镇市| 焉耆| 尉犁县| 休宁县| 霍林郭勒市| 常宁市| 博爱县| 浦东新区| 通榆县| 陵川县| 嘉荫县|