Vue(4)

监视属性

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>天气案例</title>
    <script  type="text/javascript" src="/02_Vue模板语法/vue.js"></script>
</head>
<body>
    <div id="root">
    <h2>今天天气很{{info}}</h2>
    <button @click="changeweather ">切换天气</button>
    </div>
   <script type="text/javascript">
   const vm= new Vue({
     el:'#root',
     data:{
         isHot:true,
         x:1,
         window
     },
     computed:{
         info()
         {
             return this.isHot ? '炎热' : '凉爽'
         }
     },
     methods: {
         changeweather(){
            this.isHot=!this.isHot 
         }
     },
    watch:{
       isHot:{
           immediate:true,//初始化时让handler调用一下
           //handler什么时候调用 当isHot发生改变时
           handler(newValue,oldValue){
        console.log("isHot被修改了",newValue,oldValue)
           }
       }
    }
    })
   </script>
</body>
</html>

在这里插入图片描述

深度监视

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

    备注:
         (1).Vue自身可以监测对象内部值的改变,但Vue提供的watch默认不可以!
         (2).使用watch时根据数据的具体结构,决定是否采用深度监视。
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>天气案例</title>
    <script  type="text/javascript" src="/02_Vue模板语法/vue.js"></script>
</head>
<body>
    <!--
       深度监视:
              (1).Vue中的watch默认不监测对象内部值的改变(一层)
              (2).配置deep:true可以监测对象内部值改变(多层).
        备注:
             (1).Vue自身可以监测对象内部值的改变,但Vue提供的watch默认不可以!
             (2).使用watch时根据数据的具体结构,决定是否采用深度监视。
    -->
    <div id="root">
    <h2>今天天气很{{info}}</h2>
    <button @click="changeweather ">切换天气</button>
    <hr/>
    <h3>a的值是:{{numbers.a}}</h3>
    <button @click="numbers.a++">点我让a+1</button>
    <button @click="numbers.b++">点我b+1</button>
    <h3>b的值是:{{numbers.b}}</h3>
    <button @click="numbers={a:666,b:888}">彻底替换掉numbers</button>
       {{numbers.c.d.e}} 
</div>
   <script type="text/javascript">
   const vm= new Vue({
     el:'#root',
     data:{
         isHot:true,
         numbers:{
             a:1,
             b:1,
             c:{
                 d:{
                     e:100
                 }
             }
         }
     },
     computed:{
         info()
         {
             return this.isHot ? '炎热' : '凉爽'
         }
     },
     methods: {
         changeweather(){
            this.isHot=!this.isHot 
         }
     },
    watch:{
       isHot:{
           immediate:true,//初始化时让handler调用一下
           //handler什么时候调用 当isHot发生改变时
           handler(newValue,oldValue){
        console.log("isHot被修改了",newValue,oldValue)
           }
       }
       //监视多级结构中某个属性的变化
    //    ,'numbers.a':{
    //      handler(){
    //          console.log('a被改变了')
    //      }
    //   },
   , numbers:{
       deep:true,
      handler(){
          console.log('numbers改变了')
      }
    }

    },
    })
    // vm.$watch('isHot',{
    //     immediate:true,
    //     handler(newValue,oldValue){
    //         console.log('isHot被修改了',newValue,oldValue)
    //     }
    // })
   </script>
</body>
</html>

在这里插入图片描述

watch VS computed

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

  两个重要的小原则:
          1.所被Vue管理的函数,最好写成普通函数,这样this的指向才是vm或组件实例对象。
          2.所有不被Vue所管理的函数(定时器的回调函数,ajax的回调函数等),最好写成箭头函数,这样this的指向才是vm或组件实例对象。例如计时器等。

绑定样式

绑定class样式

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
     .basic{
				width: 400px;
				height: 100px;
				border: 1px solid black;
			}
			.happy{
				border: 4px solid red;;
				background-color: rgba(255, 255, 0, 0.644);
				background: linear-gradient(30deg,yellow,pink,orange,yellow);
			}
			.sad{
				border: 4px dashed rgb(2, 197, 2);
				background-color: gray;
			}
			.normal{
				background-color: skyblue;
			}
			.atguigu1{
				background-color: yellowgreen;
			}
			.atguigu2{
				font-size: 30px;
				text-shadow:2px 2px 10px red;
			}
			.atguigu3{
				border-radius: 20px;
			}
    </style>
    <script type="text/javascript" src="/02_Vue模板语法/vue.js"></script>
</head>
<body>
    <div id="root">
        <!--绑定class样式--数组写法,适用于:样式的类名不确定,需要动态指定-->
   <div class="basic "  :class="arr" @click="changeMood">{{name}}</div>
   
   <!--绑定class样式--数组写法,适用于:要绑定的样式个数不确定,名字也确定,但要动态决定用不用-->
   <div class="basic " :class="classObj">{{name}}</div>
    </div> 
</body>
<script type="text/javascript">
    new Vue({
       el:'#root',
       data:{
           name:'尚硅谷',
           a:"normal",
           arr:['atguigu1','atguigu2','atguigu3'],
           classObj:{
               atguigu1:false,
               atguigu2:false
           }
       },
       methods:{
           changeMood(){
            //    document.getElementById('demo').className='basic happy'
        //  this.a='happy'
        const arr=['happy','sad','normal']
           const index= Math.floor(Math.random()*3)
           this.a=arr[index]
           }
       }
    })
</script>
</html>

在这里插入图片描述

条件渲染

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style></style>
    <script type="text/javascript" src="/02_Vue模板语法/vue.js"></script>
</head>
<body>
    <div id="root">
        <h2>当前的n值是{{n}}</h2>
        <button @click="n++">点我加n</button>
        <!--使用v-show做条件渲染-->
        <h1  v-show="true">人外有人{{name}}</h1>
        <h1  v-show="1==1">人外有人{{name}}</h1>

        <div v-show="n===1">Angular</div>
        <div v-show="n===2">React</div>
        <div v-show="n===3">Vue</div>
    </div>
</body>
<script type="text/javascript">
    new Vue({
        el:'#root',
        data:{
            name:'山外有山',
            a:false,
            n:1
        }
    })
</script>

</html>

在这里插入图片描述
代码学自尚硅谷。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值