美文网首页
vue得toRef,toRaw,toRefs

vue得toRef,toRaw,toRefs

作者: Birdd | 来源:发表于2022-07-06 08:49 被阅读0次

toRef

如果原始对象是非响应式的就不会更新视图 数据是会变的

toRefs

批量创建ref对象主要是方便我们解构使用

toRaw

将响应式对象转化为普通对象

<template>
  <div>
    oldValue:{{ oldValue }} newValue:{{ newValue }}
    <button @click="changetoRef()">改变changetoRef</button>
    oldValue:{{ oldRef }} --newValue:{{ lName }}{{ lage }} --toRowValue:{{toRowValue}}
    <button @click="changetoRefs()">改变changetoRefs</button>
    <button @click="changetoRow()">改变toRow</button>
  </div>
</template>
<script>
import {
  ref,
  shallowReactive,
  shallowRef,
  triggerRef,
  customRef,
  reactive,
  readonly,
  toRef,
  toRefs,
  toRaw,
} from "vue";
export default {
  setup() {
    //改变Reactive
    let oldValue = {
      name: "乌瑟尔",
      age: 18,
    };
    let newValue = toRef(oldValue, "age");
    function changetoRef() {
      newValue.value = 14;
      console.log(oldValue);
      console.log(newValue);
    }

    let oldRef = reactive({
      name: "罗大佑",
      age: 65,
    });
    let { name: lName, age: lage } = toRefs(oldRef);
    let changetoRefs = () => {
      oldRef.age = 14;
      console.log(oldRef);
      console.log(lName.value, lage.value);
    };
    let toRowValue = toRaw(oldRef);
    let changetoRow = () => {
      toRowValue.age = 49;
      toRowValue.name = "李宗盛";
      console.log(oldRef);
      console.log(toRowValue);
    };
    return {
      oldValue,
      newValue,
      changetoRef,
      changetoRefs,
      oldRef,
      lName,
      lage,
      changetoRow,
      toRowValue
    };
  },
};
</script>

相关文章

网友评论

      本文标题:vue得toRef,toRaw,toRefs

      本文链接:https://www.haomeiwen.com/subject/urnebrtx.html