{"id":2898,"date":"2025-10-26T10:23:11","date_gmt":"2025-10-26T02:23:11","guid":{"rendered":"https:\/\/www.xfan.top\/?p=2898"},"modified":"2025-11-13T15:43:45","modified_gmt":"2025-11-13T07:43:45","slug":"vue%e8%b7%af%e7%94%b1%e5%88%87%e6%8d%a2%e9%a1%b5%e9%9d%a2%e8%87%aa%e5%ae%9a%e4%b9%89%e5%8a%a8%e7%94%bb%e6%95%88%e6%9e%9c","status":"publish","type":"post","link":"https:\/\/www.xfan.top\/index.php\/2025\/10\/26\/2898\/","title":{"rendered":"VUE\u8def\u7531\u5207\u6362\u9875\u9762\u81ea\u5b9a\u4e49\u52a8\u753b\u6548\u679c"},"content":{"rendered":"<h3>\u6548\u679c\u6f14\u793a<\/h3>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/www.xfan.top\/wp-content\/uploads\/2025\/10\/SHANAab.gif'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/www.xfan.top\/wp-content\/uploads\/2025\/10\/SHANAab.gif\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" \/><\/div><\/p>\n<h3>\u4f7f\u7528\u7684API\u548c\u7c7b\u5e93<\/h3>\n<p><code>Transition<\/code>\u3001<code>router-view<\/code><br \/>\n\u53ef\u9009\uff1a<code>keep-alive<\/code>\u3001<code>pinia<\/code>\u7b49<\/p>\n<h3>\u5173\u952e\u4ee3\u7801<\/h3>\n<pre><code>&lt;template&gt;\n&lt;router-view v-slot=&quot;{ Component }&quot;&gt;\n    &lt;transition :name=&quot;transitionName&quot;&gt;\n        &lt;component :is=&quot;Component&quot; \/&gt;\n    &lt;\/transition&gt;\n&lt;\/router-view&gt;\n&lt;\/template&gt;\n&lt;script setup&gt;\nimport { ref } from &#039;vue&#039;\nconst transitionName=ref(&#039;&#039;)\nwindow.will_push = () =&gt; {\n  transitionName.value=&#039;forward&#039;\n}\nwindow.will_back = () =&gt; {\n  transitionName.value=&#039;back&#039;\n}\n&lt;\/script&gt;\n&lt;style&gt;\n\/* ================= \u524d\u8fdb\u52a8\u753b ================= *\/\n\/* \u8fdb\u5165\u7684\u5143\u7d20\u52a8\u753b\u6267\u884c\u65f6\u6837\u5f0f *\/\n.forward-enter-active {\n\n}\n\/* \u8fdb\u5165\u7684\u5143\u7d20\u52a8\u753b\u521d\u59cb\u6837\u5f0f *\/\n.forward-enter-from {\n\n}\n\/* \u8fdb\u5165\u7684\u5143\u7d20\u52a8\u753b\u7ed3\u675f\u65f6\u6837\u5f0f *\/\n.forward-enter-to {\n\n}\n\/* \u79bb\u5f00\u7684\u5143\u7d20\u52a8\u753b\u6267\u884c\u65f6\u6837\u5f0f *\/\n.forward-leave-active {\n\n}\n\/* \u79bb\u5f00\u7684\u5143\u7d20\u52a8\u753b\u521d\u59cb\u6837\u5f0f *\/\n.forward-leave-from {\n\n}\n\/* \u79bb\u5f00\u7684\u5143\u7d20\u52a8\u753b\u7ed3\u675f\u65f6\u6837\u5f0f *\/\n.forward-leave-to {\n\n}\n\n\/* ================= \u540e\u9000\u52a8\u753b ================= *\/\n\/* \u8fdb\u5165\u7684\u5143\u7d20\u52a8\u753b\u6267\u884c\u65f6\u6837\u5f0f *\/\n.back-enter-active {\n\n}\n\/* \u8fdb\u5165\u7684\u5143\u7d20\u52a8\u753b\u521d\u59cb\u6837\u5f0f *\/\n.back-enter-from {\n\n}\n\/* \u8fdb\u5165\u7684\u5143\u7d20\u52a8\u753b\u7ed3\u675f\u65f6\u6837\u5f0f *\/\n.back-enter-to {\n\n}\n\/* \u79bb\u5f00\u7684\u5143\u7d20\u52a8\u753b\u6267\u884c\u65f6\u6837\u5f0f *\/\n.back-leave-active {\n\n}\n\/* \u79bb\u5f00\u7684\u5143\u7d20\u52a8\u753b\u521d\u59cb\u6837\u5f0f *\/\n.back-leave-from {\n\n}\n\/* \u79bb\u5f00\u7684\u5143\u7d20\u52a8\u753b\u7ed3\u675f\u65f6\u6837\u5f0f *\/\n.back-leave-to {\n\n}\n&lt;\/style&gt;<\/code><\/pre>\n<p><a href=\"https:\/\/cn.vuejs.org\/guide\/built-ins\/transition#css-based-transitions\" title=\"\u5173\u4e8e\u6837\u5f0f\u7684\u5b9a\u4e49\">\u5173\u4e8e\u6837\u5f0f\u7684\u5b9a\u4e49<\/a><\/p>\n<h3>\u8bbe\u7f6e\u3001\u89e6\u53d1\u52a8\u753b\u7684\u65b9\u6cd5<\/h3>\n<p>\u5728\u9700\u8981\u6dfb\u52a0\u52a8\u753b\u7684\u8def\u7531\u5207\u6362\u65f6\uff0c\u5148\u8c03\u7528<code>window.will_push()<\/code>\u6216<code>window.will_back()<\/code>\uff0c\u518d\u8c03\u7528<code>router.push()<\/code>\u6216<code>router.back()<\/code>\uff0c\u5373\u53ef\u7a33\u5b9a\u3001\u7075\u6d3b\u5730\u89e6\u53d1\u5207\u6362\u52a8\u753b\u3002<\/p>\n<h3>\u4e0d\u5728<code>router.beforeEach<\/code>\u4e2d\u6539\u53d8<code>transitionName<\/code>\u503c\u7684\u539f\u56e0<\/h3>\n<p>\u89e6\u53d1\u8def\u7531\u8df3\u8f6c\u65f6\u7684\u6267\u884c\u6d41\u7a0b\u5982\u4e0b\uff1a<\/p>\n<pre><code>\u540c\u6b65\u4efb\u52a1 (Tick 1):\n\u2193 \u7528\u6237\u70b9\u51fb\u8def\u7531\u94fe\u63a5 \/ router.push()\n\u2193 beforeEach \u5b88\u536b\u6267\u884c \u2190 \u4fee\u6539 ref\uff08\u503c\u53d8\u4e86\uff0c\u4f46 DOM \u672a\u66f4\u65b0\uff09\n\u2193 beforeRouteLeave \u5b88\u536b\n\u2193 beforeRouteEnter \u5b88\u536b\n\u2193 \u8def\u7531\u786e\u8ba4\u5b8c\u6210\n\n\u5fae\u4efb\u52a1\u961f\u5217 (Tick 1 \u7ed3\u675f\u540e):\n\u2193 transitionName \u7684\u54cd\u5e94\u5f0f\u66f4\u65b0\u89e6\u53d1\n\u2193 \u8ba1\u7b97\u5c5e\u6027\u91cd\u65b0\u8ba1\u7b97\n\u2193 \u51c6\u5907\u7ec4\u4ef6\u66f4\u65b0\n\n\u4e0b\u4e00\u4e2a Tick (Tick 2):\n\u2193 &lt;router-view&gt; \u5f00\u59cb\u5378\u8f7d\u65e7\u7ec4\u4ef6\n\u2193 &lt;Transition&gt; \u7ec4\u4ef6\u521d\u59cb\u5316\uff0c\u8bfb\u53d6 props\uff08\u5305\u62ec name\uff09\n\u2193 \u6b64\u65f6\u624d\u7528\u5230 transitionName \u7684&quot;\u65b0\u503c&quot;<\/code><\/pre>\n<p>\u95ee\u9898\u5728\u4e8e\uff1aTransition \u53ef\u80fd\u5728\u524d\u4e00\u4e2a tick \u5c31\u5df2\u7ecf\u8bfb\u53d6\u4e86\u65e7\u503c\uff0c\u6216\u8005\u66f4\u65b0\u65f6\u673a\u4e0e\u7ec4\u4ef6\u5378\u8f7d\/\u6302\u8f7d\u4e0d\u5339\u914d\u3002\u5c24\u5176\u662f\u65e7\u7ec4\u4ef6\u7684\u9000\u51fa\u52a8\u753b\uff0c\u5f88\u53ef\u80fd\u4e0d\u4f1a\u6b63\u786e\u6267\u884c\u3002<br \/>\n\u8d34\u522b\u662f\u914d\u5408\u4f7f\u7528<code>keep-alive<\/code>\u7684\u60c5\u51b5<\/p>\n<pre><code>&lt;router-view v-slot=&quot;{ Component }&quot;&gt;\n    &lt;transition :name=&quot;transitionName&quot;&gt;\n        &lt;keep-alive :include=&quot;pageKeepAlive.names&quot;&gt;\n            &lt;component :is=&quot;Component&quot; \/&gt;\n        &lt;\/keep-alive&gt;\n    &lt;\/transition&gt;\n&lt;\/router-view&gt;<\/code><\/pre>\n<p>\u5b9e\u9645\u4f7f\u7528\u4e2d\uff0c\u5f88\u53ef\u80fd\u8fd8\u8981\u548c<code>keep-alive<\/code>\u4e00\u5e76\u4f7f\u7528\uff0c\u5e76\u4e14<code>keep-alive<\/code>\u7684<code>include<\/code>\u4e5f\u9700\u8981\u52a8\u6001\u7ed1\u5b9a\uff0c\u8fd9\u79cd\u60c5\u51b5\u66f4\u590d\u6742\u3002\u6240\u4ee5\uff0c\u5b8c\u5168\u624b\u52a8\u63a7\u5236<code>transition<\/code>\u7684<code>name<\/code>\uff0c\u5e76\u5728\u8def\u7531\u8df3\u8f6c\u5f00\u59cb\u524d\u5c31\u4fee\u6539\u597d<code>transitionName<\/code>\u7684\u503c\u662f\u4e00\u79cd\u6bd4\u8f83\u7a33\u59a5\u7684\u529e\u6cd5\u3002\u4e3a\u51cf\u5c11\u4ee3\u7801\u5197\u4f59\uff0c\u53ef\u4ee5\u81ea\u884c\u5c01\u88c5<code>router.push<\/code>\u7b49\u65b9\u6cd5\uff0c\u9884\u7f6e<code>transitionName<\/code>\u5373\u53ef\u3002<\/p>\n<h3>\u5176\u4ed6\u9700\u8981\u6ce8\u610f\u7684\u4e8b\u9879<\/h3>\n<ul>\n<li>\n<p><code>window.will_push<\/code>\u7b49\u6302\u8f7d\u5230\u5168\u5c40\u4e0a\u7684\u65b9\u6cd5\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>pinia<\/code>\u4ee3\u66ff\uff0c<code>transitionName<\/code>\u4f7f\u7528\u8ba1\u7b97\u5c5e\u6027\u5373\u53ef\u3002<\/p>\n<\/li>\n<li>\n<p>\u5728\u52a8\u753b\u6837\u5f0f\u4e2d\uff0c\u5982\u9700\u8981\u5b9e\u73b0\u9875\u9762\u6574\u4f53\u5e73\u79fb\u7b49transform\uff0c\u9700\u8981\u5728<code>v-enter-active<\/code>\u3001<code>v-leave-active<\/code>\u4e2d\u6dfb\u52a0<code>position: absolute !important<\/code>\uff0c\u901a\u8fc7\u7edd\u5bf9\u5b9a\u4f4d\u63a7\u5236\u4f4d\u79fb\u52a8\u753b\u3002<\/p>\n<\/li>\n<li>\n<p>\u5173\u4e8e<code>Transition<\/code>\u7684mode\uff0c<a href=\"https:\/\/cn.vuejs.org\/guide\/built-ins\/transition#transition-modes\" title=\"\u5b98\u65b9\u6587\u6863\">\u5b98\u65b9\u6587\u6863<\/a>\u4e2d\u6709\u8bf4\u660e\uff0c\u9700\u8981\u4e24\u4e2a\u9875\u9762\u540c\u65f6\u5b58\u5728\uff08\u8fdb\u5165\u548c\u9000\u51fa\u52a8\u753b\u540c\u65f6\u6267\u884c\uff09\u7684\u60c5\u51b5\uff0cmode\u65e0\u9700\u8bbe\u7f6e\u4fdd\u6301\u9ed8\u8ba4\u5373\u53ef\uff0c\u53e6\u5916\u7684\u60c5\u51b5\u6309\u9700\u4fee\u6539\u3002<\/p>\n<\/li>\n<li>\n<p><code>useRoute()<\/code>\u4e2d<code>meta<\/code>\u7b49\u54cd\u5e94\u5f0f\u6570\u636e\uff0c\u4f1a\u5728\u8def\u7531\u5207\u6362\u6700\u5f00\u59cb\u7684\u90a3\u4e00\u523b\u5c31\u7acb\u5373\u5207\u6362\u4e3a\u65b0\u9875\u9762\uff08to\uff09\u7684\u5185\u5bb9\u3002\u5982\u679c\u4f60\u7684\u9879\u76ee\u4e2d\u65e7\u9875\u9762\uff08from\uff09\u4e2d\u7684\u67d0\u4e9b\u5143\u7d20\u6216\u5c5e\u6027\u4f9d\u8d56\u4e86<code>useRoute()<\/code>\u4e2d\u7684\u4fe1\u606f\uff0c\u5728\u8def\u7531\u5207\u6362\u5f00\u59cb\u65f6\uff0c\u53ef\u80fd\u4f1a\u4f7f\u65e7\u9875\u9762\u7684\u67d0\u4e9b\u5143\u7d20\u53d1\u751f\u5207\u6362\u6216\u95ea\u70c1\uff0c\u6216\u89e6\u53d1\u9884\u6599\u5916\u7684\u903b\u8f91\uff0c\u6700\u597d\u7684\u529e\u6cd5\u662f\u4f7f\u7528\u989d\u5916\u7684\u53d8\u91cf\u50a8\u5b58\u521d\u59cb\u5316\u65f6\u7684<code>useRoute()<\/code>\u4e2d\u7684\u4fe1\u606f\uff0c\u4e0d\u8981\u76f4\u63a5\u4f9d\u8d56<code>useRoute()<\/code>\u3002<\/p>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u6548\u679c\u6f14\u793a \u4f7f\u7528\u7684API\u548c\u7c7b\u5e93 Transition\u3001router-view \u53ef\u9009\uff1akeep-alive\u3001pin [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22,20],"tags":[],"class_list":["post-2898","post","type-post","status-publish","format-standard","hentry","category-jsts","category-fed"],"_links":{"self":[{"href":"https:\/\/www.xfan.top\/index.php\/wp-json\/wp\/v2\/posts\/2898","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xfan.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xfan.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xfan.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xfan.top\/index.php\/wp-json\/wp\/v2\/comments?post=2898"}],"version-history":[{"count":12,"href":"https:\/\/www.xfan.top\/index.php\/wp-json\/wp\/v2\/posts\/2898\/revisions"}],"predecessor-version":[{"id":2912,"href":"https:\/\/www.xfan.top\/index.php\/wp-json\/wp\/v2\/posts\/2898\/revisions\/2912"}],"wp:attachment":[{"href":"https:\/\/www.xfan.top\/index.php\/wp-json\/wp\/v2\/media?parent=2898"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xfan.top\/index.php\/wp-json\/wp\/v2\/categories?post=2898"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xfan.top\/index.php\/wp-json\/wp\/v2\/tags?post=2898"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}