Vue.js 3.x学习笔记

本文通过两个实例展示了Vue Composition API的基本用法,包括使用ref和reactive进行响应式数据操作,利用computed实现计算属性,以及使用watch监听数据变化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.setup()

<template>
  <div class="about">
    <h1>This is an about page</h1>
    <div>{{msg0}}</div>
    <div id="a" @click="changeMsg">{{ msg }}</div>
  </div>
</template>
<script>
import { reactive, toRefs, ref } from "@vue/composition-api";

export default {
  setup() {
    console.log('输出测试')
    const msg0=ref('加油')
    msg0.value='加油1'
    const msg = reactive({
      msg: 55,
    });
    const changeMsg = () => {
      msg.msg = 66;
    };

    return { ...toRefs(msg), changeMsg,msg0 };
  },
};
</script>
<style lang="less">
.about {
  #a {
    color: red;
  }
}
</style>

2.computed,watch

<div>doubleNum{{ doubleNum }}</div>
<div>doubleNum0{{doubleNum0}}</div>

import { reactive, toRefs, ref, computed,watch } from "@vue/composition-api";

export default {
  setup() {
    console.log("输出测试");
    const msg0 = ref("加油");
    msg0.value = "加油1";
    const state = reactive({
      msg: 1,
       doubleNum0:computed(()=>state.msg*3) 
    });
   
   
   const doubleNum =  computed(()=>{
      return state.msg * 2;
    })

    watch(()=>state.msg,(newValue,oldValue)=>{
      console.log(newValue+"/"+oldValue)
    })

    return { ...toRefs(state), , msg0, , doubleNum, };
  },
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值