- 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', },
推薦閱讀
- 編程的修煉
- 測試驅動開發:入門、實戰與進階
- C語言程序設計習題解析與上機指導(第4版)
- 造個小程序:與微信一起干件正經事兒
- PHP 從入門到項目實踐(超值版)
- Ext JS Data-driven Application Design
- Effective Python Penetration Testing
- Asynchronous Android Programming(Second Edition)
- 深入理解Elasticsearch(原書第3版)
- 基于SpringBoot實現:Java分布式中間件開發入門與實戰
- C# and .NET Core Test Driven Development
- Robot Framework Test Automation
- Mastering Bootstrap 4
- Android應用開發攻略
- MySQL數據庫教程(視頻指導版)