// 2. Watch for changes (reactive) watch(count, (newVal) => { console.log(`Count changed to: ${newVal}`); });
// Function to increment the count const increment = () => { count.value++; // Mutating the value };
// Message to demonstrate tracking let message = "Initial Message";
// Function to change the message const changeMessage = () => { message = "Message Changed!"; // Change message, not tracked by ref console.log(`Message changed to: ${message}`); }; </script>
<style> button { margin-right: 10px; } </style>
Note
1.The count is created as a ref, and you can modify its value using count.value++. This shows that .value is mutable.
2.Changing message directly doesn’t trigger reactivity because it isn’t a ref. This highlights that only ref objects are tracked reactively.
Regardless of whether the value of the primitive data type or the value of the reference data type is passed to the ref function, the returned object is constructed by the RefImpl class, but the difference is the value in the object:
如果 ref 函数参数传递的是原始数据类型的值,那么 value 是一个原始值 如果 ref 函数参数传递的是引用数据类型的值,那么 value 是一个 Proxy 对象