- Vue.js 2 Web Development Projects
- Guillaume Chau
- 107字
- 2021-07-02 22:34:26
Using a method
There is a good coding principle that says Don't Repeat Yourself, and we really should follow it. That's why we can write some logic in reusable functions called methods. Let's move our saving logic into one:
- Add a new methods option to the Vue instance and use the localStorage API there:
new Vue({ // ... methods: { saveNote (val) { console.log('saving note:', val) localStorage.setItem('content', val) }, }, })
- We can now use the method name in the handler option of our watcher:
watch: { content: { handler: 'saveNote', }, },
Alternatively, we can use it with the shorter syntax:
watch: { content: 'saveNote', },
推薦閱讀
- 微服務設計(第2版)
- 從零構建知識圖譜:技術、方法與案例
- Docker技術入門與實戰(第3版)
- Moodle Administration Essentials
- 數據結構(C語言)
- 面向對象程序設計(Java版)
- GameMaker Programming By Example
- Hands-On GPU:Accelerated Computer Vision with OpenCV and CUDA
- 機器學習與R語言實戰
- 深入分布式緩存:從原理到實踐
- Learning Material Design
- Kivy Cookbook
- 微前端設計與實現
- Modular Programming with JavaScript
- 用Python動手學統計學