Blogger Information
Blog 46
fans 2
comment 0
visits 19475
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
1. 实例演示条件渲染与列表渲染 2. (选做): 使用计算属性,侦听器属性和键盘修饰符等知识 , 重写之前的购物车案例 3. 预习组件知识
P粉314265155
Original
295 people have browsed it

事件绑定

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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@next"></script>
  9. </head>
  10. <body>
  11. <!-- es6 -->
  12. <p>
  13. <!-- <a href="https://www.baidu.com" onclick = "showURL( )">URL地址跳转 </a> -->
  14. <!-- 事件属性 -->
  15. <a href="https://www.php.cn" onclick="showUrl()">显示地址一 : </a>
  16. <span class="url"> </span>
  17. </p>
  18. <!-- vue -->
  19. <div class="app">
  20. <p >
  21. <!-- <a href="https://www.baidu.com" onclick = "showURL( )">URL地址跳转 </a>
  22. -->
  23. <!-- 事件属性 v-on @ -->
  24. <a href="https://www.php.cn" @click="showUrl1($event)">显示地址二 : </a>
  25. <span class="url1">{{url1}} </span>
  26. </p>
  27. </div>
  28. <!-- 简化 -->
  29. <div class="app">
  30. <p onclick=" console.log( '你好呀');">
  31. <!-- <a href="https://www.baidu.com" onclick = "showURL( )">URL地址跳转 </a>
  32. -->
  33. <!-- 事件属性 v-on @ -->
  34. <a href="https://www.php.cn" @click="showUrl2($event)">显示地址三 : </a>
  35. <span class="url2">{{url2}} </span>
  36. </p>
  37. </div>
  38. <script>
  39. // 为事件属性绑定 事件方法
  40. function showUrl(){
  41. // 禁用跳转行为
  42. event.preventDefault();
  43. console.log( EventTarget);
  44. console.log(event.target);
  45. console.log(event.target.href);
  46. console.log(event.currentTarget);
  47. console.log(event.currentTarget.href);
  48. console.log('=======================');
  49. // 获取下一个兄弟节点event.nextElementSibling.textConent
  50. console.log(event.target.nextElementSibling);
  51. // 下一个兄弟节点内容修改
  52. // event.target.nextElementSibling.textContent = event.target.href;
  53. console.log('+++++++++++++++++++++++++++++');
  54. }
  55. </script>
  56. <!-- // 为事件属性绑定 事件方法 VUE -->
  57. <script>
  58. Vue.createApp({
  59. // 实例数据
  60. data() {
  61. return {
  62. // 插值变量
  63. url1: null,
  64. };
  65. },
  66. // 事件方法
  67. methods: {
  68. showUrl1( ev){
  69. ev.preventDefault();
  70. console.log(ev);
  71. console.log(ev.target.href);
  72. console.log(ev.currentTarget.href);
  73. this.url1 = ev.currentTarget.href;
  74. },
  75. },
  76. }).mount('.app');
  77. </script>
  78. </body>
  79. </html>
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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@next"></script>
  9. </head>
  10. <body>
  11. <div class="app">
  12. <p onclick=" console.log( '你好呀');">
  13. <!-- <a href="https://www.baidu.com" onclick = "showURL( )">URL地址跳转 </a>
  14. -->
  15. <!-- 事件属性 v-on @ -->
  16. <a href="https://www.php.cn" @click.prevent.stop="showUrl2($event)">显示地址三 : </a>
  17. <span class="url2">{{url2}} </span>
  18. </p>
  19. </div>
  20. <!-- // 为事件属性绑定 事件方法 VUE -->
  21. <script>
  22. Vue.createApp({
  23. // 实例数据
  24. data() {
  25. return {
  26. // 插值变量
  27. url2: null,
  28. };
  29. },
  30. // 事件方法
  31. methods: {
  32. showUrl2( ev){
  33. // 其中 ev.preventDefault();和 ev.stopPropagation(); 可以简化 用修饰符简化, 如 @click.prevent.stop="showUrl2($event)"
  34. // 去掉时间点击跳转行为
  35. // ev.preventDefault();
  36. // 去掉冒泡行为
  37. // ev.stopPropagation();
  38. console.log(ev);
  39. console.log(ev.target.href);
  40. console.log(ev.currentTarget.href);
  41. this.url2 = ev.currentTarget.href;
  42. },
  43. },
  44. }).mount('.app');
  45. </script>
  46. </body>
  47. </html>

