- Vue.js 2 Web Development Projects
- Guillaume Chau
- 208字
- 2021-07-02 22:34:28
The current note
Now that we know which note is currently selected, we can replace the old content data property we created at the beginning. It would be very useful to have a computed property to easily access the selected note, so we will create one now:
- Add a new computed property called selectedNote that returns the note with an ID that matches our selectedId property:
computed: { ... selectedNote () { // We return the matching note with selectedId return this.notes.find(note => note.id === this.selectedId) }, }
note => note.id === this.selectedId is an arrow function from the ES2015 JavaScript version. Here, it takes a note argument and returns the result of the note.id === this.selectedId expression.
We need to replace the old content data property with selectedNote.content in our code.
- Start by modifying the editor in the template:
<textarea v-model="selectedNote.content"></textarea>
- Then, change the notePreview computed property to now use selectedNote:
notePreview () { // Markdown rendered to HTML return this.selectedNote ? marked(this.selectedNote.content) :
'' },
Now, the text editor and the preview pane will display the selected note when you click on it in the list.
You can safely remove the content data property, its watcher, and the saveNote method, which are no longer used in the app.
推薦閱讀
- Java高并發核心編程(卷2):多線程、鎖、JMM、JUC、高并發設計模式
- 深入淺出Prometheus:原理、應用、源碼與拓展詳解
- Web開發的貴族:ASP.NET 3.5+SQL Server 2008
- Groovy for Domain:specific Languages(Second Edition)
- 前端架構:從入門到微前端
- Web Application Development with MEAN
- Learning Python by Building Games
- QGIS By Example
- Working with Odoo
- Go語言精進之路:從新手到高手的編程思想、方法和技巧(1)
- C++ Fundamentals
- Qt 4開發實踐
- The Statistics and Calculus with Python Workshop
- 關系數據庫與SQL Server 2012(第3版)
- Mastering Machine Learning with R