当前位置:   article > 正文

02-Vue 计算属性与监听器与VUE-cli使用

02-Vue 计算属性与监听器与VUE-cli使用

1.计算属性

  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="../js/vue.js"></script>
  9. </head>
  10. <body>
  11. <div id="root">
  12. 姓:<input type="text" v-model="firstName"><br><br>
  13. 名:<input type="text" v-model="lastName"><br><br>
  14. 姓名:<span>{{fullName}}</span>
  15. </div>
  16. <script>
  17. Vue.config.productionTip = false
  18. new Vue({
  19. el:'#root',
  20. data:{
  21. firstName:'张',
  22. lastName:'三'
  23. },
  24. computed:{
  25. fullName:{
  26. get(){
  27. return this.firstName + '-' + this.lastName
  28. },
  29. set(value){
  30. const arr = value.split('-')
  31. this.firstName = arr[0]
  32. this.lastName = arr[1]
  33. }
  34. }
  35. }
  36. })
  37. </script>
  38. </body>
  39. </html>

效果:

总结:

计算属性:

        定义:要用的属性不存在,需要通过已有属性计算得来。

        原理:底层借助了Objcet.defineproperty()方法提供的getter和setter。

        get函数什么时候执行?

                初次读取时会执行一次
                当依赖的数据发生改变时会被再次调用
        优势:与methods实现相比,内部有缓存机制(复用),效率更高,调试方便

备注:

计算属性最终会出现在vm上,直接读取使用即可
如果计算属性要被修改,那必须写set函数去响应修改,且set中要引起计算时依赖的数据发生改变
如果计算属性确定不考虑修改,可以使用计算属性的简写形式

  1. new Vue({
  2. el:'#root',
  3. data:{
  4. firstName:'张',
  5. lastName:'三'
  6. },
  7. computed:{
  8. fullName(){
  9. return this.firstName + '-' + this.lastName
  10. }
  11. }
  12. })

2.监视属性

2.1 监视属性基本用法

  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="../js/vue.js"></script>
  9. </head>
  10. <body>
  11. <div id="root">
  12. <h2>今天天气好{{info}}!</h2>
  13. <button @click="changeWeather">点击切换天气</button>
  14. </div>
  15. <script>
  16. Vue.config.productionTip = false
  17. new Vue({
  18. el:'#root',
  19. data:{
  20. isHot:true,
  21. },
  22. computed:{
  23. info(){
  24. return this.isHot ? '炎热' : '凉爽'
  25. }
  26. },
  27. methods:{
  28. changeWeather(){
  29. this.isHot = !this.isHot
  30. }
  31. },
  32. watch:{
  33. isHot:{
  34. immediate:true, //初始化时让handler调用一下
  35. //handler什么时候调用?当isHot发生改变时
  36. handler(newValue,oldValue){
  37. console.log('isHot被修改了',newValue,oldValue)
  38. }
  39. }
  40. }
  41. })
  42. </script>
  43. </body>
  44. </html>

总结:

监视属性watch:

  1. 当被监视的属性变化时,回调函数自动调用
  2. 监视的属性必须存在,才能进行监视
  3. 监视有两种写法:
    1. 创建Vue时传入watch配置
    2. 通过vm.$watch监视
  1. vm.$watch('isHot',{
  2. immediate:true,
  3. handler(newValue,oldValue){
  4. console.log('isHot被修改了',newValue,oldValue)
  5. }
  6. })

 2.2 深度监视

  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="../js/vue.js"></script>
  9. </head>
  10. <body>
  11. <div id="root">
  12. <h3>a的值是:{{numbers.a}}</h3>
  13. <button @click="numbers.a++">点我让a+1</button>
  14. <h3>b的值是:{{numbers.b}}</h3>
  15. <button @click="numbers.b++">点我让b+1</button>
  16. </div>
  17. <script>
  18. Vue.config.productionTip = false
  19. new Vue({
  20. el:'#root',
  21. data:{
  22. isHot:true,
  23. numbers:{
  24. a:1,
  25. b:1,
  26. }
  27. },
  28. watch:{
  29. //监视多级结构中所有属性的变化
  30. numbers:{
  31. deep:true,
  32. handler(){
  33. console.log('numbers改变了')
  34. }
  35. }
  36. //监视多级结构中某个属性的变化
  37. /* 'numbers.a':{
  38. handler(){
  39. console.log('a被改变了')
  40. }
  41. } */
  42. }
  43. })
  44. </script>
  45. </body>
  46. </html>

总结:

深度监视:

  1. Vue中的watch默认不监测对象内部值的改变(一层)
  2. 在watch中配置deep:true可以监测对象内部值的改变(多层)

备注:

  1. Vue自身可以监测对象内部值的改变,但Vue提供的watch默认不可以
  2. 使用watch时根据监视数据的具体结构,决定是否采用深度监视

 2.3 监视属性简写

