- Vue.js 2 Web Development Projects
- Guillaume Chau
- 151字
- 2021-07-02 22:34:26
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.
推薦閱讀
- 深入核心的敏捷開發:ThoughtWorks五大關鍵實踐
- 演進式架構(原書第2版)
- TensorFlow Lite移動端深度學習
- Learning Chef
- 深入淺出Spring Boot 2.x
- Getting Started with Greenplum for Big Data Analytics
- Microsoft 365 Certified Fundamentals MS-900 Exam Guide
- Learning Docker Networking
- Building Serverless Architectures
- IDA Pro權威指南(第2版)
- OpenCV 3計算機視覺:Python語言實現(原書第2版)
- R語言數據挖掘:實用項目解析
- OpenCV 3.0 Computer Vision with Java
- Microsoft Exchange Server 2016 PowerShell Cookbook(Fourth Edition)
- 3D Printing Designs:Octopus Pencil Holder