Blogger Information
Blog 94
fans 0
comment 0
visits 92790
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
【Vue】条件渲染: V-for:列表渲染、条件渲染: v-if和键盘修饰符
可乐随笔
Original
1457 people have browsed it

条件渲染: V-for:列表渲染、条件渲染: v-if和键盘修饰符

1. Vue V-for:列表渲染

  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@3/dist/vue.global.js"></script>
  9. <div class="app">
  10. <ul>
  11. <li>{{cities[0]}}</li>
  12. <li>{{cities[1]}}</li>
  13. <li>{{cities[2]}}</li>
  14. </ul>
  15. <hr>
  16. <!-- 数组渲染 -->
  17. <!-- v-for指令快速循环输出列表 -->
  18. <ul>
  19. <!-- key:diff算法,实现高效的渲染,大数量时有用 -->
  20. <li v-for="(city,key) of cities" :key="key">{{key}} : {{city}}</li>
  21. </ul>
  22. <hr>
  23. <!-- 对象渲染 -->
  24. <ul>
  25. <li v-for="(item,key) of user" :key="key">{{key}} : {{item}}</li>
  26. </ul>
  27. <hr>
  28. <!-- 对象渲染 -->
  29. <ul>
  30. <li v-for="(item,key) of users" :key="key"> {{item.name}} : {{item.email}} : {{item.phone}}</li>
  31. </ul>
  32. </div>
  33. <script>
  34. const app = Vue.createApp({
  35. data() {
  36. return {
  37. // 数组类型
  38. cities: ['合肥', '上海', '南京'],
  39. // 对象类型
  40. user: {
  41. name:'老马',
  42. email:'nx77@qq.com',
  43. phone:'13909511100',
  44. },
  45. // 对象数组
  46. users:[
  47. {
  48. name:'老马',
  49. email:'nx77@qq.com',
  50. phone:'13909511100',
  51. },
  52. {
  53. name:'老李',
  54. email:'nx88@qq.com',
  55. phone:'13909511100',
  56. },
  57. {
  58. name:'老王',
  59. email:'nx77@qq.com',
  60. phone:'13909511100',
  61. },
  62. ]
  63. };
  64. },
  65. });
  66. const vm = app.mount('.app');
  67. </script>
  68. </head>
  69. <body>
  70. </body>
  71. </html>

2. Vue 条件渲染: v-if

  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@3/dist/vue.global.js"></script>
  9. </head>
  10. <body>
  11. <div class="app">
  12. <!-- v-if:条件渲染指令 -->
  13. <p v-if="flag">{{message}}</p>
  14. <!-- !取反操作 ,如:flag = !flag-->
  15. <button @click="flag = !flag" v-text="flag ? '隐藏':'显示'"></button>
  16. <!-- if-else,if elseif if -->
  17. <!-- p >= 1000 and p < 2000 -->
  18. <p v-if="point >= 1000 && point < 2000">{{grade[0]}}</p>
  19. <p v-else-if="point >= 2000 && point < 3000">{{grade[1]}}</p>
  20. <p v-else-if="point >= 3000 && point < 4000">{{grade[2]}}</p>
  21. <p v-else>{{grade[3]}}</p>
  22. </div>
  23. <script>
  24. Vue.createApp({
  25. data() {
  26. return {
  27. message: '前端快结束了',
  28. flag: true,
  29. //会员plus级别
  30. grade:['普通会员','高级会员','金牌会员','至尊会员'],
  31. //积分
  32. point:5000,
  33. };
  34. },
  35. }).mount('.app');
  36. </script>
  37. </body>
  38. </html>

3. Vue 键盘修饰符

  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>键盘修饰符</title>
  8. <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
  9. </head>
  10. <body>
  11. <div class="app">
  12. <!-- @keyup.enter :Vue监听鼠标回车事件 -->
  13. <input type="text" @keyup.enter="submit($event)">
  14. <ul>
  15. <li v-for="(item,key) of list">{{item}}</li>
  16. </ul>
  17. </div>
  18. <script>
  19. Vue.createApp({
  20. data() {
  21. return {
  22. // 留言列表
  23. list: [],
  24. };
  25. },
  26. methods: {
  27. submit(ev) {
  28. // 将输入的字符串放入 list 数组
  29. this.list.unshift(ev.target.value);
  30. ev.target.value = null;
  31. },
  32. },
  33. }).mount('.app');
  34. </script>
  35. </body>
  36. </html>
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