- 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', },
推薦閱讀
- Hyper-V 2016 Best Practices
- 區塊鏈:以太坊DApp開發實戰
- Redis Essentials
- C# 8.0核心技術指南(原書第8版)
- Java網絡編程實戰
- Mastering Backbone.js
- 區塊鏈技術進階與實戰(第2版)
- Troubleshooting Citrix XenApp?
- 深入實踐DDD:以DSL驅動復雜軟件開發
- 貫通Tomcat開發
- Learning Jakarta Struts 1.2: a concise and practical tutorial
- OpenCV 3.0 Computer Vision with Java
- Python應用與實戰
- 一步一步學Spring Boot:微服務項目實戰(第2版)
- Effective C++:改善程序與設計的55個具體做法(第三版)中文版(雙色)