Blogger Information
Blog 34
fans 0
comment 0
visits 20140
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
vue条件渲染与列表渲染
OC的PHP大牛之路
Original
292 people have browsed it

条件渲染

  1. <div class="app">
  2. <p v-if="flag">{{message}}</p>
  3. <button @click="flag=!flag" v-text="flag ? '隐藏' : '显示'"></
  4. button>
  5. <!-- if-else, if else if else -->
  6. <p v-if="point>=1000 && point< 2000">{{grade[0]}}</p>
  7. <p v-else-if="point>=2000 && point< 3000">{{grade[1]}}</p>
  8. <p v-else-if="point>=3000 && point< 4000">{{grade[2]}}</p>
  9. <p v-if="point>=4000">{{grade[3]}}</p>
  10. <!-- <p v-else>{{grade[4]}}</p> -->
  11. </div>
  12. <script>
  13. Vue.createApp({
  14. data() {
  15. return {
  16. message: '今天是前端最后一课',
  17. flag: true,
  18. // 会员级别
  19. grade: ['纸片会员', '木头会员', '铁皮会员', '金牌会员', '非会员
  20. '],
  21. // 积分
  22. point: 3500,
  23. };
  24. },
  25. }).mount('.app');
  26. </script>

列表渲染

  1. <div class="app">
  2. <!-- array -->
  3. <ul>
  4. <li>{{cities[0]}}</li>
  5. <li>{{cities[1]}}</li>
  6. <li>{{cities[2]}}</li>
  7. </ul>
  8. <hr />
  9. <!-- v-for: 指令循环输出 -->
  10. <ul>
  11. <!-- <li v-for="city of cities">{{city}}</li> -->
  12. <li v-for="(city,index) of cities" :key="index">{{index}} : {
  13. {city}}</li>
  14. </ul>
  15. <hr />
  16. <!-- v-for: 对象数组 -->
  17. <ul>
  18. <li v-for="(user,index) of users" :key="index">{{user.name}}
  19. : ({{user.email}})</li>
  20. </ul>
  21. </div>
  22. <script>
  23. const app = Vue.createApp({
  24. data() {
  25. return {
  26. // array
  27. cities: ['合肥', '苏州', '南京'],
  28. // object: 关联数组
  29. user: {
  30. name: '猪老师',
  31. email: 'laozhu@php.cn',
  32. },
  33. // array of object
  34. users: [
  35. {
  36. name: '猪老师',
  37. email: 'laozhu@php.cn',
  38. },
  39. {
  40. name: '灭绝老师',
  41. email: 'miejue@php.cn',
  42. },
  43. {
  44. name: '欧阳老师',
  45. email: 'ouyang@php.cn',
  46. },
  47. ],
  48. };
  49. },
  50. }).mount('.app');
  51. </script>
Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post