{"id":5975,"date":"2024-04-17T23:24:50","date_gmt":"2024-04-17T15:24:50","guid":{"rendered":"http:\/\/zyg.luchers.cn\/?p=5975"},"modified":"2024-04-17T23:24:50","modified_gmt":"2024-04-17T15:24:50","slug":"%e4%ba%8c%e3%80%81%e5%b8%b8%e7%94%a8-composition-api","status":"publish","type":"post","link":"http:\/\/zyg.luchers.cn\/index.php\/2024\/04\/17\/%e4%ba%8c%e3%80%81%e5%b8%b8%e7%94%a8-composition-api\/","title":{"rendered":"\u4e8c\u3001\u5e38\u7528 Composition API"},"content":{"rendered":"<h1>\u4e8c\u3001\u5e38\u7528 Composition API<\/h1>\n<p>\u5b98\u65b9\u6587\u6863: <a href=\"https:\/\/v3.cn.vuejs.org\/guide\/composition-api-introduction.html\">https:\/\/v3.cn.vuejs.org\/guide\/composition-api-introduction.html<\/a><\/p>\n<h2>1.\u62c9\u5f00\u5e8f\u5e55\u7684setup<\/h2>\n<ol>\n<li>\u7406\u89e3\uff1aVue3.0\u4e2d\u4e00\u4e2a\u65b0\u7684\u914d\u7f6e\u9879\uff0c\u503c\u4e3a\u4e00\u4e2a\u51fd\u6570\u3002<\/li>\n<li>setup\u662f\u6240\u6709<strong style=\"color:#DD5145\">Composition API\uff08\u7ec4\u5408API\uff09<\/strong><i style=\"color:gray;font-weight:bold\">\u201c \u8868\u6f14\u7684\u821e\u53f0 \u201d<\/i>\u3002<\/li>\n<li>\u7ec4\u4ef6\u4e2d\u6240\u7528\u5230\u7684\uff1a\u6570\u636e\u3001\u65b9\u6cd5\u7b49\u7b49\uff0c\u5747\u8981\u914d\u7f6e\u5728setup\u4e2d\u3002<\/li>\n<li>setup\u51fd\u6570\u7684\u4e24\u79cd\u8fd4\u56de\u503c\uff1a\n<ol>\n<li>\u82e5\u8fd4\u56de\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5219\u5bf9\u8c61\u4e2d\u7684\u5c5e\u6027\u3001\u65b9\u6cd5, \u5728\u6a21\u677f\u4e2d\u5747\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u3002\uff08\u91cd\u70b9\u5173\u6ce8\uff01\uff09<\/li>\n<li><span style=\"color:#aad\">\u82e5\u8fd4\u56de\u4e00\u4e2a\u6e32\u67d3\u51fd\u6570\uff1a\u5219\u53ef\u4ee5\u81ea\u5b9a\u4e49\u6e32\u67d3\u5185\u5bb9\u3002\uff08\u4e86\u89e3\uff09<\/span><\/li>\n<\/ol>\n<\/li>\n<li>\u6ce8\u610f\u70b9\uff1a\n<ol>\n<li>\u5c3d\u91cf\u4e0d\u8981\u4e0eVue2.x\u914d\u7f6e\u6df7\u7528\n<ul>\n<li>Vue2.x\u914d\u7f6e\uff08data\u3001methos\u3001computed...\uff09\u4e2d<strong style=\"color:#DD5145\">\u53ef\u4ee5\u8bbf\u95ee\u5230<\/strong>setup\u4e2d\u7684\u5c5e\u6027\u3001\u65b9\u6cd5\u3002<\/li>\n<li>\u4f46\u5728setup\u4e2d<strong style=\"color:#DD5145\">\u4e0d\u80fd\u8bbf\u95ee\u5230<\/strong>Vue2.x\u914d\u7f6e\uff08data\u3001methos\u3001computed...\uff09\u3002<\/li>\n<li>\u5982\u679c\u6709\u91cd\u540d, setup\u4f18\u5148\u3002<\/li>\n<\/ul>\n<\/li>\n<li>setup\u4e0d\u80fd\u662f\u4e00\u4e2aasync\u51fd\u6570\uff0c\u56e0\u4e3a\u8fd4\u56de\u503c\u4e0d\u518d\u662freturn\u7684\u5bf9\u8c61, \u800c\u662fpromise, \u6a21\u677f\u770b\u4e0d\u5230return\u5bf9\u8c61\u4e2d\u7684\u5c5e\u6027\u3002\uff08\u540e\u671f\u4e5f\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2aPromise\u5b9e\u4f8b\uff0c\u4f46\u9700\u8981Suspense\u548c\u5f02\u6b65\u7ec4\u4ef6\u7684\u914d\u5408\uff09<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<h2>2.ref\u51fd\u6570<\/h2>\n<ul>\n<li>\u4f5c\u7528: \u5b9a\u4e49\u4e00\u4e2a\u54cd\u5e94\u5f0f\u7684\u6570\u636e<\/li>\n<li>\u8bed\u6cd5: <code><code>const xxx = ref(initValue)<\/code><\/code>\n<ul>\n<li>\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u54cd\u5e94\u5f0f\u6570\u636e\u7684<strong style=\"color:#DD5145\">\u5f15\u7528\u5bf9\u8c61\uff08reference\u5bf9\u8c61\uff0c\u7b80\u79f0ref\u5bf9\u8c61\uff09<\/strong>\u3002<\/li>\n<li>JS\u4e2d\u64cd\u4f5c\u6570\u636e\uff1a <code><code>xxx.value<\/code><\/code><\/li>\n<li>\u6a21\u677f\u4e2d\u8bfb\u53d6\u6570\u636e: \u4e0d\u9700\u8981.value\uff0c\u76f4\u63a5\uff1a<code><code>&lt;div&gt;{{xxx}}&lt;\/div&gt;<\/code><\/code><\/li>\n<\/ul>\n<\/li>\n<li>\u5907\u6ce8\uff1a\n<ul>\n<li>\u63a5\u6536\u7684\u6570\u636e\u53ef\u4ee5\u662f\uff1a\u57fa\u672c\u7c7b\u578b\u3001\u4e5f\u53ef\u4ee5\u662f\u5bf9\u8c61\u7c7b\u578b\u3002<\/li>\n<li>\u57fa\u672c\u7c7b\u578b\u7684\u6570\u636e\uff1a\u54cd\u5e94\u5f0f\u4f9d\u7136\u662f\u9760<code><code>Object.defineProperty()<\/code><code>\u7684<\/code><\/code><code>get<\/code><code><code>\u4e0e<\/code><\/code><code>set<\/code>``\u5b8c\u6210\u7684\u3002<\/li>\n<li>\u5bf9\u8c61\u7c7b\u578b\u7684\u6570\u636e\uff1a\u5185\u90e8 <i style=\"color:gray;font-weight:bold\">\u201c \u6c42\u52a9 \u201d<\/i> \u4e86Vue3.0\u4e2d\u7684\u4e00\u4e2a\u65b0\u51fd\u6570\u2014\u2014 <code><code>reactive<\/code><\/code>\u51fd\u6570\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>3.reactive\u51fd\u6570<\/h2>\n<ul>\n<li>\u4f5c\u7528: \u5b9a\u4e49\u4e00\u4e2a<strong style=\"color:#DD5145\">\u5bf9\u8c61\u7c7b\u578b<\/strong>\u7684\u54cd\u5e94\u5f0f\u6570\u636e\uff08\u57fa\u672c\u7c7b\u578b\u4e0d\u8981\u7528\u5b83\uff0c\u8981\u7528<code><code>ref<\/code><\/code>\u51fd\u6570\uff09<\/li>\n<li>\u8bed\u6cd5\uff1a<code><code>const \u4ee3\u7406\u5bf9\u8c61= reactive(\u6e90\u5bf9\u8c61)<\/code><\/code>\u63a5\u6536\u4e00\u4e2a\u5bf9\u8c61\uff08\u6216\u6570\u7ec4\uff09\uff0c\u8fd4\u56de\u4e00\u4e2a<strong style=\"color:#DD5145\">\u4ee3\u7406\u5bf9\u8c61\uff08Proxy\u7684\u5b9e\u4f8b\u5bf9\u8c61\uff0c\u7b80\u79f0proxy\u5bf9\u8c61\uff09<\/strong><\/li>\n<li>reactive\u5b9a\u4e49\u7684\u54cd\u5e94\u5f0f\u6570\u636e\u662f\u201c\u6df1\u5c42\u6b21\u7684\u201d\u3002<\/li>\n<li>\u5185\u90e8\u57fa\u4e8e ES6 \u7684 Proxy \u5b9e\u73b0\uff0c\u901a\u8fc7\u4ee3\u7406\u5bf9\u8c61\u64cd\u4f5c\u6e90\u5bf9\u8c61\u5185\u90e8\u6570\u636e\u8fdb\u884c\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<h2>4.Vue3.0\u4e2d\u7684\u54cd\u5e94\u5f0f\u539f\u7406<\/h2>\n<h3>vue2.x\u7684\u54cd\u5e94\u5f0f<\/h3>\n<ul>\n<li>\n<p>\u5b9e\u73b0\u539f\u7406\uff1a<\/p>\n<ul>\n<li>\n<p>\u5bf9\u8c61\u7c7b\u578b\uff1a\u901a\u8fc7<code><code>Object.defineProperty()<\/code><\/code>\u5bf9\u5c5e\u6027\u7684\u8bfb\u53d6\u3001\u4fee\u6539\u8fdb\u884c\u62e6\u622a\uff08\u6570\u636e\u52ab\u6301\uff09\u3002<\/p>\n<\/li>\n<li>\n<p>\u6570\u7ec4\u7c7b\u578b\uff1a\u901a\u8fc7\u91cd\u5199\u66f4\u65b0\u6570\u7ec4\u7684\u4e00\u7cfb\u5217\u65b9\u6cd5\u6765\u5b9e\u73b0\u62e6\u622a\u3002\uff08\u5bf9\u6570\u7ec4\u7684\u53d8\u66f4\u65b9\u6cd5\u8fdb\u884c\u4e86\u5305\u88f9\uff09\u3002<\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"language-js\">Object.defineProperty(data, 'count', {\n    get () {}, \n    set () {}\n})<\/code><\/pre>\n<\/li>\n<li>\n<p>\u5b58\u5728\u95ee\u9898\uff1a<\/p>\n<ul>\n<li>\u65b0\u589e\u5c5e\u6027\u3001\u5220\u9664\u5c5e\u6027, \u754c\u9762\u4e0d\u4f1a\u66f4\u65b0\u3002<\/li>\n<li>\u76f4\u63a5\u901a\u8fc7\u4e0b\u6807\u4fee\u6539\u6570\u7ec4, \u754c\u9762\u4e0d\u4f1a\u81ea\u52a8\u66f4\u65b0\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>Vue3.0\u7684\u54cd\u5e94\u5f0f<\/h3>\n<ul>\n<li>\n<p>\u5b9e\u73b0\u539f\u7406: <\/p>\n<ul>\n<li>\n<p>\u901a\u8fc7Proxy\uff08\u4ee3\u7406\uff09:  \u62e6\u622a\u5bf9\u8c61\u4e2d\u4efb\u610f\u5c5e\u6027\u7684\u53d8\u5316, \u5305\u62ec\uff1a\u5c5e\u6027\u503c\u7684\u8bfb\u5199\u3001\u5c5e\u6027\u7684\u6dfb\u52a0\u3001\u5c5e\u6027\u7684\u5220\u9664\u7b49\u3002<\/p>\n<\/li>\n<li>\n<p>\u901a\u8fc7Reflect\uff08\u53cd\u5c04\uff09:  \u5bf9\u6e90\u5bf9\u8c61\u7684\u5c5e\u6027\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n<\/li>\n<li>\n<p>MDN\u6587\u6863\u4e2d\u63cf\u8ff0\u7684Proxy\u4e0eReflect\uff1a<\/p>\n<\/li>\n<li>\n<p>Proxy\uff1a<a href=\"https:\/\/developer.mozilla.org\/zh-CN\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Proxy\">https:\/\/developer.mozilla.org\/zh-CN\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Proxy<\/a><\/p>\n<\/li>\n<li>\n<p>Reflect\uff1a<a href=\"https:\/\/developer.mozilla.org\/zh-CN\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Reflect\">https:\/\/developer.mozilla.org\/zh-CN\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Reflect<\/a><\/p>\n<pre><code class=\"language-js\">new Proxy(data, {\n\/\/ \u62e6\u622a\u8bfb\u53d6\u5c5e\u6027\u503c\n  get (target, prop) {\n    return Reflect.get(target, prop)\n  },\n  \/\/ \u62e6\u622a\u8bbe\u7f6e\u5c5e\u6027\u503c\u6216\u6dfb\u52a0\u65b0\u5c5e\u6027\n  set (target, prop, value) {\n    return Reflect.set(target, prop, value)\n  },\n  \/\/ \u62e6\u622a\u5220\u9664\u5c5e\u6027\n  deleteProperty (target, prop) {\n    return Reflect.deleteProperty(target, prop)\n  }\n})\n\nproxy.name = 'tom'   <\/code><\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>5.reactive\u5bf9\u6bd4ref<\/h2>\n<ul>\n<li>\u4ece\u5b9a\u4e49\u6570\u636e\u89d2\u5ea6\u5bf9\u6bd4\uff1a\n<ul>\n<li>ref\u7528\u6765\u5b9a\u4e49\uff1a<strong style=\"color:#DD5145\">\u57fa\u672c\u7c7b\u578b\u6570\u636e<\/strong>\u3002<\/li>\n<li>reactive\u7528\u6765\u5b9a\u4e49\uff1a<strong style=\"color:#DD5145\">\u5bf9\u8c61\uff08\u6216\u6570\u7ec4\uff09\u7c7b\u578b\u6570\u636e<\/strong>\u3002<\/li>\n<li>\u5907\u6ce8\uff1aref\u4e5f\u53ef\u4ee5\u7528\u6765\u5b9a\u4e49<strong style=\"color:#DD5145\">\u5bf9\u8c61\uff08\u6216\u6570\u7ec4\uff09\u7c7b\u578b\u6570\u636e<\/strong>, \u5b83\u5185\u90e8\u4f1a\u81ea\u52a8\u901a\u8fc7<code><code>reactive<\/code><\/code>\u8f6c\u4e3a<strong style=\"color:#DD5145\">\u4ee3\u7406\u5bf9\u8c61<\/strong>\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u4ece\u539f\u7406\u89d2\u5ea6\u5bf9\u6bd4\uff1a\n<ul>\n<li>ref\u901a\u8fc7<code><code>Object.defineProperty()<\/code><code>\u7684<\/code><\/code><code>get<\/code><code><code>\u4e0e<\/code><\/code><code>set<\/code>``\u6765\u5b9e\u73b0\u54cd\u5e94\u5f0f\uff08\u6570\u636e\u52ab\u6301\uff09\u3002<\/li>\n<li>reactive\u901a\u8fc7\u4f7f\u7528<strong style=\"color:#DD5145\">Proxy<\/strong>\u6765\u5b9e\u73b0\u54cd\u5e94\u5f0f\uff08\u6570\u636e\u52ab\u6301\uff09, \u5e76\u901a\u8fc7<strong style=\"color:#DD5145\">Reflect<\/strong>\u64cd\u4f5c<strong style=\"color:orange\">\u6e90\u5bf9\u8c61<\/strong>\u5185\u90e8\u7684\u6570\u636e\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u4ece\u4f7f\u7528\u89d2\u5ea6\u5bf9\u6bd4\uff1a\n<ul>\n<li>ref\u5b9a\u4e49\u7684\u6570\u636e\uff1a\u64cd\u4f5c\u6570\u636e<strong style=\"color:#DD5145\">\u9700\u8981<\/strong><code><code>.value<\/code>`<code>\uff0c\u8bfb\u53d6\u6570\u636e\u65f6\u6a21\u677f\u4e2d\u76f4\u63a5\u8bfb\u53d6&lt;strong style=&quot;color:#DD5145&quot;&gt;\u4e0d\u9700\u8981&lt;\/strong&gt;<\/code>`<code>.value<\/code><\/code>\u3002<\/li>\n<li>reactive\u5b9a\u4e49\u7684\u6570\u636e\uff1a\u64cd\u4f5c\u6570\u636e\u4e0e\u8bfb\u53d6\u6570\u636e\uff1a<strong style=\"color:#DD5145\">\u5747\u4e0d\u9700\u8981<\/strong><code><code>.value<\/code><\/code>\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>6.setup\u7684\u4e24\u4e2a\u6ce8\u610f\u70b9<\/h2>\n<ul>\n<li>\n<p>setup\u6267\u884c\u7684\u65f6\u673a<\/p>\n<ul>\n<li>\u5728beforeCreate\u4e4b\u524d\u6267\u884c\u4e00\u6b21\uff0cthis\u662fundefined\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>setup\u7684\u53c2\u6570<\/p>\n<ul>\n<li>props\uff1a\u503c\u4e3a\u5bf9\u8c61\uff0c\u5305\u542b\uff1a\u7ec4\u4ef6\u5916\u90e8\u4f20\u9012\u8fc7\u6765\uff0c\u4e14\u7ec4\u4ef6\u5185\u90e8\u58f0\u660e\u63a5\u6536\u4e86\u7684\u5c5e\u6027\u3002<\/li>\n<li>context\uff1a\u4e0a\u4e0b\u6587\u5bf9\u8c61<\/li>\n<li>attrs: \u503c\u4e3a\u5bf9\u8c61\uff0c\u5305\u542b\uff1a\u7ec4\u4ef6\u5916\u90e8\u4f20\u9012\u8fc7\u6765\uff0c\u4f46\u6ca1\u6709\u5728props\u914d\u7f6e\u4e2d\u58f0\u660e\u7684\u5c5e\u6027, \u76f8\u5f53\u4e8e <code><code>this.$attrs<\/code><\/code>\u3002<\/li>\n<li>slots: \u6536\u5230\u7684\u63d2\u69fd\u5185\u5bb9, \u76f8\u5f53\u4e8e <code><code>this.$slots<\/code><\/code>\u3002<\/li>\n<li>emit: \u5206\u53d1\u81ea\u5b9a\u4e49\u4e8b\u4ef6\u7684\u51fd\u6570, \u76f8\u5f53\u4e8e <code><code>this.$emit<\/code><\/code>\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>7.\u8ba1\u7b97\u5c5e\u6027\u4e0e\u76d1\u89c6<\/h2>\n<h3>1.computed\u51fd\u6570<\/h3>\n<ul>\n<li>\n<p>\u4e0eVue2.x\u4e2dcomputed\u914d\u7f6e\u529f\u80fd\u4e00\u81f4<\/p>\n<\/li>\n<li>\n<p>\u5199\u6cd5<\/p>\n<pre><code class=\"language-js\">import {computed} from 'vue'\n\nsetup(){\n  ...\n\/\/\u8ba1\u7b97\u5c5e\u6027\u2014\u2014\u7b80\u5199\n  let fullName = computed(()=>{\n      return person.firstName + '-' + person.lastName\n  })\n  \/\/\u8ba1\u7b97\u5c5e\u6027\u2014\u2014\u5b8c\u6574\n  let fullName = computed({\n      get(){\n          return person.firstName + '-' + person.lastName\n      },\n      set(value){\n          const nameArr = value.split('-')\n          person.firstName = nameArr[0]\n          person.lastName = nameArr[1]\n      }\n  })\n}<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3>2.watch\u51fd\u6570<\/h3>\n<ul>\n<li>\n<p>\u4e0eVue2.x\u4e2dwatch\u914d\u7f6e\u529f\u80fd\u4e00\u81f4<\/p>\n<\/li>\n<li>\n<p>\u4e24\u4e2a\u5c0f\u201c\u5751\u201d\uff1a<\/p>\n<ul>\n<li>\u76d1\u89c6reactive\u5b9a\u4e49\u7684\u54cd\u5e94\u5f0f\u6570\u636e\u65f6\uff1aoldValue\u65e0\u6cd5\u6b63\u786e\u83b7\u53d6\u3001\u5f3a\u5236\u5f00\u542f\u4e86\u6df1\u5ea6\u76d1\u89c6\uff08deep\u914d\u7f6e\u5931\u6548\uff09\u3002<\/li>\n<li>\u76d1\u89c6reactive\u5b9a\u4e49\u7684\u54cd\u5e94\u5f0f\u6570\u636e\u4e2d\u67d0\u4e2a\u5c5e\u6027\u65f6\uff1adeep\u914d\u7f6e\u6709\u6548\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-js\">\/\/\u60c5\u51b5\u4e00\uff1a\u76d1\u89c6ref\u5b9a\u4e49\u7684\u54cd\u5e94\u5f0f\u6570\u636e\nwatch(sum,(newValue,oldValue)=>{\nconsole.log('sum\u53d8\u5316\u4e86',newValue,oldValue)\n},{immediate:true})\n\n\/\/\u60c5\u51b5\u4e8c\uff1a\u76d1\u89c6\u591a\u4e2aref\u5b9a\u4e49\u7684\u54cd\u5e94\u5f0f\u6570\u636e\nwatch([sum,msg],(newValue,oldValue)=>{\nconsole.log('sum\u6216msg\u53d8\u5316\u4e86',newValue,oldValue)\n}) \n\n\/* \u60c5\u51b5\u4e09\uff1a\u76d1\u89c6reactive\u5b9a\u4e49\u7684\u54cd\u5e94\u5f0f\u6570\u636e\n        \u82e5watch\u76d1\u89c6\u7684\u662freactive\u5b9a\u4e49\u7684\u54cd\u5e94\u5f0f\u6570\u636e\uff0c\u5219\u65e0\u6cd5\u6b63\u786e\u83b7\u5f97oldValue\uff01\uff01\n        \u82e5watch\u76d1\u89c6\u7684\u662freactive\u5b9a\u4e49\u7684\u54cd\u5e94\u5f0f\u6570\u636e\uff0c\u5219\u5f3a\u5236\u5f00\u542f\u4e86\u6df1\u5ea6\u76d1\u89c6 \n*\/\nwatch(person,(newValue,oldValue)=>{\nconsole.log('person\u53d8\u5316\u4e86',newValue,oldValue)\n},{immediate:true,deep:false}) \/\/\u6b64\u5904\u7684deep\u914d\u7f6e\u4e0d\u518d\u594f\u6548\n\n\/\/\u60c5\u51b5\u56db\uff1a\u76d1\u89c6reactive\u5b9a\u4e49\u7684\u54cd\u5e94\u5f0f\u6570\u636e\u4e2d\u7684\u67d0\u4e2a\u5c5e\u6027\nwatch(()=>person.job,(newValue,oldValue)=>{\nconsole.log('person\u7684job\u53d8\u5316\u4e86',newValue,oldValue)\n},{immediate:true,deep:true}) \n\n\/\/\u60c5\u51b5\u4e94\uff1a\u76d1\u89c6reactive\u5b9a\u4e49\u7684\u54cd\u5e94\u5f0f\u6570\u636e\u4e2d\u7684\u67d0\u4e9b\u5c5e\u6027\nwatch([()=>person.job,()=>person.name],(newValue,oldValue)=>{\nconsole.log('person\u7684job\u53d8\u5316\u4e86',newValue,oldValue)\n},{immediate:true,deep:true})\n\n\/\/\u7279\u6b8a\u60c5\u51b5\nwatch(()=>person.job,(newValue,oldValue)=>{\n  console.log('person\u7684job\u53d8\u5316\u4e86',newValue,oldValue)\n},{deep:true}) \/\/\u6b64\u5904\u7531\u4e8e\u76d1\u89c6\u7684\u662freactive\u7d20\u5b9a\u4e49\u7684\u5bf9\u8c61\u4e2d\u7684\u67d0\u4e2a\u5c5e\u6027\uff0c\u6240\u4ee5deep\u914d\u7f6e\u6709\u6548<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3>3.watchEffect\u51fd\u6570<\/h3>\n<ul>\n<li>\n<p>watch\u7684\u5957\u8def\u662f\uff1a\u65e2\u8981\u6307\u660e\u76d1\u89c6\u7684\u5c5e\u6027\uff0c\u4e5f\u8981\u6307\u660e\u76d1\u89c6\u7684\u56de\u8c03\u3002<\/p>\n<\/li>\n<li>\n<p>watchEffect\u7684\u5957\u8def\u662f\uff1a\u4e0d\u7528\u6307\u660e\u76d1\u89c6\u54ea\u4e2a\u5c5e\u6027\uff0c\u76d1\u89c6\u7684\u56de\u8c03\u4e2d\u7528\u5230\u54ea\u4e2a\u5c5e\u6027\uff0c\u90a3\u5c31\u76d1\u89c6\u54ea\u4e2a\u5c5e\u6027\u3002<\/p>\n<\/li>\n<li>\n<p>watchEffect\u6709\u70b9\u50cfcomputed\uff1a<\/p>\n<ul>\n<li>\u4f46computed\u6ce8\u91cd\u7684\u8ba1\u7b97\u51fa\u6765\u7684\u503c\uff08\u56de\u8c03\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff09\uff0c\u6240\u4ee5\u5fc5\u987b\u8981\u5199\u8fd4\u56de\u503c\u3002<\/li>\n<li>\u800cwatchEffect\u66f4\u6ce8\u91cd\u7684\u662f\u8fc7\u7a0b\uff08\u56de\u8c03\u51fd\u6570\u7684\u51fd\u6570\u4f53\uff09\uff0c\u6240\u4ee5\u4e0d\u7528\u5199\u8fd4\u56de\u503c\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-js\">\/\/watchEffect\u6240\u6307\u5b9a\u7684\u56de\u8c03\u4e2d\u7528\u5230\u7684\u6570\u636e\u53ea\u8981\u53d1\u751f\u53d8\u5316\uff0c\u5219\u76f4\u63a5\u91cd\u65b0\u6267\u884c\u56de\u8c03\u3002\nwatchEffect(()=>{\n  const x1 = sum.value\n  const x2 = person.age\n  console.log('watchEffect\u914d\u7f6e\u7684\u56de\u8c03\u6267\u884c\u4e86')\n})<\/code><\/pre>\n<\/li>\n<\/ul>\n<h2>8.\u751f\u547d\u5468\u671f<\/h2>\n<div style=\"border:1px solid black;width:380px;float:left;margin-right:20px;\"><strong>vue2.x\u7684\u751f\u547d\u5468\u671f<\/strong><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/cn.vuejs.org\/images\/lifecycle.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/cn.vuejs.org\/images\/lifecycle.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"lifecycle_2\" style=\"zoom:33%;width:1200px\" \/><\/div><\/div>\n<div style=\"border:1px solid black;width:510px;height:985px;float:left\"><strong>vue3.0\u7684\u751f\u547d\u5468\u671f<\/strong><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/v3.cn.vuejs.org\/images\/lifecycle.svg'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/v3.cn.vuejs.org\/images\/lifecycle.svg\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"lifecycle_2\" style=\"zoom:33%;width:2500px\" \/><\/div><\/div>\n<p>1<\/p>\n<ul>\n<li>Vue3.0\u4e2d\u53ef\u4ee5\u7ee7\u7eed\u4f7f\u7528Vue2.x\u4e2d\u7684\u751f\u547d\u5468\u671f\u94a9\u5b50\uff0c\u4f46\u6709\u6709\u4e24\u4e2a\u88ab\u66f4\u540d\uff1a\n<ul>\n<li><code><code>beforeDestroy<\/code>`<code>\u6539\u540d\u4e3a <\/code>`<code>beforeUnmount<\/code><\/code><\/li>\n<li><code><code>destroyed<\/code>`<code>\u6539\u540d\u4e3a <\/code>`<code>unmounted<\/code><\/code><\/li>\n<\/ul>\n<\/li>\n<li>Vue3.0\u4e5f\u63d0\u4f9b\u4e86 Composition API \u5f62\u5f0f\u7684\u751f\u547d\u5468\u671f\u94a9\u5b50\uff0c\u4e0eVue2.x\u4e2d\u94a9\u5b50\u5bf9\u5e94\u5173\u7cfb\u5982\u4e0b\uff1a\n<ul>\n<li><code>beforeCreate<\/code>===&gt;<code>setup()<\/code><\/li>\n<li><code>created<\/code>=======&gt;<code>setup()<\/code><\/li>\n<li><code>beforeMount<\/code> ===&gt;<code>onBeforeMount<\/code><\/li>\n<li><code>mounted<\/code>=======&gt;<code>onMounted<\/code><\/li>\n<li><code>beforeUpdate<\/code>===&gt;<code>onBeforeUpdate<\/code><\/li>\n<li><code>updated<\/code> =======&gt;<code>onUpdated<\/code><\/li>\n<li><code>beforeUnmount<\/code>\u00a0==&gt;<code>onBeforeUnmount<\/code><\/li>\n<li><code>unmounted<\/code> =====&gt;<code>onUnmounted<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>9.\u81ea\u5b9a\u4e49hook\u51fd\u6570<\/h2>\n<ul>\n<li>\n<p>\u4ec0\u4e48\u662fhook\uff1f\u2014\u2014 \u672c\u8d28\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u628asetup\u51fd\u6570\u4e2d\u4f7f\u7528\u7684Composition API\u8fdb\u884c\u4e86\u5c01\u88c5\u3002<\/p>\n<\/li>\n<li>\n<p>\u7c7b\u4f3c\u4e8evue2.x\u4e2d\u7684mixin\u3002<\/p>\n<\/li>\n<li>\n<p>\u81ea\u5b9a\u4e49hook\u7684\u4f18\u52bf: \u590d\u7528\u4ee3\u7801, \u8ba9setup\u4e2d\u7684\u903b\u8f91\u66f4\u6e05\u695a\u6613\u61c2\u3002<\/p>\n<\/li>\n<\/ul>\n<h2>10.toRef<\/h2>\n<ul>\n<li>\n<p>\u4f5c\u7528\uff1a\u521b\u5efa\u4e00\u4e2a ref \u5bf9\u8c61\uff0c\u5176value\u503c\u6307\u5411\u53e6\u4e00\u4e2a\u5bf9\u8c61\u4e2d\u7684\u67d0\u4e2a\u5c5e\u6027\u3002<\/p>\n<\/li>\n<li>\n<p>\u8bed\u6cd5\uff1a<code><code>const name = toRef(person,&#039;name&#039;)<\/code><\/code><\/p>\n<\/li>\n<li>\n<p>\u5e94\u7528:   \u8981\u5c06\u54cd\u5e94\u5f0f\u5bf9\u8c61\u4e2d\u7684\u67d0\u4e2a\u5c5e\u6027\u5355\u72ec\u63d0\u4f9b\u7ed9\u5916\u90e8\u4f7f\u7528\u65f6\u3002<\/p>\n<\/li>\n<li>\n<p>\u6269\u5c55\uff1a<code><code>toRefs<\/code>`<code> \u4e0e<\/code>`<code>toRef<\/code>`<code>\u529f\u80fd\u4e00\u81f4\uff0c\u4f46\u53ef\u4ee5\u6279\u91cf\u521b\u5efa\u591a\u4e2a ref \u5bf9\u8c61\uff0c\u8bed\u6cd5\uff1a<\/code>`<code>toRefs(person)<\/code><\/code><\/p>\n<\/li>\n<\/ul>\n<h1><\/h1>\n","protected":false},"excerpt":{"rendered":"<p>\u4e8c\u3001\u5e38\u7528 Composition API \u5b98\u65b9\u6587\u6863: https:\/\/v3.cn.vuejs.org\/guid [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[61],"tags":[],"class_list":["post-5975","post","type-post","status-publish","format-standard","hentry","category-vue3"],"_links":{"self":[{"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/posts\/5975","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/comments?post=5975"}],"version-history":[{"count":1,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/posts\/5975\/revisions"}],"predecessor-version":[{"id":5976,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/posts\/5975\/revisions\/5976"}],"wp:attachment":[{"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/media?parent=5975"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/categories?post=5975"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/tags?post=5975"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}