vue专栏 05.vue高阶API 04.watch新用法 02.引用数据类型

# 02.引用类型

### 侦听单个属性
```js
const app = Vue.createApp({
    setup() {
        const {reactive, watch, toRefs} = Vue
        let eduObj = reactive({brand: 'Love', site: 'baidu.com'})

        // 侦听器 侦听一个属性 用函数引入
        watch(()=>eduObj.brand, (currentValue, preValue)=>{
            // 当前值
            console.log(currentValue);
            // 先前值
            console.log(preValue);
        })

        return {
            brand, site
        }
    },
    template: `#idea`
}).mount('#app');
```
```html
<template id="idea">
    <div>品牌:<label><input type="text" v-model="brand"></label></div>
    <p>品牌:{{brand}}</p>
</template>
```
![03](imgs/03.png)


### 侦听多个属性
```js
const app = Vue.createApp({
    setup() {
        const {reactive, watch, toRefs} = Vue
        let eduObj = reactive({brand: 'Love', site: 'baidu.com'})
        
        // 取出后便于侦听多个
        const {brand, site} = toRefs(eduObj)

        // 侦听器 侦听多个属性 
        watch([brand, site], ([currentBrand, currentSite], [preBrand, preSite])=>{
            // 当前值
            console.log(currentBrand, currentSite);
            // 先前值
            console.log(preBrand, preSite);
        }, {
                // 是否深度监视
                // 默认只监视对象的值,深度会监视对象的属性值
                deep: false,
        })

        return {
            brand, site
        }
    },

    template: `#idea`
}).mount('#app');
```
```html
<template id="idea">
    <div>品牌:<label><input type="text" v-model="brand"></label></div>
    <p>品牌:{{brand}}</p>

    <div>网址:<label><input type="text" v-model="site"></label></div>
    <p>网址:{{site}}</p>
</template>
```
![04](imgs/04.png)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值