- 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.
推薦閱讀
- Cocos2d Cross-Platform Game Development Cookbook(Second Edition)
- 在最好的年紀學Python:小學生趣味編程
- iOS 9 Game Development Essentials
- jQuery EasyUI網站開發實戰
- Learning Informatica PowerCenter 10.x(Second Edition)
- 深度強化學習算法與實踐:基于PyTorch的實現
- CKA/CKAD應試教程:從Docker到Kubernetes完全攻略
- Troubleshooting PostgreSQL
- AppInventor實踐教程:Android智能應用開發前傳
- Linux Shell核心編程指南
- 從零開始:UI圖標設計與制作(第3版)
- 零基礎學C語言(升級版)
- Robot Framework Test Automation
- Python 3快速入門與實戰
- 軟件測試技術