Blogger Information
Blog 49
fans 0
comment 3
visits 23227
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
实例演示veu中的事件属性绑定_事件修饰符_条件渲染_列表渲染_键盘修饰符_计算属性_侦听器属性等
P粉479712293
Original
411 people have browsed it

题目一:采用vue语句的事件属性绑定事件方法

html文件如下:

  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. <script src="https://unpkg.com/vue@next"></script>
  8. <title>采用vue语句的事件属性绑定事件方法</title>
  9. </head>
  10. <body>
  11. <div class="user">
  12. <p>
  13. <!-- *事件属性 -->
  14. <!-- *当点击a这超链接时,调用myphp()函数 -->
  15. <a href="https://www.php.cn"
  16. @click="myphp($event)">迎接php:</a>
  17. <span class="url">{{url}}</span>
  18. </p>
  19. </div>
  20. <script src="../static/第26章/1采用vue语句的事件属性绑定事件方法.js"></script>
  21. </body>
  22. </html>

js文件如下:

  1. Vue.createApp({
  2. // *实例数据
  3. data(){
  4. return{
  5. // *占位变量
  6. url:null,
  7. };
  8. },
  9. // *事件方法
  10. methods:{
  11. myphp(ev){
  12. // *事件的阻止默认跳转
  13. ev.preventDefault();
  14. console.log(ev);
  15. this.url=ev.target.href;
  16. },
  17. },
  18. }).mount('.user');

浏览器运行效果图如下:

题目二:事件修饰符

html文件如下:

  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. <script src="https://unpkg.com/vue@next"></script>
  8. <title>事件修饰符</title>
  9. </head>
  10. <body>
  11. <div class="user">
  12. <p>
  13. <!-- *事件属性 -->
  14. <!-- *.prevent.stop为事件修饰符,其中prevent为禁用默认行为,stop为防止冒泡 -->
  15. <a href="https://www.php.cn"
  16. @click.prevent.stop="myphp($event)">迎接php:</a>
  17. <span class="url">{{url}}</span>
  18. </p>
  19. </div>
  20. <script src="../static/第26章/2事件修饰符.js"></script>
  21. </body>
  22. </html>

js文件如下:

  1. Vue.createApp({
  2. // *实例数据
  3. data(){
  4. return{
  5. // *占位变量
  6. url:null,
  7. };
  8. },
  9. // *事件方法
  10. methods:{
  11. myphp(ev){
  12. console.log(ev);
  13. this.url=ev.target.href;
  14. },
  15. },
  16. }).mount('.user');

浏览器运行效果图如下:

题目三:使用v-for循环的列表方式渲染

html文件如下:

  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. <script src="https://unpkg.com/vue@next"></script>
  8. <title>使用v-for循环的列表方式渲染</title>
  9. </head>
  10. <body>
  11. <div class="user">
  12. <ul>
  13. <li v-for="(user,index) of users" :key="index">
  14. {{user.name}}:({{user.nickname}})
  15. </li>
  16. </ul>
  17. </div>
  18. <script src="../static/第26章/3使用v-for循环的列表方式渲染.js"></script>
  19. </body>
  20. </html>

js文件如下:

  1. Vue.createApp({
  2. data(){
  3. return{
  4. users:[
  5. {
  6. name:'关羽',
  7. nickname:'过五关斩六将',
  8. },
  9. {
  10. name:'张飞',
  11. nickname:'杖八长茅',
  12. },
  13. {
  14. name:'赵云',
  15. nickname:'大战长板坡',
  16. },
  17. {
  18. name:'马超',
  19. nickname:'万夫不档之勇',
  20. },
  21. {
  22. name:'黄忠',
  23. nickname:'八十不服老',
  24. },
  25. ],
  26. };
  27. },
  28. }).mount('.user');

浏览器运行效果图如下:

题目四:使用v-if控制的条件渲染

html文件如下:

  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. <script src="https://unpkg.com/vue@next"></script>
  8. <title>使用v-if控制的条件渲染</title>
  9. </head>
  10. <body>
  11. <div class="user">
  12. <p v-if="flag">{{film}}</p>
  13. <button @click="flag=!flag" v-text="flag ? '隐藏' :'显示'"></button>
  14. </div>
  15. <script src="../static/第26章/4使用v-if控制的条件渲染.js"></script>
  16. </body>
  17. </html>

js文件如下:

  1. Vue.createApp({
  2. data(){
  3. return{
  4. film:'今天晚上放映电影:南征北战',
  5. flag:true,
  6. };
  7. },
  8. }).mount('.user');

浏览器运行效果图如下:

题目五:使用v-if-else控制的条件渲染

html文件如下:

  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. <script src="https://unpkg.com/vue@next"></script>
  8. <title>使用v-if-else控制的条件渲染</title>
  9. </head>
  10. <body>
  11. <div class="user">
  12. <p v-if="age >= 3 && age<6">{{education[0]}}</p>
  13. <p v-else-if="age>=6 && age<12">{{education[1]}}</p>
  14. <p v-else-if="age>=12 && age<15">{{education[2]}}</p>
  15. <p v-else-if="age>=15 && age<18">{{education[3]}}</p>
  16. <p v-if="age>=18" >{{education[4]}}</p>
  17. </div>
  18. <script src="../static/第26章/5使用v-if-else控制的条件渲染.js"></script>
  19. </body>
  20. </html>