列表渲染(数组、关联数组)

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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@next"></script>
  9. </head>
  10. <body>
  11. <div class="app">
  12. <!-- 数组 array -->
  13. <p>{{cities}}</p>
  14. <p>{{cities[1]}}</p>
  15. <ul>
  16. <li>{{cities[1]}}</li>
  17. <li>{{cities[2]}}</li>
  18. </ul>
  19. <!-- fot of 循环数组
  20. for(let item of array) {
  21. }
  22. -->
  23. <!-- v-for :指令循环输出 -->
  24. <ul>
  25. <li v-for="city of cities">{{city}}</li>
  26. </ul>
  27. <ul>
  28. <!-- index 是键 key ,可以激发diff算法 city是值value :key 等于 v-bind:key -->
  29. <!-- 注意 of前后空格 -->
  30. <li v-for="(city,index) of cities" :key="index">{{index}}:{{city}}</li>
  31. </ul>
  32. console.log('=====');
  33. <p>{{cities}}</p>
  34. <hr>
  35. <!-- v-for 对象数组 -->
  36. <ul>
  37. <!-- prop属性 -->
  38. <li v-for="(user,prop,index) of users" :key="index">{{index}}:{{user}}</li>
  39. <li v-for="(user,prop,index) of users" :key="index">{{user.name}},{{user.email}}</li>
  40. </ul>
  41. <hr>
  42. <ul>
  43. <!-- 对象 -->
  44. <li >{{user.name}}:{{user.email}}</li>
  45. </ul>
  46. </div>
  47. <script>
  48. const app = Vue.createApp({
  49. data() {
  50. return {
  51. // 数组 array
  52. cities:['1','2','3','4','5'],
  53. // 对象 object 关联数组
  54. user:{
  55. name:'小狗',
  56. email:'123@qq.com',
  57. },
  58. // 对象数组 array of object
  59. users :[
  60. {
  61. name:'小猫',
  62. email:'123@qq.com',
  63. },
  64. {
  65. name:'小牛',
  66. email:'123@qq.com',
  67. },
  68. ],
  69. }
  70. },
  71. }, ).mount('.app');
  72. </script>
  73. </body>
  74. </html>

条件渲染v-if

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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>{{message}}</p>
  13. <p v-if = 'true'>{{message}},ture显示</p>
  14. <p v-if = 'false'>{{message}},false不显示</p>
  15. <p v-if = 'flag'>{{message}}</p>
  16. <!-- 开关控制 -->
  17. <!-- <button>隐藏</button> -->
  18. <hr>
  19. <button v-text = 'flag?"隐藏":"显示"'></button>
  20. <br>
  21. <button v-text = 'flag1?"隐藏":"显示"'></button>
  22. <hr>
  23. <p v-if = 'flag'>{{message}}</p>
  24. <button @click = 'flag=!flag' v-text = 'flag1?"隐藏":"显示"'></button>
  25. <hr>
  26. <!-- if -else if else if else -->
  27. <!-- <p v-if ='point'>{{grade[4]}}</p> -->
  28. console.log('=======');<br>
  29. <p v-if ='point>100 && point< 1000'>{{grade[0]}}</p>
  30. <!-- console.log('=======');<br> 条件判断中间不能加其他的标签 -->
  31. <p v-else-if='point>=2000 && point< 3000'>{{grade[1]}}</p>
  32. <p v-else-if="point>=3000 && point< 4000">{{grade[2]}}</p>
  33. <p v-else-if="point>=5000 && point< 6000">{{grade[3]}}</p>
  34. <!-- <p v--else-if="point>=4000 && point< 5000">{{grade[3]}}</p> -->
  35. <p v-else>{{grade[4]}}</p>
  36. </div>
  37. <script>
  38. const app = Vue.createApp({
  39. data() {
  40. return {
  41. message:'今天好热',
  42. // true 显示 false 不显示 style = display: none
  43. flag :true,
  44. flag1 :false,
  45. grade:['普通','银卡','金卡','白金','非会员'],
  46. point:5100,
  47. };
  48. },
  49. }).mount('.app');
  50. </script>
  51. </body>
  52. </html>

修饰符

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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="https://unpkg.com/vue@next"></script>
  9. </head>
  10. <body>
  11. <div class="app">
  12. <!-- <input type="text" @keydown = 'submit($event)'> -->
  13. <!-- enter 键盘修饰符 回车-->
  14. <input type="text" @keydown.enter = 'submit($event)'>
  15. <ul>
  16. <!-- :key 为了增加渲染的效率 -->
  17. <li v-for="(item ,index) of list" :key="index">{{item}} </li>
  18. </ul>
  19. </div>
  20. <script>
  21. Vue.createApp({
  22. data() {
  23. return {
  24. // 留言列表
  25. list:[],
  26. };
  27. },
  28. methods: {
  29. submit(ev) {
  30. console.log(ev);
  31. if (ev.key === 'Enter') {
  32. // this.list.unshift(ev.currentTarget.value);
  33. // ev.currentTarget.value = null;
  34. }
  35. this.list.unshift(ev.currentTarget.value);
  36. ev.currentTarget.value = null;
  37. },
  38. },
  39. // methods: {
  40. // submit:(ev){
  41. // console.log(ev);
  42. // if (ev.key =='Enter'){
  43. // this.List.unshift(ev.currentTarget.value);
  44. // }
  45. // }
  46. // },
  47. }).mount('.app');
  48. </script>
  49. </body>
  50. </html>

