- 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.
推薦閱讀
- Getting Started with React
- Oracle Database In-Memory(架構與實踐)
- Java 9 Programming Blueprints
- Java:Data Science Made Easy
- Cassandra Data Modeling and Analysis
- iOS編程基礎:Swift、Xcode和Cocoa入門指南
- Hands-On Natural Language Processing with Python
- Java EE 8 Application Development
- 零基礎Java學習筆記
- Mastering Gephi Network Visualization
- PHP項目開發(fā)全程實錄(第4版)
- 現代C++語言核心特性解析
- Scala編程(第4版)
- Eclipse開發(fā)(學習筆記)
- Mastering Magento Theme Design