{"id":296980,"date":"2024-05-20T16:15:55","date_gmt":"2024-05-20T08:15:55","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/296980.html"},"modified":"2024-05-20T16:16:08","modified_gmt":"2024-05-20T08:16:08","slug":"%e5%a6%82%e4%bd%95%e7%90%86%e8%a7%a3-vue-%e7%9a%84-sync-%e4%bf%ae%e9%a5%b0%e7%ac%a6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/296980.html","title":{"rendered":"\u5982\u4f55\u7406\u89e3 Vue \u7684 sync \u4fee\u9970\u7b26"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27144608\/476f1945-2a73-4ae9-b4ff-ec8f6bbcf8d0.webp\" alt=\"\u5982\u4f55\u7406\u89e3 Vue \u7684 sync \u4fee\u9970\u7b26\" \/><\/p>\n<p><p>Vue \u7684 sync \u4fee\u9970\u7b26\u662f\u4e00\u79cd\u4fbf\u6377\u7684\u6570\u636e\u901a\u4fe1\u65b9\u5f0f\uff0c\u5b83\u5141\u8bb8\u5b50\u7ec4\u4ef6\u4fee\u6539\u7ed1\u5b9a\u7684\u7236\u7ec4\u4ef6\u72b6\u6001\u800c\u4e0d\u7528\u53d1\u51fa\u4e8b\u4ef6\u3002\u5b83\u57fa\u4e8e v-bind \u548c .sync \u540e\u7f00\u5de5\u4f5c\uff0c\u5141\u8bb8\u7236\u7ec4\u4ef6\u7684\u67d0\u4e2a\u5c5e\u6027\u5b9e\u73b0\u53cc\u5411\u7ed1\u5b9a\u3002\u7b80\u5355\u6765\u8bf4\uff0c<strong>sync \u4fee\u9970\u7b26\u662f\u5c06\u5b50\u7ec4\u4ef6\u5bf9\u5c5e\u6027\u7684\u66f4\u6539\u540c\u6b65\u5230\u7236\u7ec4\u4ef6\u7684\u8fc7\u7a0b<\/strong>\u3001<strong>\u5b83\u662f\u4e00\u79cd\u7b80\u5316\u4e86\u7684 .emit \u5b9e\u73b0<\/strong>\u3002<\/p>\n<\/p>\n<p><p>sync \u4fee\u9970\u7b26\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u8bed\u6cd5\u7cd6\uff0c\u5b83\u8ba9\u6211\u4eec\u5728\u5b9e\u73b0\u7236\u5b50\u7ec4\u4ef6\u95f4\u53cc\u5411\u901a\u4fe1\u65f6\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u3002\u4ee5\u5f80\u6211\u4eec\u9700\u8981\u5728\u5b50\u7ec4\u4ef6\u5185\u90e8\u624b\u52a8\u89e6\u53d1\u4e00\u4e2a\u4e8b\u4ef6\u6765\u901a\u77e5\u7236\u7ec4\u4ef6\u66f4\u65b0\u7279\u5b9a\u7684\u5c5e\u6027\uff0c\u73b0\u5728\u4f7f\u7528\u4e86.sync\uff0cVue\u4f1a\u81ea\u52a8\u5e2e\u6211\u4eec\u628a\u8fd9\u90e8\u5206\u5de5\u4f5c\u5c01\u88c5\u597d\u3002\u8fd9\u5c31\u610f\u5473\u7740\u6bcf\u5f53\u5b50\u7ec4\u4ef6\u60f3\u8981\u66f4\u65b0\u901a\u8fc7 sync \u4f20\u9012\u7684\u5c5e\u6027\u65f6\uff0c\u5b83\u53ea\u9700\u4fee\u6539\u8fd9\u4e2a\u5c40\u90e8\u72b6\u6001\uff0c\u800c\u4e0d\u7528\u624b\u52a8\u89e6\u53d1\u4e00\u4e2a\u66f4\u65b0\u4e8b\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001SYNC \u4fee\u9970\u7b26\u7684\u4f7f\u7528\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u8981\u7406\u89e3\u5982\u4f55\u4f7f\u7528 sync \u4fee\u9970\u7b26\uff0c\u9996\u5148\u6211\u4eec\u9700\u8981\u77e5\u9053\u5b83\u662f\u5982\u4f55\u7b80\u5316\u7236\u5b50\u7ec4\u4ef6\u95f4\u7684\u901a\u4fe1\u7684\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5b50\u7ec4\u4ef6\uff0c\u5b83\u5e0c\u671b\u80fd\u591f\u4fee\u6539\u4ece\u7236\u7ec4\u4ef6\u63a5\u6536\u5230\u7684\u67d0\u4e2aprop\u7684\u503c\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e0d\u4f7f\u7528sync\u4fee\u9970\u7b26\u7684\u60c5\u51b5\u4e0b\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5b50\u7ec4\u4ef6\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u4e8b\u4ef6\u5e76\u4e14\u5728\u9002\u5f53\u7684\u65f6\u5019\u89e6\u53d1\u5b83\u3002\u7236\u7ec4\u4ef6\u76d1\u542c\u8fd9\u4e2a\u4e8b\u4ef6\uff0c\u5e76\u5728\u4e8b\u4ef6\u7684\u5904\u7406\u51fd\u6570\u4e2d\u66f4\u65b0\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">\/\/ \u5b50\u7ec4\u4ef6<\/p>\n<p>this.$emit(&#039;update:title&#039;, newTitle)<\/p>\n<p>\/\/ \u7236\u7ec4\u4ef6<\/p>\n<p>&lt;child-component :title=&quot;pageTitle&quot; @update:title=&quot;pageTitle = $event&quot; \/&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f7f\u7528sync\u4fee\u9970\u7b26\u540e\uff1a<\/strong><\/p>\n<\/p>\n<p><p>Vue.js 2.3.0\u4ee5\u4e0a\u7248\u672c\u5f15\u5165\u4e86.sync\u4fee\u9970\u7b26\u3002\u73b0\u5728\u6211\u4eec\u4f7f\u7528.sync\uff0c\u8bed\u6cd5\u53d8\u5f97\u66f4\u52a0\u7b80\u5355\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">\/\/ \u7236\u7ec4\u4ef6<\/p>\n<p>&lt;child-component :title.sync=&quot;pageTitle&quot; \/&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53ea\u9700\u5728\u5b50\u7ec4\u4ef6\u4e2d\u66f4\u65b0title\u5c5e\u6027\uff0c\u7236\u7ec4\u4ef6\u4e2d\u7684pageTitle\u5c31\u4f1a\u540c\u6b65\u66f4\u65b0\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001SYNC \u4fee\u9970\u7b26\u7684\u5de5\u4f5c\u539f\u7406<\/h3>\n<\/p>\n<p><p><strong>\u5982\u4f55\u5de5\u4f5c?<\/strong><\/p>\n<\/p>\n<p><p>sync\u4fee\u9970\u7b26\u80cc\u540e\u7684\u5de5\u4f5c\u539f\u7406\u662f\u81ea\u52a8\u4e3a\u6211\u4eec\u5904\u7406\u4e8b\u4ef6\u7684\u521b\u5efa\u548c\u76d1\u542c\u3002\u5f53\u5b50\u7ec4\u4ef6\u66f4\u65b0\u4e86\u4e00\u4e2a\u5e26\u6709.sync\u4fee\u9970\u7b26\u7684prop\u65f6\uff0c\u5b83\u5b9e\u9645\u4e0a\u4f1a\u89e6\u53d1\u4e00\u4e2a\u201cupdate:myPropName\u201d\u7684\u4e8b\u4ef6\uff0c\u5176\u4e2dmyPropName\u662f\u7ed1\u5b9a.sync\u7684prop\u540d\u79f0\u3002\u7236\u7ec4\u4ef6\u76d1\u542c\u8fd9\u4e2a\u4e8b\u4ef6\uff0c\u5e76\u4e14\u5c06\u65b0\u503c\u8d4b\u503c\u7ed9\u5bf9\u5e94\u7684\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">\/\/ \u5b50\u7ec4\u4ef6\u5185\u90e8<\/p>\n<p>this.$emit(`update:title`, newTitle)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>Vue.js\u7684\u5185\u90e8\u673a\u5236<\/strong> <\/p>\n<\/p>\n<p><p>Vue\u6846\u67b6\u4f1a\u76d1\u542c\u7531sync\u4fee\u9970\u7b26\u6807\u8bb0\u7684\u6240\u6709props\u3002\u6bcf\u5f53\u8fd9\u4e9bprops\u53d8\u5316\u65f6\uff0cVue\u90fd\u4f1a\u89e6\u53d1\u4e00\u4e2a\u66f4\u65b0\u4e8b\u4ef6\u3002\u8fd9\u610f\u5473\u7740\u6211\u4eec\u4e0d\u9700\u8981\u624b\u52a8\u89e6\u53d1\u4efb\u4f55\u4e8b\u4ef6\uff0cVue\u4f1a\u5e2e\u6211\u4eec\u5b8c\u6210\u3002\u8fd9\u662f\u4e00\u4e2a\u81ea\u52a8\u5316\u7684\u8fc7\u7a0b\uff0c\u5141\u8bb8\u6211\u4eec\u5199\u51fa\u66f4\u5c11\u3001\u66f4\u6e05\u6d01\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001SYNC\u4fee\u9970\u7b26\u7684\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><p><strong>\u4fdd\u6301\u7ec4\u4ef6\u89e3\u8026<\/strong><\/p>\n<\/p>\n<p><p>\u867d\u7136sync\u4fee\u9970\u7b26\u63d0\u4f9b\u4e86\u4e00\u79cd\u5feb\u901f\u7684\u901a\u4fe1\u624b\u6bb5\uff0c\u4f46\u4e5f\u8981\u6ce8\u610f\u4e0d\u8981\u8fc7\u5ea6\u4f9d\u8d56\u5b83\u6765\u7ef4\u6301\u7ec4\u4ef6\u95f4\u7684\u901a\u4fe1\u3002\u7406\u60f3\u60c5\u51b5\u4e0b\uff0c\u7ec4\u4ef6\u5e94\u8be5\u4fdd\u6301\u4f4e\u8026\u5408\u5e76\u4e14\u80fd\u72ec\u7acb\u5730\u5de5\u4f5c\u3002\u8fc7\u5ea6\u7684sync\u4fee\u9970\u7b26\u4f7f\u7528\u53ef\u80fd\u4f1a\u5bfc\u81f4\u7236\u5b50\u7ec4\u4ef6\u4e4b\u95f4\u7684\u72b6\u6001\u592a\u8fc7\u7d27\u5bc6\uff0c\u96be\u4ee5\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u72ec\u7acb\u7684\u66f4\u65b0\u4e8b\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u66f4\u52a0\u590d\u6742\u7684\u6570\u636e\u6d41\u6216\u8005\u9700\u8981\u66f4\u591a\u63a7\u5236\u7684\u573a\u5408\uff0c\u4e0d\u5e94\u8fc7\u5206\u4f9d\u8d56.sync\u4fee\u9970\u7b26\u3002\u6211\u4eec\u5e94\u8be5\u8003\u8651\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684\u66f4\u65b0\u4e8b\u4ef6\uff0c\u8fd9\u80fd\u7ed9\u6211\u4eec\u5e26\u6765\u66f4\u591a\u7684\u7075\u6d3b\u6027\u3002\u6211\u4eec\u80fd\u591f\u660e\u786e\u5730\u63a7\u5236\u4e8b\u4ef6\u4f55\u65f6\u88ab\u89e6\u53d1\uff0c\u4ee5\u53ca\u54cd\u5e94\u4e8b\u4ef6\u7684\u5177\u4f53\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001SYNC\u4fee\u9970\u7b26\u7684\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><p>\u5c3d\u7ba1sync\u4fee\u9970\u7b26\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u65b9\u4fbf\uff0c\u4f46\u5728\u4f7f\u7528\u7684\u65f6\u5019\u8fd8\u9700\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<p><p><strong>\u4f20\u9012\u7684props\u662f\u53ef\u53d8\u7684<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528.sync\u4fee\u9970\u7b26\u65f6\uff0c\u4f20\u9012\u7ed9\u5b50\u7ec4\u4ef6\u7684props\u5e94\u8be5\u662f\u53ef\u53d8\u7684\u3002\u5982\u679c\u7236\u7ec4\u4ef6\u4f20\u9012\u7684props\u662f\u6765\u81eaVuex store\u6216props\u662f\u4e00\u4e2a\u8ba1\u7b97\u5c5e\u6027\uff0c\u90a3\u4e48\u5c1d\u8bd5\u4fee\u6539\u5b83\u4eec\u53ef\u80fd\u4f1a\u5bfc\u81f4\u9519\u8bef\u3002\u8fd9\u662f\u56e0\u4e3aVuex\u7684\u72b6\u6001\u662f\u54cd\u5e94\u5f0f\u7684\uff0c\u76f4\u63a5\u53d8\u66f4\u5b83\u4f1a\u8fdd\u53cdVuex\u7684\u5de5\u4f5c\u539f\u5219\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e0d\u8981\u6ee5\u7528sync\u4fee\u9970\u7b26<\/strong><\/p>\n<\/p>\n<p><p>sync\u4fee\u9970\u7b26\u5e94\u5f53\u8c28\u614e\u4f7f\u7528\uff0c\u4ec5\u5728\u786e\u5b9e\u9700\u8981\u5b9e\u73b0\u53cc\u5411\u7ed1\u5b9a\u7684\u573a\u5408\u3002\u8fc7\u5ea6\u4f7f\u7528\u53ef\u80fd\u4f1a\u5bfc\u81f4\u7ec4\u4ef6\u95f4\u7684\u6570\u636e\u6d41\u53d8\u5f97\u96be\u4ee5\u8ffd\u8e2a\u548c\u7406\u89e3\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u7684\u8be6\u5c3d\u8ba8\u8bba\uff0c\u6211\u4eec\u7406\u89e3\u4e86Vue\u7684sync\u4fee\u9970\u7b26\u7684\u4f7f\u7528\u65b9\u6cd5\u3001\u5de5\u4f5c\u539f\u7406\u3001\u6700\u4f73\u5b9e\u8df5\u4ee5\u53ca\u6ce8\u610f\u4e8b\u9879\u3002\u5b83\u662fVue\u63d0\u4f9b\u7684\u4e00\u4e2a\u975e\u5e38\u5b9e\u7528\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u6709\u6548\u7b80\u5316\u7ec4\u4ef6\u95f4\u7684\u6570\u636e\u540c\u6b65\u9700\u6c42\uff0c\u4f46\u5728\u4f7f\u7528\u65f6\u4e5f\u9700\u8981\u6ce8\u610f\u4e0a\u8ff0\u63d0\u5230\u7684\u4e00\u4e9b\u7ec6\u8282\uff0c\u4ee5\u786e\u4fdd\u5e94\u7528\u7684\u6e05\u6670\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u4e3a\u4ec0\u4e48\u9700\u8981 Vue \u7684 sync \u4fee\u9970\u7b26\uff1f<\/strong><\/p>\n<p>Vue \u7684 sync \u4fee\u9970\u7b26\u662f\u4e3a\u4e86\u89e3\u51b3\u5b50\u7ec4\u4ef6\u4e0e\u7236\u7ec4\u4ef6\u4e4b\u95f4\u6570\u636e\u53cc\u5411\u7ed1\u5b9a\u7684\u95ee\u9898\u3002\u7236\u7ec4\u4ef6\u53ef\u4ee5\u901a\u8fc7 sync \u4fee\u9970\u7b26\u5c06\u81ea\u5df1\u7684\u6570\u636e\u4f20\u9012\u7ed9\u5b50\u7ec4\u4ef6\uff0c\u5e76\u4e14\u5f53\u5b50\u7ec4\u4ef6\u5185\u90e8\u4fee\u6539\u4e86\u8fd9\u4e2a\u6570\u636e\u65f6\uff0c\u8fd9\u4e2a\u6539\u53d8\u4e5f\u4f1a\u88ab\u540c\u6b65\u5230\u7236\u7ec4\u4ef6\u4e0a\u3002<\/p>\n<p><strong>2. \u5982\u4f55\u4f7f\u7528 Vue \u7684 sync \u4fee\u9970\u7b26\uff1f<\/strong><\/p>\n<p>\u4f7f\u7528 Vue \u7684 sync \u4fee\u9970\u7b26\u975e\u5e38\u7b80\u5355\u3002\u9996\u5148\uff0c\u5728\u7236\u7ec4\u4ef6\u4e2d\uff0c\u5c06\u8981\u4f20\u9012\u7ed9\u5b50\u7ec4\u4ef6\u7684\u6570\u636e\u4f5c\u4e3a\u4e00\u4e2a\u5c5e\u6027\u4f20\u5165\uff0c\u5e76\u4e14\u5728\u7ed1\u5b9a\u8fd9\u4e2a\u5c5e\u6027\u65f6\u4f7f\u7528 sync \u4fee\u9970\u7b26\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-html\">&lt;child-component :childData.sync=&quot;parentData&quot;&gt;&lt;\/child-component&gt;\n<\/code><\/pre>\n<p>\u5728\u5b50\u7ec4\u4ef6\u4e2d\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 <code>this.$emit(&#039;update:childData&#039;, newValue)<\/code> \u6765\u66f4\u65b0\u7236\u7ec4\u4ef6\u7684\u6570\u636e\u3002<\/p>\n<p><strong>3. Vue \u7684 sync \u4fee\u9970\u7b26\u4e0e\u53cc\u5411\u7ed1\u5b9a\u7684\u533a\u522b\u662f\u4ec0\u4e48\uff1f<\/strong><\/p>\n<p>Vue \u7684 sync \u4fee\u9970\u7b26\u4e0e\u53cc\u5411\u7ed1\u5b9a\uff08\u4f7f\u7528 v-model\uff09\u7684\u533a\u522b\u5728\u4e8e\uff0csync \u4fee\u9970\u7b26\u66f4\u52a0\u660e\u786e\u5730\u5c06\u6570\u636e\u6d41\u52a8\u7684\u65b9\u5411\u8fdb\u884c\u4e86\u6807\u8bc6\u3002\u4f7f\u7528 sync \u4fee\u9970\u7b26\u65f6\uff0c\u7236\u7ec4\u4ef6\u901a\u8fc7 v-bind.sync \u5c06\u6570\u636e\u4f20\u9012\u7ed9\u5b50\u7ec4\u4ef6\uff0c\u5e76\u901a\u8fc7\u5b50\u7ec4\u4ef6\u7684 $emit \u65b9\u6cd5\u6765\u66f4\u65b0\u7236\u7ec4\u4ef6\u7684\u6570\u636e\u3002\u800c\u53cc\u5411\u7ed1\u5b9a\u66f4\u52a0\u7b80\u6d01\uff0c\u76f4\u63a5\u4f7f\u7528 v-model \u53ef\u4ee5\u5728\u7236\u5b50\u7ec4\u4ef6\u4e4b\u95f4\u5b9e\u73b0\u6570\u636e\u7684\u53cc\u5411\u540c\u6b65\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Vue \u7684 sync \u4fee\u9970\u7b26\u662f\u4e00\u79cd\u4fbf\u6377\u7684\u6570\u636e\u901a\u4fe1\u65b9\u5f0f\uff0c\u5b83\u5141\u8bb8\u5b50\u7ec4\u4ef6\u4fee\u6539\u7ed1\u5b9a\u7684\u7236\u7ec4\u4ef6\u72b6\u6001\u800c\u4e0d\u7528\u53d1\u51fa\u4e8b\u4ef6\u3002\u5b83\u57fa\u4e8e [&hellip;]","protected":false},"author":3,"featured_media":297005,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/296980"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=296980"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/296980\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/297005"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=296980"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=296980"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=296980"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}