vue3基础

http://huaxhe.gitee.io/vue3_study_docs/00_%E8%AF%BE%E7%A8%8B%E4%BB%8B%E7%BB%8D.html

https://vuepress.vuejs.org/zh/

计算属性get跟set

https://lurongtao.gitee.io/felixbooks-gp19-node.js/basics/01-Node.js%E5%9F%BA%E7%A1%80.html

setup

vue3所有的API组合都在这个函数中执行。

<template>
  <div>
    <div>{
   {
   aiqi}}</div>
    <button @click="aiqiF">+1</button>

    <div>{
   {
   obj.name}}</div>
    <div>{
   {
   obj.id}}</div>
    <button @click="change(1)">111111</button>
    <router-view></router-view>
  </div>
</template>

<script lang="ts">
import {
    reactive, ref } from "vue";
export default {
   
  created(){
   
    console.log(this);
  },
  data(){
   
    return{
   
      ai:11111
    }
  },
  setup() {
   
    const aiqi = ref(123);
    const obj = reactive({
   
      name: "aiqi",
      id: 1,
    });
    function aiqiF() {
   
      aiqi.value = aiqi.value + 1;
    }
    console.log(obj);
    const change = (num:number) => {
   
      obj.name = 'zengyongfeng';
      obj.id = 24;
      console.log(num);
      
      return 1;
    };
    // function change():number{
   

    // }
    return {
   
      aiqi,
      obj,
      aiqiF,
      change,
    };
  },
};
</script>
<style>
</style>

reactive和ref

1.这两个都是让数据变成响应式数据
2.ref是通过给value添加getters和setters来实现对数据的劫持
3.reactive是通过proxy代理对象实现对对象数据劫持,通过Reflect操作对象属性的
4.ref如果传入为对象或数组数据会转换为proxy代理对象,
5.在js或ts中改变ref的值要通过value,而模板中则不需要

<template>
  <div>
    <div>{
   {
   aiqi}}</div>
    <button @click="aiqiF">+1</button>

    <div>{
   {
   obj.name}}</div>
    <div>{
   {
   obj.id}}</div>
    <button @click="change(1)">111111</button>
    <router-view></router-view>
  </div>
</template>

<script lang="ts">
import {
    reactive, ref } from "vue";
export default {
   
  created(){
   
    console.log(this);
  },
  data(){
   
    return{
   
      ai:11111
    }
  },
  setup() {
   
    const aiqi = ref(123);
    const obj = reactive({
   
      name: "aiqi",
      id: 1,
    });
    function aiqiF() {
   
      aiqi.value = aiqi.value + 1;
    }
    console.log(obj);
    const change = (num:number) => {
   
      obj.name = 'zengyongfeng';
      obj.id = 24;
      console.log(num);
      
      return 1;
    };
    // function change():number{
   

    // }
    return {
   
      aiqi,
      obj,
      aiqiF,
      change,
    };
  },
};
</script>
<style>
</style>

computed、watch、watchEffect

1.computed

和vue一样

computed的set,是当计算属性值改变,才会触发

<template>
  <div>
    <div>{
   {
   aiqi}}</div>
    <div>{
   {
   ac}}</div>

    <button @click="caozuoac">caozuoac</button>
  </div>
</template>

<script lang="ts">
import {
    computed, reactive } from 'vue'
export default {
   
  setup(){
   
    const aiqi1 = re
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值