{"id":8285,"date":"2022-11-03T10:03:21","date_gmt":"2022-11-03T02:03:21","guid":{"rendered":"http:\/\/123.57.164.21\/?p=8285"},"modified":"2022-11-03T10:03:21","modified_gmt":"2022-11-03T02:03:21","slug":"vue3%e4%b8%adref%e5%92%8creactive","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=8285","title":{"rendered":"Vue3\u4e2dref\u548creactive"},"content":{"rendered":"\n<p>Vue3 \u4e3a\u5f00\u53d1\u8005\u63d0\u4f9b <code>ref<\/code>\u548c <code>reactive<\/code>\u4e24\u4e2a API \u6765\u5b9e\u73b0\u54cd\u5e94\u5f0f\u6570\u636e\uff0c\u8fd9\u4e5f\u662f\u6211\u4eec\u4f7f\u7528 Vue3 \u5f00\u53d1\u9879\u76ee\u4e2d\u7ecf\u5e38\u7528\u5230\u7684\u4e24\u4e2a API\u3002<\/p>\n\n\n\n<p><strong>1. reactive API \u5982\u4f55\u4f7f\u7528\uff1f<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><code>reactive<\/code>\u65b9\u6cd5\u7528\u6765\u521b\u5efa\u54cd\u5e94\u5f0f\u5bf9\u8c61\uff0c\u5b83\u63a5\u6536\u4e00\u4e2a\u5bf9\u8c61\/\u6570\u7ec4\u53c2\u6570\uff0c\u8fd4\u56de\u5bf9\u8c61\u7684\u54cd\u5e94\u5f0f\u526f\u672c\uff0c\u5f53\u8be5\u5bf9\u8c61\u7684\u5c5e\u6027\u503c\u53d1\u751f\u53d8\u5316\uff0c\u4f1a\u81ea\u52a8\u66f4\u65b0\u4f7f\u7528\u8be5\u5bf9\u8c61\u7684\u5730\u65b9\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u4ee5\u5206\u522b\u4ee5<strong>\u5bf9\u8c61<\/strong>\u548c<strong>\u6570\u7ec4<\/strong>\u4f5c\u4e3a\u53c2\u6570\u6f14\u793a\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import { reactive } from 'vue'\n\nlet reactiveObj = reactive({ name : 'Chris1993' });\nlet setReactiveObj = () => {\n  reactiveObj.name = 'Hello Chris1993';\n}\n\nlet reactiveArr = reactive(['a', 'b', 'c', 'd']);\nlet setReactiveArr = () => {\n  reactiveArr[1] = 'Hello Chris1993';\n}<\/pre>\n\n\n\n<p>\u6a21\u7248\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;template>\n  &lt;h2>Vue3 reactive API Base&lt;\/h2>\n  &lt;div>\n    Object:{{reactiveObj.name}} \n    &lt;span @click=\"setReactiveObj\">Update&lt;\/span>\n  &lt;\/div>\n  &lt;div>\n    Array:{{reactiveArr}} \n    &lt;span @click=\"setReactiveArr\">Update&lt;\/span>\n  &lt;\/div>\n&lt;\/template><\/pre>\n\n\n\n<p>\u6b64\u65f6\u9875\u9762\u5c55\u793a\u5982\u4e0b\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-69-1024x256.png\" alt=\"\" class=\"wp-image-8286\" width=\"461\" height=\"115\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-69-1024x256.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-69-300x75.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-69-768x192.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-69-830x208.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-69-230x58.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-69-350x88.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-69-480x120.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-69.png 1176w\" sizes=\"(max-width: 461px) 100vw, 461px\" \/><\/figure><\/div>\n\n\n\n<p>\u5f53\u6211\u4eec\u5206\u522b\u70b9\u51fb <code>Update<\/code>\u6309\u94ae\u540e\uff0c\u53ef\u4ee5\u770b\u5230\u6570\u636e\u53d8\u5316\u540e\uff0c\u89c6\u56fe\u4e0a\u5185\u5bb9\u4e5f\u4e00\u8d77\u66f4\u65b0\u4e86<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-70-1024x226.png\" alt=\"\" class=\"wp-image-8287\" width=\"444\" height=\"98\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-70-1024x226.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-70-300x66.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-70-768x170.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-70-830x184.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-70-230x51.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-70-350x77.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-70-480x106.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-70.png 1176w\" sizes=\"(max-width: 444px) 100vw, 444px\" \/><\/figure><\/div>\n\n\n\n<p><strong>2. ref API \u5982\u4f55\u4f7f\u7528\uff1f<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><code>ref<\/code> \u7684\u4f5c\u7528\u5c31\u662f\u5c06\u4e00\u4e2a<strong>\u539f\u59cb\u6570\u636e\u7c7b\u578b<\/strong>\uff08primitive data type\uff09\u8f6c\u6362\u6210\u4e00\u4e2a\u5e26\u6709<strong>\u54cd\u5e94\u5f0f\u7279\u6027<\/strong>\u7684\u6570\u636e\u7c7b\u578b\uff0c\u539f\u59cb\u6570\u636e\u7c7b\u578b\u5171\u67097\u4e2a\uff0c\u5206\u522b\u662f\uff1a<code>String<\/code>\/ <code>Number<\/code> \/<code>BigInt<\/code> \/<code>Boolean<\/code> \/<code>Symbol<\/code> \/<code>Null<\/code> \/<code>Undefined<\/code>\u3002<\/p>\n\n\n\n<p><code>ref<\/code>\u7684\u503c\u5728 JS\/TS \u4e2d\u8bfb\u53d6\u548c\u4fee\u6539\u65f6\uff0c\u9700\u8981\u4f7f\u7528 <code>.value<\/code>\u83b7\u53d6\uff0c\u5728\u6a21\u7248\u4e2d\u8bfb\u53d6\u662f\uff0c\u4e0d\u9700\u8981\u4f7f\u7528 <code>.value<\/code>\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u4ee5\u5206\u522b\u4ee5<strong>\u5b57\u7b26\u4e32<\/strong>\u548c<strong>\u5bf9\u8c61<\/strong>\u4f5c\u4e3a\u53c2\u6570\u6f14\u793a\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import { ref } from 'vue'\n\nlet refValue = ref('Chris1993');\nlet setRefValue = () => {\n  refValue.value = 'Hello Chris1993';\n}\nlet refObj = ref({ name : 'Chris1993' });\nlet setRefObj = () => {\n  refObj.value.name = 'Hello Chris1993';\n}<\/pre>\n\n\n\n<p>\u6a21\u7248\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;template>\n  &lt;h2>Vue3 ref API Base&lt;\/h2>\n  &lt;div>\n    String:{{refValue}} \n    &lt;span @click=\"setRefValue\">Update&lt;\/span>\n  &lt;\/div>\n  &lt;div>\n    Object:{{refObj.name}}\n    &lt;span @click=\"setRefObj\">Update&lt;\/span>\n  &lt;\/div>\n&lt;\/template><\/pre>\n\n\n\n<p>\u6b64\u65f6\u9875\u9762\u5c55\u793a\u5982\u4e0b\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-71-1024x245.png\" alt=\"\" class=\"wp-image-8288\" width=\"469\" height=\"111\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-71-1024x245.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-71-300x72.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-71-768x184.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-71-830x199.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-71-230x55.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-71-350x84.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-71-480x115.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-71.png 1168w\" sizes=\"(max-width: 469px) 100vw, 469px\" \/><\/figure><\/div>\n\n\n\n<p>\u5f53\u6211\u4eec\u5206\u522b\u70b9\u51fb <code>Update<\/code>\u6309\u94ae\u540e\uff0c\u53ef\u4ee5\u770b\u5230\u6570\u636e\u53d8\u5316\u540e\uff0c\u89c6\u56fe\u5185\u5bb9\u4e5f\u4e00\u8d77\u66f4\u65b0\u4e86\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-72-1024x254.png\" alt=\"\" class=\"wp-image-8289\" width=\"483\" height=\"120\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-72-1024x254.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-72-300x74.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-72-768x190.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-72-830x206.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-72-230x57.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-72-350x87.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-72-480x119.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-72.png 1194w\" sizes=\"(max-width: 483px) 100vw, 483px\" \/><\/figure><\/div>\n\n\n\n<p><strong>3. reactive \u53ef\u4ee5\u7528\u5728\u6df1\u5c42\u5bf9\u8c61\u6216\u6570\u7ec4\u5417\uff1f<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u7b54\u6848\u662f<strong>\u53ef\u4ee5\u7684<\/strong>\uff0c<code>reactive<\/code>\u662f\u57fa\u4e8e <a rel=\"noreferrer noopener\" href=\"https:\/\/link.segmentfault.com\/?enc=2GLb%2BoG%2BrSjUjGhZ4Z3bbA%3D%3D.%2F6ZtqGwWlfIEJAEeTUxHuuGEjZDiLi%2BQMuaDCFD63VQcbWL88R5XbJX7ZOanYPWzzqAueDYD2euNKyYKIuI7stYuZEZ%2BGTxagu93BRQY9EpJbJ3DpNxTCIOgUWtyRPn7\" target=\"_blank\">ES2015 Proxy API<\/a> \u5b9e\u73b0\u7684\uff0c\u5b83\u7684\u54cd\u5e94\u5f0f\u662f\u6574\u4e2a\u5bf9\u8c61\u7684\u6240\u6709\u5d4c\u5957\u5c42\u7ea7\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u4ee5\u5206\u522b\u4ee5<strong>\u5bf9\u8c61<\/strong>\u548c<strong>\u6570\u7ec4<\/strong>\u4f5c\u4e3a\u53c2\u6570\u6f14\u793a\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import { reactive } from 'vue'\n\nlet reactiveDeepObj = reactive({\n  user: {name : 'Chris1993'}\n});\nlet setReactiveDeepObj = () => {\n  reactiveDeepObj.user.name = 'Hello Chris1993';\n}\n\nlet reactiveDeepArr = reactive(['a', ['a1', 'a2', 'a3'], 'c', 'd']);\nlet setReactiveDeepArr = () => {\n  reactiveDeepArr[1][1] = 'Hello Chris1993';\n}<\/pre>\n\n\n\n<p>\u6a21\u7248\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;template>\n  &lt;h2>Vue3 reactive deep API Base&lt;\/h2>\n  &lt;div>\n    Object:{{reactiveDeepObj.user.name}}\n    &lt;span @click=\"setReactiveDeepObj\">Update&lt;\/span>\n  &lt;\/div>\n  &lt;div>\n    Array:{{reactiveDeepArr}}\n    &lt;span @click=\"setReactiveDeepArr\">Update&lt;\/span>\n  &lt;\/div>\n&lt;\/template><\/pre>\n\n\n\n<p>\u6b64\u65f6\u9875\u9762\u5c55\u793a\u5982\u4e0b\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-73-1024x258.png\" alt=\"\" class=\"wp-image-8290\" width=\"443\" height=\"111\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-73-1024x258.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-73-300x76.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-73-768x194.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-73-830x209.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-73-230x58.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-73-350x88.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-73-480x121.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-73.png 1070w\" sizes=\"(max-width: 443px) 100vw, 443px\" \/><\/figure><\/div>\n\n\n\n<p>\u5f53\u6211\u4eec\u5206\u522b\u70b9\u51fb <code>Update<\/code>\u6309\u94ae\u540e\uff0c\u53ef\u4ee5\u770b\u5230\u6570\u636e\u53d8\u5316\u540e\uff0c\u89c6\u56fe\u4e0a\u5185\u5bb9\u4e5f\u4e00\u8d77\u66f4\u65b0\u4e86\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-74-1024x238.png\" alt=\"\" class=\"wp-image-8291\" width=\"483\" height=\"112\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-74-1024x238.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-74-300x70.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-74-768x178.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-74-830x193.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-74-230x53.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-74-350x81.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-74-480x111.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-74.png 1094w\" sizes=\"(max-width: 483px) 100vw, 483px\" \/><\/figure><\/div>\n\n\n\n<p><strong>4. reactive \u8fd4\u56de\u503c\u548c\u6e90\u5bf9\u8c61\u76f8\u7b49\u5417\uff1f<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u7b54\u6848\u662f<strong>\u4e0d\u76f8\u7b49\u7684<\/strong>\uff0c\u56e0\u4e3a<code>reactive<\/code>\u662f\u57fa\u4e8e <a rel=\"noreferrer noopener\" href=\"https:\/\/link.segmentfault.com\/?enc=Jgyf6XsaUpb8MOvVqBa%2Byg%3D%3D.0ovQ%2B10pRr2W07EOlRxEJXOTcKGpLNjWU2E0d7AmgGWyMzK2EZSDx6wE084XjCJRJFQWeca6ahgpe9CcNHdTH6lMSbrkggJ08dOK8mzmw94f%2B3E6CoYoLF%2Bf7FF%2Bmwyy\" target=\"_blank\">ES2015 Proxy API<\/a> \u5b9e\u73b0\u7684\uff0c\u8fd4\u56de\u7ed3\u679c\u662f\u4e2a proxy \u5bf9\u8c61\u3002<\/p>\n\n\n\n<p>\u6d4b\u8bd5\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">let reactiveSource = { name: 'Chris1993' };\nlet reactiveData = reactive(reactiveSource);\n\nconsole.log(reactiveSource === reactiveData);\n\/\/ false\n\nconsole.log(reactiveSource);\n\/\/ {name: 'Chris1993'}\n\nconsole.log(reactiveData);\n\/\/ Reactive&lt;{name: 'Chris1993'}><\/pre>\n\n\n\n<p><strong>5. TypeScript \u5982\u4f55\u5199 ref \u548c reactive \u53c2\u6570\u7c7b\u578b\uff1f<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u5728\u4f7f\u7528 TypeScript \u5199 ref \/ reactive \u53c2\u6570\u7c7b\u578b\u65f6\uff0c\u53ef\u4ee5\u6839\u636e ref \/ reactive \u63a5\u53e3\u7c7b\u578b\u6765\u5b9e\u73b0\u5177\u4f53\u7684\u7c7b\u578b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function ref&lt;T>(value: T): Ref&lt;T>\n\nfunction reactive&lt;T extends object>(target: T): UnwrapNestedRefs&lt;T><\/pre>\n\n\n\n<p>\u5c06\u524d\u9762\u5b9e\u4f8b\u4ee3\u7801\u6539\u9020\u4e00\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import { ref } from 'vue'\n\nlet refValue = ref&lt;string>('Chris1993');\n\/\/ refValue \u7c7b\u578b\u4e3a\uff1a Ref&lt;string>\nlet setRefValue = () => {\n  refValue.value = 'Hello Chris1993'; \/\/ ok!\n  refValue.value = 1993; \/\/ error!\n}\n\n\/\/ reactive\u4e5f\u7c7b\u4f3c\nlet reactiveValue = reactive&lt;{name: string}>({name: 'Chris1993'});<\/pre>\n\n\n\n<p><strong>6. \u628a ref \u503c\u4f5c\u4e3a reactive \u53c2\u6570\u4f1a\u600e\u4e48\u6837\uff1f<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u5f53\u6211\u4eec\u5df2\u6709\u4e00\u4e2a <code>ref<\/code>\u5bf9\u8c61\uff0c\u9700\u8981\u4f7f\u7528\u5728 <code>reactive<\/code>\u5bf9\u8c61\u4e2d\uff0c\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462\uff1f<\/p>\n\n\n\n<p>\u5047\u8bbe\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">let name = ref('Chris1993');\nlet nameReactive = reactive({name})<\/pre>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u505a\u4e0b\u5217\u64cd\u4f5c\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">let name = ref('Chris1993');\nlet nameReactive = reactive({name})\nconsole.log(name.value === nameReactive.name); \/\/ true\n\nname.value = 'Hello Chris1993';\nconsole.log(name.value);        \/\/ Hello Chris1993\nconsole.log(nameReactive.name); \/\/ Hello Chris1993\n\nnameReactive.name = 'Hi Chris1993';\nconsole.log(name.value);        \/\/ Hi Chris1993\nconsole.log(nameReactive.name); \/\/ Hi Chris1993<\/pre>\n\n\n\n<p>\u8fd9\u662f\u56e0\u4e3a <code>reactive<\/code>\u5c06\u4f1a\u5bf9\u6240\u6709\u6df1\u5c42\u7684 <code>refs<\/code>\u8fdb\u884c\u89e3\u5305\uff0c\u5e76\u4e14\u4fdd\u6301 <code>ref<\/code>\u7684\u54cd\u5e94\u5f0f\u3002<\/p>\n\n\n\n<p>\u5f53\u901a\u8fc7\u8d4b\u503c\u65b9\u5f0f\u5c06 <code>ref<\/code>\u5206\u914d\u7ed9 <code>reactive<\/code>\u5c5e\u6027\u65f6\uff0c<code>ref<\/code>\u4e5f\u4f1a\u81ea\u52a8\u88ab\u89e3\u5305\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">let name = ref('Chris1993');\nlet nameReactive = reactive({})\nnameReactive.name = name;\n\nconsole.log(name.value);        \/\/ Chris1993\nconsole.log(nameReactive.name); \/\/ Chris1993\nconsole.log(name.value === nameReactive.name); \/\/ true<\/pre>\n\n\n\n<p><strong>7. \u603b\u7ed3<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u7b80\u5355\u603b\u7ed3\u4e00\u4e0b\uff1a<\/p>\n\n\n\n<ul><li><code>reactive<\/code> \u4e00\u822c\u7528\u4e8e\u5bf9\u8c61\/\u6570\u7ec4\u7c7b\u578b\u7684\u6570\u636e\uff0c\u90fd\u4e0d\u9700\u8981\u4f7f\u7528 <code>.value<\/code>\uff1b<\/li><li><code>ref<\/code>\u4e00\u822c\u7528\u4e8e\u57fa\u7840\u6570\u636e\u7c7b\u578b\u7684\u6570\u636e\uff0c\u5728 JS \u4e2d\u8bfb\u53d6\u548c\u4fee\u6539\u65f6\uff0c\u9700\u8981\u4f7f\u7528 <code>.value<\/code>\uff0c\u5728\u6a21\u7248\u4e2d\u4f7f\u7528\u65f6\u5219\u4e0d\u9700\u8981\uff1b<\/li><li><code>reactive<\/code> \u53ef\u4ee5\u4fee\u6539\u6df1\u5c42\u5c5e\u6027\u503c\uff0c\u5e76\u4fdd\u6301\u54cd\u5e94\uff1b<\/li><li><code>reactive<\/code> \u8fd4\u56de\u503c\u548c\u6e90\u5bf9\u8c61\u4e0d\u540c\uff1b<\/li><li><code>reactive<\/code>\u7684\u5c5e\u6027\u503c\u53ef\u4ee5\u662f <code>ref<\/code>\u503c\uff1b<\/li><li><code>ref<\/code> \u672c\u8d28\u4e5f\u662f <code>reactive<\/code>\uff0c<code>ref(obj)<\/code>\u7b49\u4ef7\u4e8e <code>reactive({value: obj})<\/code>\u3002<\/li><\/ul>\n\n\n\n<p><strong>8.\u9644\u5f55\u5bf9\u6bd4<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-75-1024x473.png\" alt=\"\" class=\"wp-image-8292\" width=\"551\" height=\"255\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-75-1024x473.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-75-300x139.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-75-768x355.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-75-830x383.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-75-230x106.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-75-350x162.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-75-480x222.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-75.png 1256w\" sizes=\"(max-width: 551px) 100vw, 551px\" \/><\/figure><\/div>\n\n\n\n<p>\u5982\u4e0a\u4ee3\u7801\uff0c\u6211\u4eec\u4f7f\u7528\u53d8\u91cf\u58f0\u660e\u7684\u65b9\u5f0f\uff0cref\u7684\u65b9\u5f0f\uff0creactive\u7684\u65b9\u5f0f\u5b9a\u4e49\u7684\u4e09\u4e2a\u53d8\u91cf\uff0cnum1,num2,num3<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-76-1024x294.png\" alt=\"\" class=\"wp-image-8293\" width=\"571\" height=\"164\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-76-1024x294.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-76-300x86.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-76-768x221.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-76-830x239.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-76-230x66.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-76-350x101.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-76-480x138.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-76.png 1280w\" sizes=\"(max-width: 571px) 100vw, 571px\" \/><\/figure><\/div>\n\n\n\n<p>\u6211\u4eec\u53d1\u73b0\u4f7f\u7528ref\u5b9a\u4e49\u7684\u6570\u636e\uff0c\u6253\u5370\u7ed3\u679c\u662f\u4e00\u4e2a\u88ab\u5bf9\u8c61\u5305\u88f9\u7684\u54cd\u5e94\u7684\u6570\u636e\uff0c\u4f7f\u7528reactive\u7684\u65b9\u5f0f\u548c\u7eaf\u53d8\u91cf\u58f0\u660e\u7684\u65b9\u5f0f\u6253\u5370\u7ed3\u679c\u662f\u4e00\u6837\u7684\uff0c\u8fd9\u662f\u4ec0\u4e48\u539f\u56e0\u5462\uff1f<\/p>\n\n\n\n<p>\u6211\u4eec\u53d1\u73b0\u5728\u63a7\u5236\u53f0\u8f93\u51fa\u4e00\u4e2a\u8b66\u544a\u4fe1\u606f\uff0c \u63d0\u793a100\u8fd9\u4e2a\u503c\u4e0d\u80fd\u88abreactive\u521b\u5efa\uff0c\u5b98\u65b9\u4e5f\u63a8\u8350\u6211\u4eec\u5728\u5b9a\u4e49\u6570\u636e\u7684\u65f6\u5019\uff0creactive\u5b9a\u4e49\u590d\u6742\u7684\u6570\u636e\u7c7b\u578b\u7684\u6570\u636e\uff0cref\u63a8\u8350\u5b9a\u4e49\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u6240\u4ee5\u5982\u679c\u8981\u4f7f\u7528reactive\u5b9a\u4e49\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u8bdd\uff0c\u6211\u4eec\u9700\u8981\u5728reactive\u4e2d\u5c06\u6570\u636e\u5305\u88c5\u4e00\u4e0b<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-77-1024x151.png\" alt=\"\" class=\"wp-image-8294\" width=\"561\" height=\"82\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-77-1024x151.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-77-300x44.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-77-768x113.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-77-830x123.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-77-230x34.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-77-350x52.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-77-480x71.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-77.png 1138w\" sizes=\"(max-width: 561px) 100vw, 561px\" \/><\/figure><\/div>\n\n\n\n<p>\u6211\u4eec\u5728\u4f7f\u7528reactive\u5b9a\u4e49\u6570\u636e\u7684\u65f6\u5019\u7528\u5bf9\u8c61\u505a\u4e00\u5c42\u5305\u88f9\uff0c\u8fd9\u6837\u63a7\u5236\u53f0\u5c31\u4e0d\u4f1a\u62a5\u8b66\u544a\u4fe1\u606f\u4e86\uff0c<\/p>\n\n\n\n<p>\u4f46\u662f\u4f7f\u7528reactive\u5b9a\u4e49\u7684\u6570\u636e\u548cref\u5b9a\u4e49\u7684\u6570\u636e\u6253\u5370\u7ed3\u679c\u6709\u4e00\u4e9b\u5dee\u5f02<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-78-1024x449.png\" alt=\"\" class=\"wp-image-8295\" width=\"572\" height=\"251\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-78-1024x449.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-78-300x132.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-78-768x337.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-78-830x364.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-78-230x101.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-78-350x154.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-78-480x211.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-78.png 1354w\" sizes=\"(max-width: 572px) 100vw, 572px\" \/><\/figure><\/div>\n\n\n\n<p>\u6211\u4eec\u53d1\u73b0ref\u5b9a\u4e49\u7684\u6570\u636e\u6253\u5370\u7ed3\u679c\u9700\u8981.value\u624d\u80fd\u83b7\u53d6\u5230\u7ed3\u679c\uff0c\u800creactive\u5219\u4e0d\u9700\u8981<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-79-1024x133.png\" alt=\"\" class=\"wp-image-8296\" width=\"603\" height=\"78\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-79-1024x133.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-79-300x39.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-79-768x100.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-79-830x108.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-79-230x30.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-79-350x46.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-79-480x63.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-79.png 1304w\" sizes=\"(max-width: 603px) 100vw, 603px\" \/><\/figure><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Vue3 \u4e3a\u5f00\u53d1\u8005\u63d0\u4f9b ref\u548c reactive\u4e24\u4e2a API \u6765\u5b9e\u73b0\u54cd\u5e94\u5f0f\u6570\u636e\uff0c\u8fd9\u4e5f\u662f\u6211\u4eec\u4f7f\u7528 Vue3 \u5f00 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/8285"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8285"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/8285\/revisions"}],"predecessor-version":[{"id":8297,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/8285\/revisions\/8297"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8285"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}