博客列表 >实例演示条件渲染与列表渲染

实例演示条件渲染与列表渲染

P粉191340380
P粉191340380原创
2022年09月18日 08:06:56350浏览

列表渲染

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>列表渲染:v-for</title>
  8. <script src="https://unpkg.com/vue@next"></script>
  9. </head>
  10. <body>
  11. <div class="app">
  12. <!-- array -->
  13. <ul>
  14. <li>{{cities[0]}}</li>
  15. <li>{{cities[1]}}</li>
  16. <li>{{cities[2]}}</li>
  17. </ul>
  18. <hr>
  19. <!-- v-for: 指令循环输出 -->
  20. <ul>
  21. <!-- <li v-for="city of cities">{{city}}</li> -->
  22. <li v-for="(city,index) of cities" :key="index">{{index}} : {{city}}</li>
  23. </ul>
  24. <hr>
  25. <!-- v-for:对象数组 -->
  26. <ul>
  27. <!-- <li v-for="city of cities">{{city}}</li> -->
  28. <li v-for="(user,index) of users" :key="index">{{user.name}} : {{user.email}}</li>
  29. </ul>
  30. <hr>
  31. </div>
  32. <script>
  33. const app = Vue.createApp({
  34. data() {
  35. return {
  36. // array
  37. cities: ['深圳', '广州', '中山'],
  38. // object: 关联数组
  39. user: {
  40. name: '老朱',
  41. email: 'laozhu@php.cn',
  42. },
  43. // array of object
  44. users: [
  45. {
  46. name: '老朱',
  47. email: 'laozhu@php.cn',
  48. },
  49. {
  50. name: '老李',
  51. email: 'laoli@php.cn',
  52. },
  53. {
  54. name: '老陈',
  55. email: 'laochen@php.cn',
  56. },
  57. ],
  58. };
  59. },
  60. }).mount('.app');
  61. </script>
  62. </body>
  63. </html>

条件渲染

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>条件渲染: v-if</title>
  8. <script src="https://unpkg.com/vue@next"></script>
  9. </head>
  10. <body>
  11. <div class="app">
  12. <p v-if="flag">{{message}}</p>
  13. <button @click="flag=!flag" v-text="flag ?'隐藏' : '显示'"></button>
  14. <!-- if-else, if else if else -->
  15. <p v-if="point>=100 && point<500">{{grade[0]}}</p>
  16. <p v-else-if="point>=500 && point<1500">{{grade[1]}}</p>
  17. <p v-else-if="point>=1500 && point<2500">{{grade[2]}}</p>
  18. <p v-if="point>=2500">{{grade[3]}}</p>
  19. <!-- <p v-else>{{grade[4]}}</p> -->
  20. </div>
  21. <script>
  22. Vue.createApp({
  23. data(){
  24. return{
  25. message: 'xx网咖',
  26. flag: true,
  27. // 会员级别
  28. grade: ['普通会员', '黄金会员', '白银会员', '钻石会员', '非会员'],
  29. // 积分
  30. point: 2500,
  31. };
  32. },
  33. }).mount('.app');
  34. </script>
  35. </body>
  36. </html>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议