- Vue.js 2 Web Development Projects
- Guillaume Chau
- 200字
- 2021-07-02 22:34:30
Text stats
The last stats we can display are more "writer-oriented"--the lines, words, and characters count:
- Let's create three new computed properties for each counter, with some Regular Expressions to get the job done:
computed: { linesCount () { if (this.selectedNote) { // Count the number of new line characters return this.selectedNote.content.split(/\r\n|\r|\n/).length } }, wordsCount () { if (this.selectedNote) { var s = this.selectedNote.content // Turn new line cahracters into white-spaces s = s.replace(/\n/g, ' ') // Exclude start and end white-spaces s = s.replace(/(^\s*)|(\s*$)/gi, '') // Turn 2 or more duplicate white-spaces into 1 s = s.replace(/\s\s+/gi, ' ') // Return the number of spaces return s.split(' ').length } }, charactersCount () { if (this.selectedNote) { return this.selectedNote.content.split('').length } }, }
Here, we added some conditions to prevent the code from running if no note is currently selected. This will avoid crashes if you use the Vue devtools to inspect the app in this case, because it will try to compute all the properties.
- You can now add three new stat span elements with the corresponding computed properties:
<span class="lines"> <span class="label">Lines</span> <span class="value">{{ linesCount }}</span> </span> <span class="words"> <span class="label">Words</span> <span class="value">{{ wordsCount }}</span> </span> <span class="characters"> <span class="label">Characters</span> <span class="value">{{ charactersCount }}</span> </span>
The final status bar should look like this:

推薦閱讀
- Java Web基礎(chǔ)與實(shí)例教程(第2版·微課版)
- Python網(wǎng)絡(luò)爬蟲從入門到實(shí)踐(第2版)
- SAS數(shù)據(jù)統(tǒng)計(jì)分析與編程實(shí)踐
- Unity Game Development Scripting
- C語言程序設(shè)計(jì)同步訓(xùn)練與上機(jī)指導(dǎo)(第三版)
- Scala Data Analysis Cookbook
- UNIX Linux程序設(shè)計(jì)教程
- Python Data Science Cookbook
- Java編程從入門到精通
- 從Excel到Python數(shù)據(jù)分析:Pandas、xlwings、openpyxl、Matplotlib的交互與應(yīng)用
- 并行編程方法與優(yōu)化實(shí)踐
- Mastering VMware Horizon 7(Second Edition)
- PowerDesigner 16 從入門到精通
- C# 7.1 and .NET Core 2.0:Modern Cross-Platform Development(Third Edition)
- Visual Basic語言程序設(shè)計(jì)上機(jī)指導(dǎo)與練習(xí)(第3版)