{"id":1221,"date":"2023-06-02T04:03:16","date_gmt":"2023-06-02T12:03:16","guid":{"rendered":"https:\/\/ligai.cn\/blog\/?p=1221"},"modified":"2024-03-04T22:08:14","modified_gmt":"2024-03-05T06:08:14","slug":"%e6%8a%80%e6%9c%af%e5%88%86%e4%ba%ab-%e5%a6%82%e4%bd%95%e7%bc%96%e5%86%99%e5%90%8c%e6%97%b6%e5%85%bc%e5%ae%b9-vue2-%e5%92%8c-vue3-%e7%9a%84%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"https:\/\/ligai.cn\/blog\/sharing\/1221.html","title":{"rendered":"\u6280\u672f\u5206\u4eab | \u5982\u4f55\u7f16\u5199\u540c\u65f6\u517c\u5bb9 Vue2 \u548c Vue3 \u7684\u4ee3\u7801?"},"content":{"rendered":"\n<p>LigaAI \u7684\u8bc4\u8bba\u7f16\u8f91\u5668\u3001\u9644\u4ef6\u5c55\u793a\u4ee5\u53ca\u5bcc\u6587\u672c\u7f16\u8f91\u5668\u90fd\u652f\u6301\u5728 Vue2\uff08Web\uff09\u4e0e Vue3\uff08VSCode\u3001lDEA\uff09\u4e2d\u4f7f\u7528\u3002\u8fd9\u6837\u4e0d\u4ec5\u53ef\u4ee5\u5728\u4e0d\u540c Vue \u7248\u672c\u7684\u5de5\u7a0b\u4e2d\u95f4\u5171\u4eab\u4ee3\u7801\uff0c\u8fd8\u80fd\u4e3a\u540e\u7eed\u5347\u7ea7 Vue3 \u51cf\u5c11\u4e00\u5b9a\u963b\u788d\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1024\" height=\"311\" src=\"https:\/\/ligai.cn\/blog\/wp-content\/uploads\/2024\/03\/1-1-1024x311.png\" alt=\"\" class=\"wp-image-1401\" srcset=\"https:\/\/ligai.cn\/blog\/wp-content\/uploads\/2024\/03\/1-1-1024x311.png 1024w, https:\/\/ligai.cn\/blog\/wp-content\/uploads\/2024\/03\/1-1-300x91.png 300w, https:\/\/ligai.cn\/blog\/wp-content\/uploads\/2024\/03\/1-1-768x233.png 768w, https:\/\/ligai.cn\/blog\/wp-content\/uploads\/2024\/03\/1-1.png 1080w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u90a3\u4e48\uff0c\u540c\u65f6\u517c\u5bb9 Vue2 \u4e0e Vue3 \u7684\u4ee3\u7801\u8be5\u5982\u4f55\u5b9e\u73b0\uff1f\u4e1a\u52a1\u5b9e\u8df5\u4e2d\u53c8\u6709\u54ea\u4e9b\u4ee3\u7801\u7cbe\u7b80\u548c\u4f18\u5316\u7684\u5c0f\u6280\u5de7\uff1f\u8ba9\u6211\u4eec\u5148\u4ece\u517c\u5bb9\u4ee3\u7801\u7684\u5de5\u7a0b\u5316\u8bb2\u8d77\u3002<\/p>\n\n\n\n<h1>1. \u5de5\u7a0b\u5316\uff1a\u7f16\u5199\u540c\u65f6\u517c\u5bb9 Vue2 \u4e0e Vue3 \u7684\u4ee3\u7801<\/h1>\n\n\n\n<p>\u539f\u7406\u4e0a\uff0c\u517c\u5bb9\u5de5\u4f5c\u7531\u4e24\u90e8\u5206\u5b8c\u6210\uff1a<\/p>\n\n\n\n<ul><li><strong>\u7f16\u8bd1\u9636\u6bb5\uff1a\u8d1f\u8d23\u6839\u636e\u4f7f\u7528\u7684\u9879\u76ee\u73af\u5883\uff0c\u81ea\u52a8\u9009\u62e9\u4f7f\u7528 Vue2 \u6216 Vue3 \u7684 API\u3002<\/strong> \u4f7f\u7528\u65f6\uff0c\u53ea\u9700\u8981\u4ece <code>Vue-Demi<\/code> \u91cc\u9762 import \u9700\u8981\u4f7f\u7528\u7684 API\uff0c\u5c31\u4f1a\u81ea\u52a8\u6839\u636e\u73af\u5883\u8fdb\u884c\u5207\u6362\uff1b\u53ef\u4ee5\u5206\u4e3a\u5728\u6d4f\u89c8\u5668\u4e2d\u8fd0\u884c\uff08IIFE\uff09\u548c\u4f7f\u7528\u6253\u5305\u5de5\u5177\uff08cjs\u3001umd\u3001esm\uff09\u4e24\u79cd\u60c5\u51b5\u3002<\/li><li><strong>\u8fd0\u884c\u9636\u6bb5\uff1a\u8f6c\u6362 <code>createElement<\/code> \u51fd\u6570\u7684\u53c2\u6570\uff0c\u4f7f Vue2 \u4e0e Vue3 \u7684\u53c2\u6570\u683c\u5f0f\u4e00\u81f4\u3002<\/strong> Vue2 \u548c Vue3 Composition API \u7684\u533a\u522b\u975e\u5e38\u5c0f\uff0c\u8fd0\u884c\u65f6 API \u6700\u5927\u7684\u533a\u522b\u5728\u4e8e <code>createElement<\/code> \u51fd\u6570\u7684\u53c2\u6570\u683c\u5f0f\u4e0d\u4e00\u81f4\uff0cVue3 \u6362\u6210\u4e86 React JSX \u683c\u5f0f\u3002<\/li><\/ul>\n\n\n\n<h2>1.1 \u7f16\u8bd1\u9636\u6bb5\u2014\u2014IIFE<\/h2>\n\n\n\n<p>\u5728 <code>window<\/code>\u4e2d\u5b9a\u4e49\u4e00\u4e2a <code>VueDemi<\/code> \u53d8\u91cf\uff0c\u7136\u540e\u68c0\u67e5 <code>window<\/code> \u4e2d\u7684 Vue \u53d8\u91cf\u7684\u7248\u672c\uff0c\u6839\u636e\u7248\u672c reexport \u5bf9\u5e94\u7684 API\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var VueDemi = (function (VueDemi, Vue, VueCompositionAPI) {  \n  \/\/ Vue 2.7 \u6709\u4e0d\u540c\uff0c\u8fd9\u91cc\u53ea\u5217\u51fa 2.0 ~ 2.6 \u7684\u7248\u672c\n  if (Vue.version.slice(0, 2) === '2.') {\n    for (var key in VueCompositionAPI) {\n          VueDemi&#91;key] = VueCompositionAPI&#91;key]    \n    }    \n    VueDemi.isVue2 = true\n  } else if (Vue.version.slice(0, 2) === '3.') {\n    for (var key in Vue) {\n    VueDemi&#91;key] = Vue&#91;key]\n    }    \n    VueDemi.isVue3 = true\n  }  \n    return VueDemi\n  })(this.VueDemi,this.Vue,this.VueCompositionAPI)<\/code><\/pre>\n\n\n\n<h2>1.2 \u7f16\u8bd1\u9636\u6bb5\u2014\u2014\u6253\u5305\u5de5\u5177<\/h2>\n\n\n\n<p>\u5229\u7528 <code>npm postinstall<\/code> \u7684 hook\uff0c\u68c0\u67e5\u672c\u5730\u7684 Vue \u7248\u672c\uff0c\u7136\u540e\u6839\u636e\u7248\u672c reexport \u5bf9\u5e94\u7684 API\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const Vue = loadModule('vue') \/\/ \u8fd9\u91cc\u662f\u68c0\u67e5\u672c\u5730\u7684 vue \u7248\u672c\nif (Vue.version.startsWith('2.')) {\n  switchVersion(2)\n}\nelse if (Vue.version.startsWith('3.')) {\n  switchVersion(3)\n}\nfunction switchVersion(version, vue) {\n  copy('index.cjs', version, vue)\n  copy('index.mjs', version, vue)\n}\n\/\/ VueDemi \u81ea\u5df1\u7684 lib \u76ee\u5f55\u4e0b\u6709 v2 v3 v2.7 \u4e09\u4e2a\u6587\u4ef6\u5939\uff0c\u5206\u522b\u5bf9\u5e94\u4e0d\u540c\u7684 Vue \u7248\u672c\uff0cCopy \u51fd\u6570\u7684\u529f\u80fd\u5c31\u662f\u628a\u9700\u8981\u7684\u7248\u672c\u590d\u5236\u5230 lib \u76ee\u5f55\u4e0b\n\/\/ \u7136\u540e\u5728 package.json \u91cc\u9762\u6307\u5411 lib\/index.cjs \u548c lib\/index.mjs\nfunction copy(name, version, vue) {\n  const src = path.join(dir, `v${version}`, name)\n  const dest = path.join(dir, name)\n  fs.write(dest, fs.read(src))\n}<\/code><\/pre>\n\n\n\n<h2>1.3 \u8fd0\u884c\u9636\u6bb5 createElement \u51fd\u6570\u7684\u533a\u522b<\/h2>\n\n\n\n<h3>1.3.1 Vue 2<\/h3>\n\n\n\n<ul><li>attrs \u9700\u8981\u5199\u5728 <code>attrs<\/code> \u5c5e\u6027\u4e2d\uff1b<\/li><li><code>on: { click=&gt; {}}<\/code><\/li><li>scopedSlots \u5199\u5728 <code>scopedSlots<\/code> \u5c5e\u6027\u4e2d\u3002<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>h(LayoutComponent, {\n    staticClass: 'button',\n    class: { 'is-outlined': isOutlined },\n    staticStyle: { color: '#34495E' },\n    style: { backgroundColor: buttonColor },\n    attrs: { id: 'submit' },\n    domProps: { innerHTML: '' },\n    on: { click: submitForm },\n    key: 'submit-button',\n    \/\/ \u8fd9\u91cc\u53ea\u8003\u8651 scopedSlots \u7684\u60c5\u51b5\u4e86\n    \/\/ \u4e4b\u524d\u7684 slots \u6ca1\u5fc5\u8981\u8003\u8651\uff0c\u5168\u90e8\u7528 scopedSlots \u662f\u4e00\u6837\u7684\n    scopedSlots: { \n      header: () =&gt; h('div', this.header),\n      content: () =&gt; h('div', this.content),\n    },\n  }\n);<\/code><\/pre>\n\n\n\n<h3>1.3.2 Vue 3<\/h3>\n\n\n\n<ul><li><code>attrs<\/code> \u548c <code>props<\/code> \u4e00\u6837\uff0c\u53ea\u9700\u5199\u5728\u6700\u5916\u5c42\uff1b<\/li><li><code>onClick: ()=&gt; {}<\/code><\/li><li>slot \u5199\u5728 <code>createElement<\/code> \u51fd\u6570\u7684\u7b2c\u4e09\u4e2a\u53c2\u6570\u4e2d\u3002<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>h(LayoutComponent, {\n    class: &#91;'button', { 'is-outlined': isOutlined }],\n    style: &#91;{ color: '#34495E' }, { backgroundColor: buttonColor }],\n    id: 'submit',\n    innerHTML: '',\n    onClick: submitForm,\n    key: 'submit-button',\n  }, {\n    header: () =&gt; h('div', this.header),\n    content: () =&gt; h('div', this.content),\n  }\n);<\/code><\/pre>\n\n\n\n<h2>1.4 \u5b8c\u6574\u4ee3\u7801<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>import { h as hDemi, isVue2 } from 'vue-demi';\n\n\/\/ \u6211\u4eec\u4f7f\u7528\u7684\u65f6\u5019\u4f7f\u7528\u7684 Vue2 \u7684\u5199\u6cd5\uff0c\u4f46\u662f props \u8fd8\u662f\u5199\u5728\u6700\u5916\u5c42\uff0c\u4e3a\u4e86 ts \u7684\u667a\u80fd\u63d0\u793a\nexport const h = (\n  type: String | Record&lt;any, any&gt;,\n  options: Options &amp; any = {},\n  children?: any,\n) =&gt; {\n  if (isVue2) {\n    const propOut = omit(options, &#91;\n      'props',\n      \/\/ ... \u7701\u7565\u4e86\u5176\u4ed6 Vue 2 \u7684\u9ed8\u8ba4\u5c5e\u6027\u5982 attrs\u3001on\u3001domProps\u3001class\u3001style\n    ]);\n    \/\/ \u8fd9\u91cc\u63d0\u53d6\u51fa\u4e86\u7ec4\u4ef6\u7684 props\n    const props = defaults(propOut, options.props || {}); \n    if ((type as Record&lt;string, any&gt;).props) {\n      \/\/ \u8fd9\u91cc\u7701\u7565\u4e86\u4e00\u4e9b\u8fc7\u6ee4 attrs \u548c props \u7684\u903b\u8f91\uff0c\u4e0d\u662f\u5f88\u91cd\u8981\n      return hDemi(type, { ...options, props }, children);\n    }\n    return hDemi(type, { ...options, props }, children);\n  }\n\n  const { props, attrs, domProps, on, scopedSlots, ...extraOptions } = options;\n\n  const ons = adaptOnsV3(on); \/\/ \u5904\u7406\u4e8b\u4ef6\n  const params = { ...extraOptions, ...props, ...attrs, ...domProps, ...ons }; \/\/ \u6392\u9664 scopedSlots\n\n  const slots = adaptScopedSlotsV3(scopedSlots); \/\/ \u5904\u7406 slots\n  if (slots &amp;&amp; Object.keys(slots).length) {\n    return hDemi(type, params, {\n      default: slots?.default || children,\n      ...slots,\n    });\n  }\n  return hDemi(type, params, children);\n};\n\nconst adaptOnsV3 = (ons: Object) =&gt; {\n  if (!ons) return null;\n  return Object.entries(ons).reduce((ret, &#91;key, handler]) =&gt; {\n    \/\/ \u4fee\u9970\u7b26\u7684\u8f6c\u6362\n    if (key&#91;0] === '!') {\n      key = key.slice(1) + 'Capture';\n    } else if (key&#91;0] === '&amp;') {\n      key = key.slice(1) + 'Passive';\n    } else if (key&#91;0] === '~') {\n      key = key.slice(1) + 'Once';\n    }\n    key = key.charAt(0).toUpperCase() + key.slice(1);\n    key = `on${key}`;\n\n    return { ...ret, &#91;key]: handler };\n  }, {});\n};\n\nconst adaptScopedSlotsV3 = (scopedSlots: any) =&gt; {\n  if (!scopedSlots) return null;\n  return Object.entries(scopedSlots).reduce((ret, &#91;key, slot]) =&gt; {\n    if (isFunction(slot)) {\n      return { ...ret, &#91;key]: slot };\n    }\n    return ret;\n  }, {} as Record&lt;string, Function&gt;);\n};<\/code><\/pre>\n\n\n\n<h1>2. \u7f16\u7801\u6280\u5de7\uff1a\u5229\u7528\u4ee3\u6570\u6570\u636e\u7c7b\u578b\u7cbe\u7b80\u4ee3\u7801<\/h1>\n\n\n\n<p>\u8fd9\u91cc\u8ddf\u5927\u5bb6\u5206\u4eab\u6211\u81ea\u5df1\u603b\u7ed3\u7684\u7528\u4e8e\u4f18\u5316\u4ee3\u7801\u7684\u7406\u8bba\u5de5\u5177\u3002\u6e29\u99a8\u63d0\u793a\uff0c\u53ef\u80fd\u548c\u4e66\u672c\u4e0a\u7684\u539f\u6709\u6982\u5ff5\u6709\u4e9b\u4e0d\u540c\u3002<\/p>\n\n\n\n<p>\u4e8e\u6211\u800c\u8a00\uff0c\u8861\u91cf\u4e00\u6bb5\u4ee3\u7801\u590d\u6742\u5ea6\u7684\u65b9\u6cd5\u662f\u770b\u72b6\u6001\u6570\u91cf\u3002\u72b6\u6001\u8d8a\u5c11\uff0c\u903b\u8f91\u3001\u4ee3\u7801\u5c31\u8d8a\u7b80\u5355\uff1b\u72b6\u6001\u6570\u91cf\u8d8a\u591a\uff0c\u903b\u8f91\u3001\u4ee3\u7801\u8d8a\u590d\u6742\uff0c\u8d8a\u5bb9\u6613\u51fa\u9519\u3002\u56e0\u6b64\uff0c\u6211\u8ba4\u4e3a\u300c\u597d\u4ee3\u7801\u300d\u7684\u7279\u5f81\u4e4b\u4e00\u5c31\u662f\uff0c\u5728\u5b8c\u6210\u4e1a\u52a1\u9700\u6c42\u7684\u524d\u63d0\u4e0b\uff0c\u5c3d\u91cf\u51cf\u5c11\u72b6\u6001\u7684\u6570\u91cf\uff08\u5373\u5927\u5c0f\uff09\u3002<\/p>\n\n\n\n<p>\u90a3\u4e48\uff0c\u4ec0\u4e48\u662f\u72b6\u6001\uff1f\u5728 Vue \u7684\u573a\u666f\u4e0b\uff0c\u53ef\u4ee5\u8fd9\u4e48\u7406\u89e3\uff1a<\/p>\n\n\n\n<ul><li>data \u91cc\u9762\u7684\u53d8\u91cf\u5c31\u662f\u72b6\u6001\uff0cprops\u3001\u8ba1\u7b97\u5c5e\u6027\u90fd\u4e0d\u662f\u72b6\u6001\u3002<\/li><li>Composition API \u4e2d <code>ref<\/code> \u548c <code>reactive<\/code> \u662f\u72b6\u6001\uff0c\u800c computed \u4e0d\u662f\u72b6\u6001\u3002<\/li><\/ul>\n\n\n\n<h2>2.1 \u4ec0\u4e48\u662f\u300c\u72b6\u6001\u300d\uff1f<\/h2>\n\n\n\n<p>\u72b6\u6001\u662f\u53ef\u4ee5\u7531\u7cfb\u7edf\u5185\u90e8\u884c\u4e3a\u66f4\u6539\u7684\u6570\u636e\uff0c\u800c\u72b6\u6001\u5927\u5c0f\u662f\u72b6\u6001\u6240\u6709\u53ef\u80fd\u7684\u503c\u7684\u96c6\u5408\u7684\u5927\u5c0f\uff0c\u8bb0\u4f5c <code>size(State)<\/code>\u3002\u800c<code>\u4ee3\u7801\u590d\u6742\u5ea6 = States.reduce((acc, cur) =&gt; acc * size(cur),1)<\/code>\u3002<\/p>\n\n\n\n<h3>2.1.1 \u5e38\u89c1\u6570\u636e\u7c7b\u578b\u7684\u72b6\u6001\u5927\u5c0f<\/h3>\n\n\n\n<p>\u4e00\u4e9b\u5e38\u89c1\u7684\u6570\u636e\u7c7b\u578b\uff0c\u6bd4\u5982 <code>unit<\/code> \u7684\u72b6\u6001\u5927\u5c0f\u662f 1\uff0c\u5728\u524d\u7aef\u91cc\u53ef\u4ee5\u662f null\u3001undefined\uff1b\u6240\u6709\u7684\u5e38\u91cf\u3001\u975e\u72b6\u6001\u7684\u5927\u5c0f\u4e5f\u662f 1\u3002\u800c <code>Boolean<\/code>\u7684\u72b6\u6001\u5927\u5c0f\u662f 2\u3002<\/p>\n\n\n\n<p><code>Number<\/code>\u548c <code>String<\/code> \u4e00\u7c7b\u6709\u591a\u4e2a\u6216\u65e0\u9650\u4e2a\u503c\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5728\u8ba1\u7b97\u72b6\u6001\u5927\u5c0f\u65f6\u9700\u660e\u786e\u4e00\u70b9\uff0c\u6211\u4eec\u53ea\u5173\u5fc3\u72b6\u6001\u5728\u4e1a\u52a1\u903b\u8f91\u4e2d\u7684\u610f\u4e49\uff0c\u800c\u4e0d\u662f\u5176\u5177\u4f53\u503c\uff0c\u56e0\u6b64\u533a\u5206\u4f1a\u5f71\u54cd\u4e1a\u52a1\u903b\u8f91\u7684\u72b6\u6001\u503c\u5373\u53ef\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\uff0c\u4e00\u4e2a\u63a5\u53e3\u8fd4\u56de\u7684\u6570\u636e\u662f\u4e00\u4e2a\u6570\u5b57\uff0c\u4f46\u6211\u4eec\u53ea\u5173\u5fc3\u8fd9\u4e2a\u6570\u5b57\u662f\u6b63\u6570\u8fd8\u662f\u8d1f\u6570\uff0c\u90a3\u4e48\u8fd9\u4e2a\u6570\u5b57\u7684\u72b6\u6001\u5927\u5c0f\u5c31\u662f 2\u3002<\/p>\n\n\n\n<h3>2.1.2 \u590d\u5408\u7c7b\u578b\u7684\u72b6\u6001\u5927\u5c0f<\/h3>\n\n\n\n<p>\u590d\u5408\u7c7b\u578b\u5206\u4e3a\u548c\u7c7b\u578b\u4e0e\u79ef\u7c7b\u578b\u4e24\u79cd\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" width=\"797\" height=\"444\" src=\"https:\/\/ligai.cn\/blog\/wp-content\/uploads\/2024\/03\/2-1.png\" alt=\"\" class=\"wp-image-1402\" srcset=\"https:\/\/ligai.cn\/blog\/wp-content\/uploads\/2024\/03\/2-1.png 797w, https:\/\/ligai.cn\/blog\/wp-content\/uploads\/2024\/03\/2-1-300x167.png 300w, https:\/\/ligai.cn\/blog\/wp-content\/uploads\/2024\/03\/2-1-768x428.png 768w\" sizes=\"(max-width: 797px) 100vw, 797px\" \/><\/figure>\n\n\n\n<p>\u548c\u7c7b\u578b\u72b6\u6001\u5927\u5c0f\u7684\u8ba1\u7b97\u516c\u5f0f\u4e3a <code>size(C) = size(A) + size(B)<\/code>\uff0c\u800c\u79ef\u7c7b\u578b\u72b6\u6001\u5927\u5c0f\u7684\u8ba1\u7b97\u516c\u5f0f\u4e3a <code>size(C) = size(A) * size(B)<\/code>\u3002<\/p>\n\n\n\n<p>\u4e86\u89e3\u5b8c\u4ee3\u7801\u4f18\u5316\u6807\u51c6\u540e\uff0c\u6211\u4eec\u901a\u8fc7\u4e00\u4e2a\u6848\u4f8b\u8bf4\u660e\u5982\u4f55\u5229\u7528\u4ee3\u6570\u6570\u636e\u7c7b\u578b\uff0c\u7cbe\u7b80\u4ee3\u7801\u3002<\/p>\n\n\n\n<h2>2.2 \u6848\u4f8b\uff1a\u8bc4\u8bba\u7f16\u8f91\u5668\u7684\u663e\u793a\u63a7\u5236<\/h2>\n\n\n\n<p>\u5728 LigaAI \u4e2d\uff0c\u6bcf\u4e2a\u8bc4\u8bba\u90fd\u6709\u4e24\u4e2a\u7f16\u8f91\u5668\uff0c\u4e00\u4e2a\u7528\u6765\u7f16\u8f91\u8bc4\u8bba\uff0c\u4e00\u4e2a\u7528\u6765\u56de\u590d\u8bc4\u8bba\uff1b\u4e14\u540c\u4e00\u65f6\u95f4\u6700\u591a\u53ea\u5141\u8bb8\u5b58\u5728\u4e00\u4e2a\u6d3b\u52a8\u7684\u7f16\u8f91\u5668\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1024\" height=\"325\" src=\"https:\/\/ligai.cn\/blog\/wp-content\/uploads\/2024\/03\/3-1-1024x325.png\" alt=\"\" class=\"wp-image-1403\" srcset=\"https:\/\/ligai.cn\/blog\/wp-content\/uploads\/2024\/03\/3-1-1024x325.png 1024w, https:\/\/ligai.cn\/blog\/wp-content\/uploads\/2024\/03\/3-1-300x95.png 300w, https:\/\/ligai.cn\/blog\/wp-content\/uploads\/2024\/03\/3-1-768x244.png 768w, https:\/\/ligai.cn\/blog\/wp-content\/uploads\/2024\/03\/3-1.png 1080w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h3>2.2.1 \u4f18\u5316\u524d\u7684\u505a\u6cd5<\/h3>\n\n\n\n<p>\u4e3a\u56de\u590d\u7ec4\u4ef6\u5b9a\u4e49\u4e24\u4e2a\u5e03\u5c14\u53d8\u91cf <code>IsShowReply<\/code> \u548c <code>IsShowEdit<\/code> \uff0c\u901a\u8fc7 <code>v-if<\/code> \u63a7\u5236\u662f\u5426\u663e\u793a\u7f16\u8f91\u5668\u3002\u70b9\u51fb\u300c\u56de\u590d\u300d\u6309\u94ae\u65f6\uff0c\u903b\u8f91\u5982\u4e0b\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>(1) \u5224\u65ad\u81ea\u5df1\u7684 <code>IsShowReply<\/code> \u662f\u5426\u4e3a true\uff0c\u5982\u679c\u662f\uff0c\u76f4\u63a5\u8fd4\u56de\uff1b<\/p><p>(2) \u5224\u65ad\u81ea\u5df1\u7684 <code>IsshowEdit<\/code>\uff0c\u5982\u679c\u4e3a true \u5219\u4fee\u6539\u4e3a false\uff0c\u5173\u95ed\u7f16\u8f91\u8bc4\u8bba\uff1b<\/p><p>(3) \u4f9d\u6b21\u8bbe\u7f6e\u6240\u6709\u5176\u4ed6\u8bc4\u8bba\u7ec4\u4ef6\u7684 <code>IsShowReply<\/code> \u548c <code>IsShowEdit<\/code> \u4e3a false\uff1b<\/p><p>(4) \u4fee\u6539\u81ea\u5df1\u7684 <code>IsShowReply<\/code> \u4e3a true\u3002<\/p><\/blockquote>\n\n\n\n<p>\u5f53\u6709 10 \u4e2a\u8bc4\u8bba\u7ec4\u4ef6\u65f6\uff0c\u4ee3\u7801\u590d\u6742\u5ea6\u662f\u591a\u5c11\uff1f<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>size(CommentComponent) = size(Boolean) * size(Boolean) = 2 * 2 = 4\nsize(total) = size(CommentComponent) ^ count(CommentComponent) = 4 ^ 10 = 1048576<\/code><\/pre>\n\n\n\n<p>\u5c3d\u7ba1\u903b\u8f91\u4e0a\u4e92\u65a5\uff0c\u4f46\u8fd9\u4e9b\u7ec4\u4ef6\u5728\u4ee3\u7801\u5c42\u9762\u6beb\u65e0\u5173\u7cfb\uff0c\u53ef\u4ee5\u5168\u90e8\u8bbe\u7f6e\u4e3a true\u3002\u5982\u679c\u4ee3\u7801\u51fa\u73b0\u95ee\u9898\uff08\u5305\u62ec\u5199\u9519\uff09\uff0c\u6ca1\u5904\u7406\u597d\u4e92\u65a5\uff0c\u8fd9\u79cd\u60c5\u51b5\u5b8c\u5168\u53ef\u80fd\u51fa\u73b0\u3002\u5904\u7406\u4e92\u65a5\u8fd8\u6d89\u53ca\u67e5\u627e dom \u548c\u7ec4\u4ef6\uff0c\u51fa\u95ee\u9898\u7684\u51e0\u7387\u4e5f\u4f1a\u5927\u5927\u63d0\u9ad8\u3002<\/p>\n\n\n\n<h3>2.2.2 \u4f18\u5316\u540e\u7684\u505a\u6cd5<\/h3>\n\n\n\n<p>\u5728 <code>store<\/code> \u4e2d\u5b9a\u4e49\u4e00\u4e2a\u5b57\u7b26\u4e32\u53d8\u91cf <code>activeCommentEditor<\/code>\uff0c\u8868\u793a\u5f53\u524d\u6d3b\u52a8\u7684\u8bc4\u8bba\u7ec4\u4ef6\u53ca\u5176\u7c7b\u578b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type CommentId = number;\ntype ActiveCommentStatus = `${'Edit' | 'Reply'}${CommentId}` | 'Close'; \/\/ TS \u7684\u6a21\u677f\u5b57\u7b26\u4e32\u7c7b\u578b\nlet activeCommentEditor: ActiveCommentStatus = 'Close';<\/code><\/pre>\n\n\n\n<p>\u9664 <code>'Close'<\/code> \u5916\uff0c\u8be5\u53d8\u91cf\u8fd8\u7531\u4e24\u90e8\u5206\u7ec4\u6210\u3002\u7b2c\u4e00\u90e8\u5206\u8bf4\u660e\u5f53\u524d\u662f\u300c\u7f16\u8f91\u8bc4\u8bba\u300d\u8fd8\u662f\u300c\u56de\u590d\u8bc4\u8bba\u300d\uff0c\u7b2c\u4e8c\u90e8\u5206\u8bf4\u660e\u8bc4\u8bba\u7684 id\u3002\u6309\u94ae\u7684\u56de\u8c03\u51fd\u6570\uff08\u5982\u70b9\u51fb\u56de\u590d\uff09\uff0c\u53ea\u9700\u8981\u8bbe\u7f6e<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>activeCommentEditor = `Reply${id}`<\/code><\/pre>\n\n\n\n<p>\u7ec4\u4ef6\u4f7f\u7528\u65f6\uff0c\u53ef\u4ee5\u8fd9\u6837<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>v-if=\"activeCommentEditor === `Edit${id}`\"\nv-if=\"activeCommentEditor === `Reply${id}`\"<\/code><\/pre>\n\n\n\n<p>\u5c31\u8fd9\u4e48\u7b80\u5355\uff0c\u6ca1\u6709\u5224\u65ad\uff0c\u6ca1\u6709 dom\uff0c\u6ca1\u6709\u5176\u4ed6\u7ec4\u4ef6\u3002\u867d\u7136 id \u662f number\uff0c\u4f46\u4e8e\u524d\u7aef\u800c\u8a00\u53ea\u662f\u4e00\u4e2a\u5e38\u91cf\uff0c\u6240\u4ee5\u5176\u5927\u5c0f\u4e3a 1\u3002\u90a3\u4e48\u5f53\u6709 10 \u4e2a\u8bc4\u8bba\u7ec4\u4ef6\u65f6\uff0c\u8fd9\u6bb5\u4ee3\u7801\u7684\u590d\u6742\u5ea6\u5c31\u662f<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>size(total) = size('Reply''Edit') * count(Comment) * 1 + size('close') = 2 * 10 * 1 +1 = 21<\/code><\/pre>\n\n\n\n<p>\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d\uff0c\u6211\u4eec\u53d1\u73b0\u786e\u5b9e\u5b58\u5728 21 \u79cd\u72b6\u6001\uff1b\u5728\u4ee3\u7801\u5c42\u9762\uff0c\u6211\u4eec\u4e5f\u7cbe\u51c6\u63a7\u5236\u4e86\u8fd9\u4e2a\u503c\u53ea\u80fd\u5728\u8fd9 21 \u79cd\u6b63\u786e\u7684\u72b6\u6001\u4e2d\uff0c\u6240\u4ee5\u51fa\u9519\u7684\u51e0\u7387\u4e5f\u5927\u5927\u964d\u4f4e\uff08\u51e0\u4e4e\u4e0d\u53ef\u80fd\u51fa\u9519\uff09\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>\u4ee5\u4e0a\u5c31\u662f\u4eca\u5929\u60f3\u8ddf\u5927\u5bb6\u5206\u4eab\u7684 Vue2 \u548c Vue3 \u4ee3\u7801\u517c\u5bb9\u7684\u5b9e\u73b0\u548c\u4f18\u5316\u65b9\u6848\uff0c\u540e\u7eed\u6211\u4eec\u4e5f\u4f1a\u5206\u4eab\u6216\u8865\u5145\u66f4\u591a\u76f8\u5173\u6848\u4f8b\u4e0e\u5b8c\u6574\u4ee3\u7801\u3002<\/p><\/blockquote>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>&gt;&gt; LigaAI \u5f80\u671f\u7cbe\u5f69\u9605\u8bfb &lt;&lt;<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/ligai.cn\/blog\/alige\/1216.html\">\u4ea7\u54c1\u7ecf\u7406\u7684 5 \u79cd\u9519\u8bef\u6253\u5f00\u65b9\u5f0f\uff0c\u4f60\u4e2d\u62db\u4e86\u5417\uff1f<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/ligai.cn\/blog\/%e7%a0%94%e5%8f%91%e7%ae%a1%e7%90%86\/1212.html\">\u4ec0\u4e48\u662f\u7814\u53d1 Lead Time\uff1f\u6211\u7ec8\u4e8e\u63b0\u626f\u660e\u767d\u4e86\uff01<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/ligai.cn\/blog\/%e7%a0%94%e5%8f%91%e7%ae%a1%e7%90%86\/1210.html\">\u7814\u53d1\u6548\u80fd\u7ba1\u7406\u4e2d\u7684\u7ecf\u5178\u5ea6\u91cf\u2014\u2014DORA \u6307\u6807<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/ligai.cn\/blog\/%e7%a0%94%e5%8f%91%e7%ae%a1%e7%90%86\/1202.html\">\u6e05\u5355\u63a8\u8350\uff1a\u5e38\u89c1\u7684\u7814\u53d1\u6548\u80fd\u5ea6\u91cf\u6307\u6807\uff08\u79d1\u5b66\u7ba1\u7406\u7248\uff09<\/a><\/p>\n\n\n\n<p> <strong><strong>LigaAI&nbsp;\u5c06\u6301\u7eed\u5206\u4eab\u66f4\u591a\u6210\u957f\u7ecf\u9a8c\u3001\u7814\u53d1\u6548\u80fd\u7ba1\u7406\u5b9e\u8df5\uff0c\u966a\u4f34\u6bcf\u4e00\u4e2a\u7814\u53d1\u56e2\u961f\u548c\u5f00\u53d1\u8005\u6210\u957f\u3002\u6b22\u8fce\u5173\u6ce8\u6211\u4eec\u7684\u56e2\u961f\u535a\u5ba2\u6216\u70b9\u51fb&nbsp;<a href=\"https:\/\/ligai.cn\/\">LigaAI-\u667a\u80fd\u7814\u53d1\u534f\u4f5c\u5e73\u53f0<\/a>\uff0c\u5728\u7ebf\u7533\u8bf7\u4f53\u9a8c\u6211\u4eec\u7684\u4ea7\u54c1\u3002<\/strong><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>LigaAI \u7684\u8bc4\u8bba\u7f16\u8f91\u5668\u3001\u9644\u4ef6\u5c55\u793a\u4ee5\u53ca\u5bcc\u6587\u672c\u7f16\u8f91\u5668\u90fd\u652f\u6301\u5728 Vue2\uff08Web\uff09\u4e0e Vue3\uff08VSCode\u3001l &#8230; <a title=\"\u6280\u672f\u5206\u4eab | \u5982\u4f55\u7f16\u5199\u540c\u65f6\u517c\u5bb9 Vue2 \u548c Vue3 \u7684\u4ee3\u7801?\" class=\"read-more\" href=\"https:\/\/ligai.cn\/blog\/sharing\/1221.html\" aria-label=\"\u7ee7\u7eed\u9605\u8bfb\u6280\u672f\u5206\u4eab | \u5982\u4f55\u7f16\u5199\u540c\u65f6\u517c\u5bb9 Vue2 \u548c Vue3 \u7684\u4ee3\u7801?\">\u9605\u8bfb\u66f4\u591a<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[22],"tags":[],"_links":{"self":[{"href":"https:\/\/ligai.cn\/blog\/wp-json\/wp\/v2\/posts\/1221"}],"collection":[{"href":"https:\/\/ligai.cn\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ligai.cn\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ligai.cn\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ligai.cn\/blog\/wp-json\/wp\/v2\/comments?post=1221"}],"version-history":[{"count":2,"href":"https:\/\/ligai.cn\/blog\/wp-json\/wp\/v2\/posts\/1221\/revisions"}],"predecessor-version":[{"id":1404,"href":"https:\/\/ligai.cn\/blog\/wp-json\/wp\/v2\/posts\/1221\/revisions\/1404"}],"wp:attachment":[{"href":"https:\/\/ligai.cn\/blog\/wp-json\/wp\/v2\/media?parent=1221"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ligai.cn\/blog\/wp-json\/wp\/v2\/categories?post=1221"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ligai.cn\/blog\/wp-json\/wp\/v2\/tags?post=1221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}