1.列表渲染.html 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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>Document</title>
  8. <script src="../../vue.js"></script>
  9. </head>
  10. <body>
  11. <div id="app">
  12. <div class="user-list">
  13. <table>
  14. <thead>
  15. <th>序号</th>
  16. <th>姓名</th>
  17. <th>性别</th>
  18. </thead>
  19. <tbody>
  20. <!-- 在Vue中 可以 使用v-for指令来根据数组或者对象 去 循环创建 HTML元素 -->
  21. <!-- v-for="循环变量 in|of users" -->
  22. <!-- 在 tr 或者 后代节点中 比如td 都可以使用该循环变量 user -->
  23. <!-- <tr v-for="user of users">
  24. <td>{{ user.name }}</td>
  25. <td>{{ user.sex }}</td>
  26. </tr> -->
  27. <!-- v-for="(value, index) in|of users" -->
  28. <tr v-for="(user, index) of users">
  29. <td>{{ index + 1 }}</td>
  30. <td>{{ user.name }}</td>
  31. <td>{{ user.sex }}</td>
  32. </tr>
  33. </tbody>
  34. </table>
  35. </div>
  36. </div>
  37. <script>
  38. var vm = new Vue({
  39. data: {
  40. users: [
  41. {
  42. id: 1,
  43. name: '郭郭',
  44. sex: '男',
  45. },
  46. {
  47. id: 2,
  48. name: '静静',
  49. sex: '女',
  50. },
  51. {
  52. id: 3,
  53. name: '过儿',
  54. sex: '男',
  55. },
  56. {
  57. id: 4,
  58. name: '龙儿',
  59. sex: '女',
  60. },
  61. ],
  62. },
  63. });
  64. // 除了使用el外,还可以使用vm的$mount方法指定视图的容器元素
  65. vm.$mount('#app');
  66. </script>
  67. </body>
  68. </html>