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

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

秋闲独醉
秋闲独醉原创
2022年08月06日 21:17:58298浏览
  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. <style>
  9. table {
  10. width: 20em;
  11. height: 10em;
  12. text-align: center;
  13. border-collapse: collapse;
  14. margin: 1em;
  15. }
  16. table caption {
  17. font-size: 1.2em;
  18. padding: 1em;
  19. margin-bottom: 2em;
  20. border-bottom: 1px solid;
  21. }
  22. tbody tr th:first-of-type {
  23. background: linear-gradient(to left, lightcyan, #fff);
  24. border-right: 1px solid;
  25. }
  26. body tbody tr:not(:last-of-type) > * {
  27. border-bottom: 1px solid;
  28. }
  29. </style>
  30. <script src="https://unpkg.com/vue@next"></script>
  31. </head>
  32. <body>
  33. <!-- es6 -->
  34. <!-- <p> -->
  35. <!-- 事件属性 -->
  36. <!-- <a href="https://www.php.cn" onclick="showUrl(this)">show URL : </a>
  37. <span class="url"></span>
  38. </p> -->
  39. <!-- <script>
  40. // es6 原生方法
  41. function showUrl(ele) {
  42. event.preventDefault();
  43. console.log(ele.nextElementSibling);
  44. console.log(event.currentTarget.nextElementSibling);
  45. // event.currentTarget.nextElementSibling.textContent = event.currentTarget.href;
  46. ele.nextElementSibling.textContent = ele.href;
  47. }
  48. </script> -->
  49. <!-- Vue -->
  50. <!-- <div id="app"> -->
  51. <!-- 事件属性 -->
  52. <!-- <a href="https://www.php.cn" v-on:click="showUrl($event)">show URL : </a> -->
  53. <!-- <span v-text="url"></span> -->
  54. <!-- <span v-text="'url'"></span> -->
  55. <!-- </div> -->
  56. <!-- 循环渲染 -->
  57. <!-- <div id="app">
  58. <ul>
  59. <li v-text="cities[0]"></li>
  60. <li v-text="cities[1]"></li>
  61. <li v-text="cities[2]"></li>
  62. <li v-text="cities[3]"></li>
  63. </ul>
  64. <ul>
  65. <li v-for="city of cities" v-bind:key="index">{{city}}</li>
  66. </ul>
  67. <ul>
  68. <li v-text="user.name"></li>
  69. <li v-text="user.age"></li>
  70. </ul>
  71. <ul>
  72. <li v-for="(user,index) of users" v-bind:key="index">第{{++index}}-{{user.name}}:{{user.age}}岁</li>
  73. </ul>
  74. </div> -->
  75. <!-- 条件渲染 -->
  76. <!-- <div id="app">
  77. <p v-if="flag">{{content}}</p>
  78. <button v-on:click="flag = !flag" v-text="flag ? '隐藏':'显示'"></button>
  79. <p v-if="distance >1000">我在{{cities[0]}}</p>
  80. <p v-else-if="distance>800 & distance <=1000">我在{{cities[1]}}</p>
  81. <p v-else-if="distance >500 & distance <=800">我在{{cities[2]}}</p>
  82. <p v-else-if="distance <=500">我在{{cities[3]}}</p>
  83. </div> -->
  84. <!-- Vue留言版 -->
  85. <!-- <div id="app"> -->
  86. <!-- <input type="text" placeholder="请输入你的留言" v-on:keydown="talk($event)" /> -->
  87. <!-- enter:键盘修饰符,代表回车 .enter -->
  88. <!-- <input type="text" placeholder="请输入你的留言" v-on:keydown.enter="talk($event)" />
  89. <ul>
  90. <li v-for="msg of message" :key="index">{{msg}}</li>
  91. </ul>
  92. </div> -->
  93. <!-- 计算属性,侦听器属性 -->
  94. <div id="app">
  95. <table>
  96. <caption>
  97. 商品结算
  98. </caption>
  99. <tbody>
  100. <tr>
  101. <th>ID</th>
  102. <td>HA110</td>
  103. </tr>
  104. <tr>
  105. <th>品名</th>
  106. <td>伊利纯牛奶</td>
  107. </tr>
  108. <tr>
  109. <th>单价</th>
  110. <td>100</td>
  111. </tr>
  112. <tr>
  113. <th>数量</th>
  114. <td><input type="number" v-model="num" style="width: 5em" /></td>
  115. </tr>
  116. <tr>
  117. <th>金额</th>
  118. <!-- <td>{{num * price}}</td> -->
  119. <td>{{amount}}</td>
  120. </tr>
  121. </tbody>
  122. </table>
  123. <p style="font-size: 1.2em">
  124. 实付金额: {{realAmount}}, 优惠了 :
  125. <span style="color: red">{{difAmount}}</span>
  126. </p>
  127. </div>
  128. <script>
  129. const app = Vue.createApp({
  130. data() {
  131. return {
  132. url: null,
  133. cities: ["北京", "上海", "杭州", "厦门"],
  134. user: {
  135. name: "小鸡",
  136. age: 18,
  137. },
  138. users: [
  139. {
  140. name: "小马",
  141. age: 19,
  142. },
  143. {
  144. name: "小猪",
  145. age: 20,
  146. },
  147. {
  148. name: "小牛",
  149. age: 30,
  150. },
  151. ],
  152. content: "你找到我了,恭喜你",
  153. flag: false,
  154. distance: 600,
  155. message: [],
  156. price: 100,
  157. num: 0,
  158. discount: 1,
  159. };
  160. },
  161. methods: {
  162. showUrl(ev) {
  163. ev.preventDefault();
  164. this.url = ev.currentTarget.href;
  165. },
  166. talk(ev) {
  167. console.log(ev);
  168. // if (ev.key === "Enter") {
  169. // this.message.unshift(ev.currentTarget.value);
  170. // ev.currentTarget.value = null;
  171. // }
  172. this.message.unshift(ev.currentTarget.value);
  173. ev.currentTarget.value = null;
  174. },
  175. },
  176. //计算属性(访问器属性)
  177. computed: {
  178. amount: {
  179. get() {
  180. return this.price * this.num;
  181. },
  182. },
  183. },
  184. //侦听属性
  185. watch: {
  186. amount(current, origin) {
  187. console.log(current, origin);
  188. switch (true) {
  189. case current >= 1000 && current < 2000:
  190. this.discount = 0.9;
  191. break;
  192. case current >= 2000 && current < 3000:
  193. this.discount = 0.8;
  194. break;
  195. case current >= 3000 && current < 5000:
  196. this.discount = 0.6;
  197. break;
  198. }
  199. this.realAmount = this.amount * this.discount;
  200. this.difAmount = this.amount - this.realAmount;
  201. },
  202. },
  203. mounted() {
  204. this.num = 1;
  205. },
  206. }).mount("#app");
  207. </script>
  208. </body>
  209. </html>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议