练习2_商品列表.html 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <script src="./js/vue.js"></script>
  8. <style>
  9. .box {
  10. display: flex;
  11. flex-wrap: wrap;
  12. }
  13. .goods-card {
  14. width: 234px;
  15. box-shadow: 0 0 5px gray;
  16. padding-bottom: 30px;
  17. margin-left: 20px;
  18. margin-bottom: 20px;
  19. }
  20. .goods-pic img {
  21. width: 234px;
  22. }
  23. .goods-title,
  24. .goods-info,
  25. .goods-price {
  26. padding: 0 10px;
  27. }
  28. .goods-title {
  29. color: #333;
  30. font-size: 14px;
  31. font-weight: 400;
  32. text-align: center;
  33. margin-bottom: 2px;
  34. }
  35. .goods-info {
  36. font-size: 12px;
  37. color: #b0b0b0;
  38. overflow: hidden;
  39. text-overflow: ellipsis;
  40. text-wrap: nowrap;
  41. margin-bottom: 10px;
  42. }
  43. .goods-price {
  44. text-align: center;
  45. }
  46. .goods-price span {
  47. color: #ff6700;
  48. font-size: 14px;
  49. font-weight: 400;
  50. }
  51. .goods-price del {
  52. color: #b0b0b0;
  53. font-size: 14px;
  54. font-weight: 400;
  55. }
  56. </style>
  57. </head>
  58. <body>
  59. <div id="app">
  60. <button @click="getData">获取商品列表</button>
  61. <div class="box">
  62. <div class="goods-card" v-for="item in productDtoList">
  63. <div class="goods-pic">
  64. <img v-bind:src="item.pic" alt="phone">
  65. </div>
  66. <div class="goods-title">
  67. <span>{{item.prodName}}</span>
  68. </div>
  69. <div class="goods-info">
  70. {{item.brief}}
  71. </div>
  72. <div class="goods-price">
  73. <span>¥{{item.price}}</span>
  74. </div>
  75. </div>
  76. </div>
  77. </div>
  78. <script>
  79. new Vue({
  80. // 绑定Vue实例到id为app的元素 (挂载点)
  81. el: "#app",
  82. // data属性用于存储页面中要显示的数据
  83. data: {
  84. productDtoList: [],
  85. num:0,
  86. str:"hello world!",
  87. },
  88. methods: {
  89. getData() {
  90. // 定义that指向vue实例
  91. let that = this;
  92. // 第一个步创建XMLHttpRequest对象
  93. let xhr = new XMLHttpRequest();
  94. // 第二个步调用open方法配置请求信息
  95. xhr.open("GET", "http://shop-api.edu.koobietech.com/prod/tagProdList");
  96. // 第三步发送请求
  97. xhr.send();
  98. // 第四步监听响应
  99. xhr.onreadystatechange = function () {
  100. if (xhr.readyState == 4 && xhr.status == 200) {
  101. let jsonStr = xhr.responseText;
  102. // 服务端返回的数据为JSON字符串
  103. // 把JSON字符串转换为对象
  104. let obj = JSON.parse(jsonStr);
  105. // 将获取到的值放到data中
  106. that.productDtoList = obj.data[0].productDtoList
  107. }
  108. }
  109. }
  110. }
  111. });
  112. </script>
  113. </body>
  114. </html>