Blogger Information
Blog 94
fans 0
comment 0
visits 92680
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
【Vue】 Vue 计算属性和侦听器属性与实战
可乐随笔
Original
1432 people have browsed it

Vue 计算属性和侦听器属性与实战

1. Vue 计算属性 和 侦听器属性

Vue实例 计算属性 computed:
Vue 侦听器属性 watch:

  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>计算属性和侦听器属性:本质上就是方法</title>
  8. <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
  9. <style>
  10. input {
  11. width: 5em;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <div id="app">
  17. <p>2数求和</p>
  18. <input type="number" v-model="a">+
  19. <input type="number" v-model="b">=
  20. <!-- 表达式 -->
  21. <!-- <span>{{a + b}}</span> -->
  22. <!-- 函数 -->
  23. <!-- <span>{{ add()}}</span> -->
  24. <!-- 计算属性(访问器) -->
  25. <span>{{ sum }}</span>
  26. </div>
  27. <script>
  28. Vue.createApp({
  29. data() {
  30. return {
  31. //Vue中需要一个初始值
  32. a : 0,
  33. b : 0,
  34. };
  35. },
  36. methods: {
  37. // add (){
  38. // return this.a + this.b;
  39. // },
  40. },
  41. //计算属性(与函数区别:可以缓存结果)
  42. computed:{
  43. sum(){
  44. return this.a + this.b;
  45. }
  46. },
  47. //侦听器
  48. watch:{
  49. sum(curr,prev){
  50. //curr:当前值(新),prev:原值(旧)
  51. console.log(curr,prev);
  52. if(curr >= 20){
  53. alert('恭喜,您中奖了!')
  54. }
  55. }
  56. }
  57. }).mount('#app');
  58. </script>
  59. </body>
  60. </html>

2. Vue 实战1:购物车实自动计算(计算属性与侦听器属性)

总结:Vue实例:数据/方法/钩子函数/计算属性/侦听器 综合应用实战

生命周期钩子函数 mounted(),实例挂载成功后执行

  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>实战1:购物车实自动计算</title>
  8. <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
  9. <style>
  10. input {
  11. width: 5em;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <div id="app">
  17. <h3>购物车计算</h3>
  18. <input type="number" v-model="num" min="1">
  19. <span> &times; </span>
  20. <span>{{price}}</span>
  21. <span> = </span>
  22. <!-- <span>{{ num * price}} 元</span> -->
  23. <!-- 计算属性 -->
  24. <span>{{ shouldPay }} 元</span>
  25. <p>优惠:<span>{{discount}} 元,实付:{{realPay}}</span></p>
  26. </div>
  27. <script>
  28. Vue.createApp({
  29. data() {
  30. return {
  31. //数量
  32. num: 5,
  33. //单价
  34. price: 150,
  35. //优惠金额
  36. discount: 0,
  37. //实付
  38. realPay: 0,
  39. //折扣率
  40. disRate: 1,
  41. };
  42. },
  43. //生命周期钩子函数
  44. //mounted,实例挂载成功后执行
  45. mounted() {
  46. // 默认:实付 = 应付
  47. this.realPay = this.shouldPay;
  48. // 优惠
  49. this.discount = this.shouldPay - this.realPay;
  50. },
  51. //计算属性(与函数区别:可以缓存结果)
  52. computed: {
  53. //应付金额
  54. shouldPay(){
  55. return this.num * this.price;
  56. }
  57. },
  58. //侦听器
  59. watch: {
  60. //监听“应付金额”的变化,确定折扣比例和金额
  61. shouldPay(curr){
  62. //curr:必须;prev:可选
  63. // 1. 计算折扣率
  64. switch(true){
  65. // [2000,5000]:8折
  66. // [>=5000]:8折
  67. case curr >= 2000 && curr < 5000 : this.disRate = 0.8;
  68. break;
  69. case curr > 5000 :this.disRate = 0.5;
  70. }
  71. // 2.计算实付 = 应付 * 折扣率
  72. this.realPay = this.shouldPay * this.disRate;
  73. // 3.计算优惠金额 = 应付 - 实付
  74. this.discount = this.shouldPay - this.realPay;
  75. }
  76. },
  77. }).mount('#app');
  78. </script>
  79. </body>
  80. </html>

3. Vue 实战2:智能搜索(关键字过滤)

array.filter(callback):数据过滤器应用实例

  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>实战2:智能搜索</title>
  8. <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
  9. </head>
  10. <body>
  11. <div id="app">
  12. <input type="search" v-model="keywords" placeholder="请输入关键字">
  13. <ul>
  14. <li v-for="(lang,key) in matchlangs" :key="key">{{lang}}</li>
  15. <!-- 下面应该显示的是一个匹配了关键字的结果集:计算属性进行数据过滤 -->
  16. </ul>
  17. </div>
  18. <script>
  19. Vue.createApp({
  20. data() {
  21. return {
  22. //关键字
  23. keywords: '',
  24. //数据模拟
  25. langs: ['html', 'css', 'javascript', 'js', 'php', 'mysql'],
  26. };
  27. },
  28. //计算属性
  29. computed: {
  30. // 过滤结果
  31. matchlangs(){
  32. // 1.判断用户是否输入了内容
  33. if(this.keywords != ''){
  34. // 2.对结果进行过滤
  35. // array.filter(callback):返回true的元素数据
  36. return this.langs.filter((lang) => {
  37. // string.includes(str):查询子串,返回 true/false
  38. return lang.includes(this.keywords);
  39. });
  40. }
  41. },
  42. },
  43. }).mount('#app');
  44. </script>
  45. </body>
  46. </html>
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