如果监视属性除了handler没有其他配置项的话,可以进行简写。

  1. <script type="text/javascript">
  2. Vue.config.productionTip = false //阻止 vue 在启动时生成生产提示。
  3. const vm = new Vue({
  4. el:'#root',
  5. data:{
  6. isHot:true,
  7. },
  8. computed:{
  9. info(){
  10. return this.isHot ? '炎热' : '凉爽'
  11. }
  12. },
  13. methods: {
  14. changeWeather(){
  15. this.isHot = !this.isHot
  16. }
  17. },
  18. watch:{
  19. //正常写法
  20. isHot:{
  21. handler(newValue,oldValue){
  22. console.log('isHot被修改了',newValue,oldValue)
  23. }
  24. },
  25. //简写
  26. isHot(newValue,oldValue){
  27. console.log('isHot被修改了',newValue,oldValue,this)
  28. }
  29. }
  30. })
  31. //正常写法
  32. vm.$watch('isHot',{
  33. handler(newValue,oldValue){
  34. console.log('isHot被修改了',newValue,oldValue)
  35. }
  36. })
  37. //简写
  38. vm.$watch('isHot',function(newValue,oldValue){
  39. console.log('isHot被修改了',newValue,oldValue,this)
  40. })
  41. </script>

2.4 监听属性VS计算属性

总结:

computed和watch之间的区别:

  1. computed能完成的功能,watch都可以完成
  2. watch能完成的功能,computed不一定能完成,例如:watch可以进行异步操作

两个重要的小原则:

  1. 所有被Vue管理的函数,最好写成普通函数,这样this的指向才是vm 或 组件实例对象
  2. 所有不被Vue所管理的函数(定时器的回调函数、ajax的回调函数等、Promise的回调函数),最好写成箭头函数,这样this的指向才是vm 或 组件实例对象。

3. 绑定样式

  1. <style>
  2. .basic{
  3. width: 400px;
  4. height: 100px;
  5. border: 1px solid black;
  6. }
  7. .happy{
  8. border: 4px solid red;;
  9. background-color: rgba(255, 255, 0, 0.644);
  10. background: linear-gradient(30deg,yellow,pink,orange,yellow);
  11. }
  12. .sad{
  13. border: 4px dashed rgb(2, 197, 2);
  14. background-color: gray;
  15. }
  16. .normal{
  17. background-color: skyblue;
  18. }
  19. .atguigu1{
  20. background-color: yellowgreen;
  21. }
  22. .atguigu2{
  23. font-size: 30px;
  24. text-shadow:2px 2px 10px red;
  25. }
  26. .atguigu3{
  27. border-radius: 20px;
  28. }
  29. </style>
  1. <div id="root">
  2. <!-- 绑定class样式--字符串写法,适用于:样式的类名不确定,需要动态指定 -->
  3. <div class="basic" :class="mood" @click="changeMood">{{name}}</div> <br/><br/>
  4. <!-- 绑定class样式--数组写法,适用于:要绑定的样式个数不确定、名字也不确定 -->
  5. <div class="basic" :class="classArr">{{name}}</div> <br/><br/>
  6. <!-- 绑定class样式--对象写法,适用于:要绑定的样式个数确定、名字也确定,但要动态决定用不用 -->
  7. <div class="basic" :class="classObj">{{name}}</div> <br/><br/>
  8. <!-- 绑定style样式--对象写法 -->
  9. <div class="basic" :style="styleObj">{{name}}</div> <br/><br/>
  10. <!-- 绑定style样式--数组写法 -->
  11. <div class="basic" :style="styleArr">{{name}}</div>
  12. </div>
  1. <script type="text/javascript">
  2. Vue.config.productionTip = false
  3. const vm = new Vue({
  4. el:'#root',
  5. data:{
  6. name:'尚硅谷',
  7. mood:'normal',
  8. classArr:['atguigu1','atguigu2','atguigu3'],
  9. classObj:{
  10. atguigu1:false,
  11. atguigu2:false,
  12. },
  13. styleObj:{
  14. fontSize: '40px',
  15. color:'red',
  16. },
  17. styleObj2:{
  18. backgroundColor:'orange'
  19. },
  20. styleArr:[
  21. {
  22. fontSize: '40px',
  23. color:'blue',
  24. },
  25. {
  26. backgroundColor:'gray'
  27. }
  28. ]
  29. },
  30. methods: {
  31. changeMood(){
  32. const arr = ['happy','sad','normal']
  33. const index = Math.floor(Math.random()*3)
  34. this.mood = arr[index]
  35. }
  36. },
  37. })
  38. </script>

总结:

class样式:

写法:class="xxx",xxx可以是字符串、对象、数组

  • 字符串写法适用于:类名不确定,要动态获取
  • 对象写法适用于:要绑定多个样式,个数不确定,名字也不确定
  • 数组写法适用于:要绑定多个样式,个数确定,名字也确定,但不确定用不用

style样式:

  • :style="{fontSize: xxx}"其中xxx是动态值
  • :style="[a,b]"其中a、b是样式对象


 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/185352
推荐阅读
相关标签
  

闽ICP备14008679号