计算属性,侦听器属性

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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@next"></script>
  9. </head>
  10. <style>
  11. table {
  12. width: 20em;
  13. height: 10em;
  14. text-align: center;
  15. border-collapse: collapse;
  16. margin: 1em;
  17. }
  18. table caption {
  19. font-size: 1.2em;
  20. padding: 1em;
  21. margin-bottom: 2em;
  22. border-bottom: 1px solid;
  23. }
  24. tbody tr th:first-of-type {
  25. background: linear-gradient(to left, lightcyan, #fff);
  26. border-right: 1px solid;
  27. }
  28. body tbody tr:not(:last-of-type) > * {
  29. border-bottom: 1px solid;
  30. }
  31. </style>
  32. </head>
  33. <body>
  34. <div class="app">
  35. <table>
  36. <caption>
  37. 商品结算
  38. </caption>
  39. <tbody>
  40. <tr>
  41. <th>ID</th>
  42. <td>HA110</td>
  43. </tr>
  44. <tr>
  45. <th>品名</th>
  46. <td>伊利纯牛奶</td>
  47. </tr>
  48. <tr>
  49. <th>单价</th>
  50. <td>100</td>
  51. </tr>
  52. <tr>
  53. <th>数量</th>
  54. <!-- v- modle 双向绑定 -->
  55. <td><input type="number" v-model="num" style="width: 5em" /></td>
  56. </tr>
  57. <tr>
  58. <th>金额</th>
  59. <!-- <td>{{num * price}}</td> -->
  60. <td>{{amount}}</td>
  61. </tr>
  62. </tbody>
  63. </table>
  64. <p style="font-size: 1.2em">
  65. 实付金额: {{realAmount}}
  66. <br>
  67. 优惠了 :
  68. <span style="color: red">{{difAmount}}</span>
  69. </p>
  70. </div>
  71. <script>
  72. const app = Vue.createApp({
  73. data() {
  74. return {
  75. // 单价
  76. price: 100,
  77. // 数量
  78. num: 0,
  79. // 折扣
  80. discount: 1,
  81. // 实际金额
  82. // realAmount:100,
  83. // 优惠金额
  84. difAmount:0,
  85. };
  86. },
  87. // 计算器属性
  88. computed:{
  89. // 计算属性金额 = 单价乘以数量
  90. // get amount(){
  91. // },
  92. amount:{
  93. get(){
  94. return this.price * this.num;
  95. },
  96. set (value){
  97. },
  98. },
  99. },
  100. // 侦听器属性
  101. watch:{
  102. // 访问器属性实现。看着是属性,实际是方法
  103. // 被侦听的 属性其实一个方法 有两个参数组装,第一是 新值当前值 第二个值是原来的值,以前的旧值
  104. amount( current,origin){
  105. // console.log('当前'+ current ,'旧值'+origin);
  106. // 根据当前金额确定打折
  107. switch (true) {
  108. // 1000-2000: 9折
  109. case current >= 1000 && current < 2000:
  110. this.discount = 0.9;
  111. break;
  112. // 2000 -> 3000 : 8折
  113. case current >= 2000 && current < 3000:
  114. this.discount = 0.8;
  115. break;
  116. // 3000 -> 4000 : 7折
  117. case current >= 3000 && current < 4000:
  118. this.discount = 0.7;
  119. break;
  120. // 4000 -> 5000 : 6折
  121. case current >= 4000 && current < 5000:
  122. this.discount = 0.6;
  123. break;
  124. // 5000 : 5折
  125. case current >= 5000:
  126. this.discount = 0.5;
  127. }
  128. // 实付金额 =原始金额*折扣率
  129. // this.realAmount = this.amount * this.discount;
  130. // 优惠金额(差价) =原始金额 -实付金额
  131. // this.difAmount = this.amount - this.realAmount ;
  132. // 实付金额 = 原始金额 * 折扣率
  133. this.realAmount = this.amount * this.discount;
  134. console.log(this.realAmount);
  135. // 优惠金额(差价) = 原始金额 - 实付金额
  136. this.difAmount = this.amount - this.realAmount;
  137. },
  138. },
  139. // 实例的声明周期,当实例与挂载点绑定成功时,自动执行
  140. mounted(){
  141. this.num =1;
  142. }
  143. // 挂载
  144. }).mount('.app');
  145. </script>
  146. </body>
  147. </html>
Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!