js文件如下:

  1. Vue.createApp({
  2. data(){
  3. return{
  4. education:['幼儿园','小学',
  5. '初中','高中','大学'],
  6. age:16,
  7. };
  8. },
  9. }).mount('.user')

浏览器运行效果图如下:

题目六:

html文件如下:

  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. <script src="https://unpkg.com/vue@next"></script>
  8. <title>利用键盘修饰符enter时调用函数</title>
  9. </head>
  10. <body>
  11. <div class="user">
  12. <!-- *enter(回车键):键盘修饰符 -->
  13. <input type="text" @keydown.enter="submit($event)">
  14. <ul>
  15. <li v-for="(item,index) of list" :key="index">
  16. {{item}}
  17. </li>
  18. </ul>
  19. </div>
  20. <script src="../static/第26章/6利用键盘修饰符enter时调用函数.js"></script>
  21. </body>
  22. </html>

js文件如下:

  1. Vue.createApp({
  2. data(){
  3. return{
  4. // *留言列表
  5. list:[],
  6. };
  7. },
  8. methods:{
  9. submit(ev){
  10. this.list.unshift(ev.currentTarget.value);
  11. ev.currentTarget.value=null;
  12. },
  13. },
  14. }).mount('.user');

浏览器运行效果图如下:

题目七:

html文件如下:

  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. <script src="https://unpkg.com/vue@next"></script>
  8. <link rel="stylesheet" href="../static/第26章/css/7计算属性与侦听器属性.css">
  9. <title>Document</title>
  10. </head>
  11. <body>
  12. <div class="user">
  13. <table>
  14. <caption>
  15. 商品结算
  16. </caption>
  17. <tbody>
  18. <tr>
  19. <th>ID</th>
  20. <td>HA110</td>
  21. </tr>
  22. <tr>
  23. <th>品名</th>
  24. <td>伊利纯牛奶</td>
  25. </tr>
  26. <tr>
  27. <th>单价</th>
  28. <td>100</td>
  29. </tr>
  30. <tr>
  31. <th>数量</th>
  32. <td><input type="number" v-model="num" style="width: 5em" /></td>
  33. </tr>
  34. <tr>
  35. <th>金额</th>
  36. <!-- <td>{{num * price}}</td> -->
  37. <td>{{amount}}</td>
  38. </tr>
  39. </tbody>
  40. </table>
  41. <p style="font-size: 1.2em">
  42. 实付金额: {{realAmount}}, 优惠了 :
  43. <span style="color: red">{{difAmount}}</span>
  44. </p>
  45. </div>
  46. <script src="../static/第26章/7计算属性与侦听器属性.js"></script>
  47. </body>
  48. </html>

js文件如下:

  1. const app = Vue.createApp({
  2. data() {
  3. return {
  4. // 单价
  5. price: 100,
  6. // 数量
  7. num: 0,
  8. // 折扣
  9. discount: 1,
  10. };
  11. },
  12. // 计算属性(访问器属性)
  13. computed: {
  14. // 计算属性金额 = 单价 * 数量
  15. // get amount(){
  16. // }
  17. amount: {
  18. get() {
  19. console.log(this);
  20. return this.price * this.num;
  21. },
  22. set(value) {
  23. //...
  24. },
  25. },
  26. },
  27. // 侦听器属性
  28. watch: {
  29. // 访问器属性
  30. // 被侦听的属性,其实是一个方法,它有二个参数
  31. // 第一个参数是新值(当前值),第二个参数是原值(旧值)
  32. amount(current, origin) {
  33. console.log(current, origin);
  34. // 根据当前金额确定打折
  35. switch (true) {
  36. // 1000-2000: 9折
  37. case current >= 1000 && current < 2000:
  38. console.log(this);
  39. this.discount = 0.9;
  40. break;
  41. // 2000 -> 3000 : 8折
  42. case current >= 2000 && current < 3000:
  43. this.discount = 0.8;
  44. break;
  45. // 3000 -> 4000 : 7折
  46. case current >= 3000 && current < 4000:
  47. this.discount = 0.7;
  48. break;
  49. // 4000 -> 5000 : 6折
  50. case current >= 4000 && current < 5000:
  51. this.discount = 0.6;
  52. break;
  53. // 5000 : 5折
  54. case current >= 5000:
  55. this.discount = 0.5;
  56. }
  57. // 实付金额 = 原始金额 * 折扣率
  58. this.realAmount = this.amount * this.discount;
  59. // 优惠金额(差价) = 原始金额 - 实付金额
  60. this.difAmount = this.amount - this.realAmount;
  61. },
  62. },
  63. // 实例生命周期: 当实例与挂载点绑定成功时,自动执行
  64. mounted() {
  65. //初始化商品数量,默认为1
  66. this.num = 1;
  67. },
  68. }).mount('.user');

css文件如下:

  1. table {
  2. width: 20em;
  3. height: 10em;
  4. text-align: center;
  5. border-collapse: collapse;
  6. margin: 1em;
  7. }
  8. table caption {
  9. font-size: 1.2em;
  10. padding: 1em;
  11. margin-bottom: 2em;
  12. border-bottom: 1px solid;
  13. }
  14. tbody tr th:first-of-type {
  15. background: linear-gradient(to left, lightcyan, #fff);
  16. border-right: 1px solid;
  17. }
  18. body tbody tr:not(:last-of-type) > * {
  19. border-bottom: 1px solid;
  20. }

浏览器运行效果图如下:

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