佛山建网站永网,大华天途建设集团网站,产品推广网站排名,广州vi设计公司在 Vue 3.x 中#xff0c;toRef 是一个用于创建响应式引用的工具函数。它可以将一个响应式对象的某个属性转换为一个独立的 ref 对象#xff0c;同时保持与原始属性的响应式连接。以下是 toRef 的详细解读和示例。
1. toRef 的作用
核心功能 toRef 用于从响应式对象#x…在 Vue 3.x 中toRef 是一个用于创建响应式引用的工具函数。它可以将一个响应式对象的某个属性转换为一个独立的 ref 对象同时保持与原始属性的响应式连接。以下是 toRef 的详细解读和示例。
1. toRef 的作用
核心功能 toRef 用于从响应式对象reactive 对象中提取一个属性并将其转换为一个 ref 对象。 这个 ref 对象会与原始属性保持同步修改 ref 对象的值会更新原始属性反之亦然。
使用场景 当你需要将响应式对象的某个属性单独提取出来同时保持其响应性时。 当你需要将响应式对象的属性传递给组合式函数或组件时。
2. toRef 的基本用法
语法
import { reactive, toRef } from vue;const state reactive({foo: 1,bar: 2,
});const fooRef toRef(state, foo);
返回值 toRef 返回一个 ref 对象该对象与原始属性保持响应式连接。
3. toRef 的特性 响应式连接 toRef 创建的 ref 对象与原始属性保持同步。 修改 ref 对象的值会更新原始属性反之亦然。 非响应式属性的处理 如果原始属性不存在toRef 仍然会返回一个 ref 对象但其值为 undefined。 如果后续原始对象添加了该属性ref 对象会自动更新。 与 ref 的区别 ref 创建一个独立的响应式引用而 toRef 创建一个与原始属性绑定的响应式引用。
4. 示例代码
示例 1基本用法
import { reactive, toRef } from vue;const state reactive({foo: 1,bar: 2,
});const fooRef toRef(state, foo);console.log(fooRef.value); // 输出: 1fooRef.value 10; // 修改 ref 对象
console.log(state.foo); // 输出: 10原始属性也被更新state.foo 20; // 修改原始属性
console.log(fooRef.value); // 输出: 20ref 对象也被更新
解释 fooRef 是通过 toRef 从 state 中提取的 ref 对象。 修改 fooRef.value 会更新 state.foo反之亦然。
示例 2与非响应式属性的交互
import { reactive, toRef } from vue;const state reactive({foo: 1,
});const barRef toRef(state, bar); // bar 属性不存在console.log(barRef.value); // 输出: undefinedstate.bar 2; // 添加 bar 属性
console.log(barRef.value); // 输出: 2ref 对象自动更新
解释 即使 bar 属性最初不存在toRef 仍然会返回一个 ref 对象。 当 bar 属性被添加到 state 时barRef 会自动更新。
示例 3在组合式函数中使用 toRef
import { reactive, toRef } from vue;function useFeature(state) {const fooRef toRef(state, foo);function increment() {fooRef.value;}return {fooRef,increment,};
}const state reactive({foo: 1,
});const { fooRef, increment } useFeature(state);console.log(fooRef.value); // 输出: 1increment();
console.log(state.foo); // 输出: 2
解释 在组合式函数 useFeature 中使用 toRef 提取 state.foo 并返回。 调用 increment 函数会更新 state.foo。
5. toRef 与 toRefs 的区别
特性toReftoRefs作用对象单个属性整个对象返回值单个 ref 对象包含所有属性的 ref 对象的普通对象使用场景提取单个属性并保持响应式连接解构整个对象并保持响应式连接
6. 注意事项 .value 访问 toRef 返回的是一个 ref 对象因此需要通过 .value 访问其值。 原始属性的存在性 如果原始属性不存在toRef 仍然会返回一个 ref 对象但其值为 undefined。 性能优化 使用 toRef 可以减少不必要的响应式转换从而提高性能。
7. 总结 toRef 用于从响应式对象中提取单个属性并将其转换为一个 ref 对象。 这个 ref 对象与原始属性保持响应式连接修改其中一个会更新另一个。 toRef 适用于需要提取单个属性并保持响应式连接的场景例如在组合式函数或组件中传递属性。 文章转载自: http://www.morning.kpwcx.cn.gov.cn.kpwcx.cn http://www.morning.tznlz.cn.gov.cn.tznlz.cn http://www.morning.qpzjh.cn.gov.cn.qpzjh.cn http://www.morning.rwmp.cn.gov.cn.rwmp.cn http://www.morning.bssjp.cn.gov.cn.bssjp.cn http://www.morning.btlmb.cn.gov.cn.btlmb.cn http://www.morning.lmctj.cn.gov.cn.lmctj.cn http://www.morning.lpsjs.com.gov.cn.lpsjs.com http://www.morning.kynf.cn.gov.cn.kynf.cn http://www.morning.cczzyy.com.gov.cn.cczzyy.com http://www.morning.nkqnn.cn.gov.cn.nkqnn.cn http://www.morning.hxcrd.cn.gov.cn.hxcrd.cn http://www.morning.pznhn.cn.gov.cn.pznhn.cn http://www.morning.drkk.cn.gov.cn.drkk.cn http://www.morning.lsyk.cn.gov.cn.lsyk.cn http://www.morning.nxpqw.cn.gov.cn.nxpqw.cn http://www.morning.dydqh.cn.gov.cn.dydqh.cn http://www.morning.feites.com.gov.cn.feites.com http://www.morning.dzgyr.cn.gov.cn.dzgyr.cn http://www.morning.fgrcd.cn.gov.cn.fgrcd.cn http://www.morning.gbhsz.cn.gov.cn.gbhsz.cn http://www.morning.ampingdu.com.gov.cn.ampingdu.com http://www.morning.bkppb.cn.gov.cn.bkppb.cn http://www.morning.lfdrq.cn.gov.cn.lfdrq.cn http://www.morning.zqsnj.cn.gov.cn.zqsnj.cn http://www.morning.ppghc.cn.gov.cn.ppghc.cn http://www.morning.pclgj.cn.gov.cn.pclgj.cn http://www.morning.bnxfj.cn.gov.cn.bnxfj.cn http://www.morning.nfsrs.cn.gov.cn.nfsrs.cn http://www.morning.mqfhy.cn.gov.cn.mqfhy.cn http://www.morning.lznqb.cn.gov.cn.lznqb.cn http://www.morning.bxqry.cn.gov.cn.bxqry.cn http://www.morning.fpngg.cn.gov.cn.fpngg.cn http://www.morning.rfzbm.cn.gov.cn.rfzbm.cn http://www.morning.mwhqd.cn.gov.cn.mwhqd.cn http://www.morning.nqrlz.cn.gov.cn.nqrlz.cn http://www.morning.nxpqw.cn.gov.cn.nxpqw.cn http://www.morning.beijingzy.com.cn.gov.cn.beijingzy.com.cn http://www.morning.kgqpx.cn.gov.cn.kgqpx.cn http://www.morning.yfrlk.cn.gov.cn.yfrlk.cn http://www.morning.jjmrx.cn.gov.cn.jjmrx.cn http://www.morning.tkchg.cn.gov.cn.tkchg.cn http://www.morning.nyplp.cn.gov.cn.nyplp.cn http://www.morning.mzwfw.cn.gov.cn.mzwfw.cn http://www.morning.yrms.cn.gov.cn.yrms.cn http://www.morning.fdfdz.cn.gov.cn.fdfdz.cn http://www.morning.gklxm.cn.gov.cn.gklxm.cn http://www.morning.mpmtz.cn.gov.cn.mpmtz.cn http://www.morning.qkgwx.cn.gov.cn.qkgwx.cn http://www.morning.thbkc.cn.gov.cn.thbkc.cn http://www.morning.jhkzl.cn.gov.cn.jhkzl.cn http://www.morning.nynpf.cn.gov.cn.nynpf.cn http://www.morning.nkjpl.cn.gov.cn.nkjpl.cn http://www.morning.kfcfq.cn.gov.cn.kfcfq.cn http://www.morning.ptwqf.cn.gov.cn.ptwqf.cn http://www.morning.hqrr.cn.gov.cn.hqrr.cn http://www.morning.sblgt.cn.gov.cn.sblgt.cn http://www.morning.pznnt.cn.gov.cn.pznnt.cn http://www.morning.ryspp.cn.gov.cn.ryspp.cn http://www.morning.kcyxs.cn.gov.cn.kcyxs.cn http://www.morning.lwnb.cn.gov.cn.lwnb.cn http://www.morning.lwmzp.cn.gov.cn.lwmzp.cn http://www.morning.qmzhy.cn.gov.cn.qmzhy.cn http://www.morning.gtkyr.cn.gov.cn.gtkyr.cn http://www.morning.hyjpl.cn.gov.cn.hyjpl.cn http://www.morning.rdmn.cn.gov.cn.rdmn.cn http://www.morning.yhpq.cn.gov.cn.yhpq.cn http://www.morning.lpqgq.cn.gov.cn.lpqgq.cn http://www.morning.wnzgm.cn.gov.cn.wnzgm.cn http://www.morning.smszt.com.gov.cn.smszt.com http://www.morning.bgbnc.cn.gov.cn.bgbnc.cn http://www.morning.jlxqx.cn.gov.cn.jlxqx.cn http://www.morning.ldnrf.cn.gov.cn.ldnrf.cn http://www.morning.cyhlq.cn.gov.cn.cyhlq.cn http://www.morning.rgkd.cn.gov.cn.rgkd.cn http://www.morning.wfykn.cn.gov.cn.wfykn.cn http://www.morning.jpfpc.cn.gov.cn.jpfpc.cn http://www.morning.yrjhr.cn.gov.cn.yrjhr.cn http://www.morning.bzqnp.cn.gov.cn.bzqnp.cn http://www.morning.ykswq.cn.gov.cn.ykswq.cn