{"id":106355,"date":"2026-09-17T08:48:34","date_gmt":"2026-09-17T00:48:34","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/106355.html"},"modified":"2026-09-17T08:48:34","modified_gmt":"2026-09-17T00:48:34","slug":"vue3-%e9%9b%b6%e5%9f%ba%e7%a1%80%e6%af%8f%e6%97%a5%e7%ac%94%e8%ae%b0%ef%bc%88036%ef%bc%89%ef%bc%9a%e5%8a%a8%e6%89%8b%e5%b0%81%e8%a3%85-usetitle-%e4%b8%8e-usetoggle-%e6%9c%80%e5%b0%8f","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/106355.html","title":{"rendered":"Vue3 \u96f6\u57fa\u7840\u6bcf\u65e5\u7b14\u8bb0\uff08036\uff09\uff1a\u52a8\u624b\u5c01\u88c5 useTitle \u4e0e useToggle\u2014\u2014\u6700\u5c0f\u7684\u7ec4\u5408\u5f0f\u51fd\u6570"},"content":{"rendered":"<h3>\u4e00\u3001\u524d\u8a00<\/h3>\n<p>\u4e0a\u7bc7\u61c2\u4e86&#034;\u4e3a\u4ec0\u4e48&#034;&#xff0c;\u672c\u7bc7\u5f00\u59cb&#034;\u52a8\u624b&#034;\u3002\u9009\u4e24\u4e2a\u6700\u7b80\u5355\u7684\u4e0b\u624b&#xff1a;useTitle&#xff08;\u54cd\u5e94\u5f0f\u6539\u7f51\u9875\u6807\u9898&#xff09;\u548c useToggle&#xff08;\u5e03\u5c14\u5f00\u5173&#xff09;\u3002\u9ebb\u96c0\u867d\u5c0f\u4e94\u810f\u4ff1\u5168\u2014\u2014\u8fd4\u56de\u503c\u600e\u4e48\u8bbe\u8ba1\u3001\u521d\u59cb\u503c\u600e\u4e48\u515c\u5e95\u3001\u600e\u4e48\u547d\u540d&#xff0c;\u7ec4\u5408\u5f0f\u51fd\u6570\u7684\u5168\u90e8\u57fa\u7840\u89c4\u8303\u90fd\u5728\u8fd9\u4e24\u4e2a\u5341\u884c\u51fd\u6570\u91cc\u3002<\/p>\n<h3>\u4e8c\u3001useTitle&#xff1a;\u8ba9\u7f51\u9875\u6807\u9898\u53d8\u6210\u54cd\u5e94\u5f0f\u6570\u636e<\/h3>\n<p>\u76ee\u6807 API&#xff1a;const title &#061; useTitle()&#xff0c;\u6539 title.value \u9875\u9762\u6807\u7b7e\u5b9e\u65f6\u53d8\u3002<\/p>\n<p>\u5148\u60f3\u6e05\u695a\u5b9e\u73b0\u4e09\u6b65&#xff1a;\u8bfb\u5f53\u524d\u6807\u9898\u4f5c\u521d\u59cb\u503c \u2192 \u6570\u636e\u53d8\u65f6\u5199\u56de document.title \u2192 \u8fd4\u56de\u6570\u636e\u3002<\/p>\n<p><span class=\"token comment\">\/\/ src\/composables\/useTitle.js<\/span><br \/>\n<span class=\"token keyword\">import<\/span> <span class=\"token punctuation\">{<\/span> ref<span class=\"token punctuation\">,<\/span> watch <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">from<\/span> <span class=\"token string\">&#039;vue&#039;<\/span><\/p>\n<p><span class=\"token keyword\">export<\/span> <span class=\"token keyword\">function<\/span> <span class=\"token function\">useTitle<\/span><span class=\"token punctuation\">(<\/span><span class=\"token parameter\">newTitle<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n  <span class=\"token comment\">\/\/ \u521d\u59cb\u503c&#xff1a;\u4f20\u4e86\u5c31\u7528\u4f20\u7684&#xff0c;\u6ca1\u4f20\u5c31\u8bfb\u5f53\u524d\u6807\u9898<\/span><br \/>\n  <span class=\"token keyword\">const<\/span> title <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">ref<\/span><span class=\"token punctuation\">(<\/span>newTitle <span class=\"token operator\">??<\/span> document<span class=\"token punctuation\">.<\/span>title<span class=\"token punctuation\">)<\/span><\/p>\n<p>  <span class=\"token comment\">\/\/ \u6570\u636e\u53d8 \u2192 \u5199\u56de\u6d4f\u89c8\u5668\u6807\u7b7e<\/span><br \/>\n  <span class=\"token function\">watch<\/span><span class=\"token punctuation\">(<\/span>title<span class=\"token punctuation\">,<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token parameter\">val<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#061;&gt;<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    document<span class=\"token punctuation\">.<\/span>title <span class=\"token operator\">&#061;<\/span> val<br \/>\n  <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token punctuation\">{<\/span> <span class=\"token literal-property property\">immediate<\/span><span class=\"token operator\">:<\/span> <span class=\"token boolean\">true<\/span> <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span>   <span class=\"token comment\">\/\/ immediate&#xff1a;\u8fdb\u6765\u5148\u540c\u6b65\u4e00\u6b21<\/span><\/p>\n<p>  <span class=\"token keyword\">return<\/span> title<br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>&lt;!&#8211; \u4f7f\u7528 &#8211;&gt;<br \/>\n&lt;template&gt;<br \/>\n  &lt;p&gt;\u9875\u9762\u6807\u7b7e\u5b9e\u65f6\u53d8\u5316&#xff1a;&lt;\/p&gt;<br \/>\n  &lt;button &#064;click&#061;&#034;title &#061; &#096;&#xff08;${count} \u6761\u672a\u8bfb&#xff09;\u6211\u7684\u5e94\u7528&#096;&#034;&gt;\u6a21\u62df\u65b0\u6d88\u606f&lt;\/button&gt;<br \/>\n  &lt;button &#064;click&#061;&#034;title &#061; &#039;\u6211\u7684\u5e94\u7528&#039;&#034;&gt;\u6e05\u9664\u672a\u8bfb&lt;\/button&gt;<br \/>\n  &lt;p&gt;{{ count }} \u6761\u672a\u8bfb&lt;\/p&gt;<br \/>\n&lt;\/template&gt;<\/p>\n<p>&lt;script setup&gt;<br \/>\nimport { ref } from &#039;vue&#039;<br \/>\nimport { useTitle } from &#039;&#064;\/composables\/useTitle&#039;<\/p>\n<p>const count &#061; ref(3)<br \/>\nconst title &#061; useTitle(&#039;\u6211\u7684\u5e94\u7528&#039;)<br \/>\n&lt;\/script&gt;<\/p>\n<p>\u8bbe\u8ba1\u590d\u76d8&#xff08;\u6bcf\u4e2a\u7ec6\u8282\u90fd\u6709\u8bb2\u7a76&#xff09;&#xff1a;<\/p>\n<li>newTitle ?? document.title&#xff1a;\u53c2\u6570\u53ef\u9009\u7684\u515c\u5e95\u5199\u6cd5&#xff0c;\u4e24\u79cd\u7528\u6cd5\u90fd\u652f\u6301\u2014\u2014useTitle() \u7eaf\u6258\u7ba1\u3001useTitle(&#039;xxx&#039;) \u5e26\u521d\u59cb\u503c<\/li>\n<li>immediate: true&#xff1a;\u4fdd\u8bc1&#034;\u8fdb\u6765\u90a3\u4e00\u523b&#034;\u6807\u9898\u5c31\u88ab\u540c\u6b65&#xff08;\u6bd4\u5982\u63a5\u7ba1\u4e86\u4e00\u4e2a\u5df2\u7ecf\u4e71\u4e03\u516b\u7cdf\u7684\u6807\u9898&#xff09;<\/li>\n<li>\u8fd4\u56de ref \u800c\u4e0d\u662f\u5b57\u7b26\u4e32&#xff1a;\u8fd4\u56de ref \u8c03\u7528\u65b9\u624d\u80fd\u6539\u3001\u9875\u9762\u624d\u54cd\u5e94\u2014\u2014\u7ec4\u5408\u5f0f\u51fd\u6570\u8fd4\u56de\u7684\u4e00\u5f8b\u662f&#034;\u6d3b\u7684\u6570\u636e&#034;<\/li>\n<p>\u3010\u622a\u56fe\u4f4d\u7f6e&#xff1a;\u70b9\u51fb\u6309\u94ae\u540e\u6d4f\u89c8\u5668\u6807\u7b7e\u9875\u6807\u9898\u53d8\u5316\u7684\u6548\u679c\u3011<\/p>\n<h3>\u4e09\u3001useToggle&#xff1a;\u5e03\u5c14\u5f00\u5173 &#043; \u4efb\u610f\u4e24\u503c\u5207\u6362<\/h3>\n<p>\u76ee\u6807 API&#xff1a;const [state, toggle] &#061; useToggle()\u2014\u2014\u6570\u7ec4\u89e3\u6784\u7684\u8fd4\u56de\u5f62\u5f0f&#xff08;\u548c 012 \u7bc7 toRefs \u7684\u5bf9\u8c61\u5f62\u5f0f\u5e76\u5217&#xff0c;\u4e24\u79cd\u90fd\u8981\u4f1a&#xff09;\u3002<\/p>\n<p><span class=\"token comment\">\/\/ src\/composables\/useToggle.js<\/span><br \/>\n<span class=\"token keyword\">import<\/span> <span class=\"token punctuation\">{<\/span> ref <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">from<\/span> <span class=\"token string\">&#039;vue&#039;<\/span><\/p>\n<p><span class=\"token keyword\">export<\/span> <span class=\"token keyword\">function<\/span> <span class=\"token function\">useToggle<\/span><span class=\"token punctuation\">(<\/span><span class=\"token parameter\">init <span class=\"token operator\">&#061;<\/span> <span class=\"token boolean\">false<\/span><\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n  <span class=\"token keyword\">const<\/span> state <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">ref<\/span><span class=\"token punctuation\">(<\/span>init<span class=\"token punctuation\">)<\/span><\/p>\n<p>  <span class=\"token comment\">\/**<br \/>\n   * \u5207\u6362\u72b6\u6001<br \/>\n   * &#064;param {boolean} [value] \u53ef\u9009&#xff1a;\u76f4\u63a5\u6307\u5b9a\u76ee\u6807\u503c<br \/>\n   *\/<\/span><br \/>\n  <span class=\"token keyword\">function<\/span> <span class=\"token function\">toggle<\/span><span class=\"token punctuation\">(<\/span><span class=\"token parameter\">value<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u4f20\u4e86\u503c\u5c31\u7528\u4f20\u7684&#xff0c;\u6ca1\u4f20\u5c31\u53d6\u53cd<\/span><br \/>\n    state<span class=\"token punctuation\">.<\/span>value <span class=\"token operator\">&#061;<\/span> value <span class=\"token operator\">??<\/span> <span class=\"token operator\">!<\/span>state<span class=\"token punctuation\">.<\/span>value<br \/>\n  <span class=\"token punctuation\">}<\/span><\/p>\n<p>  <span class=\"token keyword\">return<\/span> <span class=\"token punctuation\">[<\/span>state<span class=\"token punctuation\">,<\/span> toggle<span class=\"token punctuation\">]<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>&lt;template&gt;<br \/>\n  &lt;!&#8211; \u573a\u666f 1&#xff1a;\u5f00\u5173\u5f39\u7a97 &#8211;&gt;<br \/>\n  &lt;button &#064;click&#061;&#034;toggle()&#034;&gt;\u5f39\u7a97\u5f00\u5173&#xff1a;{{ open ? &#039;\u5f00&#039; : &#039;\u5173&#039; }}&lt;\/button&gt;<\/p>\n<p>  &lt;!&#8211; \u573a\u666f 2&#xff1a;\u660e\u6587\/\u5bc6\u6587\u5207\u6362&#xff0c;\u76f4\u63a5\u6307\u5b9a\u503c &#8211;&gt;<br \/>\n  &lt;input :type&#061;&#034;showPwd ? &#039;text&#039; : &#039;password&#039;&#034; v-model&#061;&#034;pwd&#034; \/&gt;<br \/>\n  &lt;button &#064;click&#061;&#034;toggle(true)&#034;&gt;\u663e\u793a\u660e\u6587&lt;\/button&gt;<br \/>\n  &lt;button &#064;click&#061;&#034;toggle(false)&#034;&gt;\u9690\u85cf&lt;\/button&gt;<br \/>\n&lt;\/template&gt;<\/p>\n<p>&lt;script setup&gt;<br \/>\nimport { useToggle } from &#039;&#064;\/composables\/useToggle&#039;<\/p>\n<p>const [open, toggle] &#061; useToggle()          \/\/ \u5f39\u7a97\u5f00\u5173<br \/>\nconst [showPwd, setPwdVisible] &#061; useToggle(false)  \/\/ \u5bc6\u7801\u53ef\u89c1\u6027<br \/>\nconst pwd &#061; ref(&#039;&#039;)<br \/>\n&lt;\/script&gt;<\/p>\n<p>\u8bbe\u8ba1\u590d\u76d8&#xff1a;<\/p>\n<li>\u8fd4\u56de\u6570\u7ec4 [state, toggle]&#xff1a;\u8c03\u7528\u65b9\u89e3\u6784\u65f6\u53ef\u4ee5\u968f\u610f\u91cd\u547d\u540d&#xff08;toggle \/ setPwdVisible&#xff09;&#xff0c;\u6570\u7ec4\u6309\u4f4d\u7f6e\u5bf9\u53f7&#xff0c;\u7279\u522b\u9002\u5408&#034;\u4e00\u6570\u636e\u4e00\u65b9\u6cd5&#034;\u7684\u5c0f\u51fd\u6570<\/li>\n<li>value ?? !state.value&#xff1a;toggle() \u65e0\u53c2\u53d6\u53cd\u3001toggle(true) \u5b9a\u70b9\u8bbe\u7f6e&#xff0c;\u4e00\u4e2a\u51fd\u6570\u4e24\u79cd\u7528\u6cd5<\/li>\n<li>JSDoc \u6ce8\u91ca&#xff1a;\u5199\u4e0a\u53c2\u6570\u8bf4\u660e&#xff0c;\u540c\u4e8b\u7528\u4f60\u7684\u51fd\u6570\u65f6 IDE \u6709\u63d0\u793a\u2014\u2014\u534f\u4f5c\u4ece\u6ce8\u91ca\u5f00\u59cb<\/li>\n<p>\u5bf9\u8c61\u5f62\u5f0f\u8fd4\u56de&#xff08;\u9879\u76ee\u91cc\u7edf\u4e00\u98ce\u683c\u65f6\u7528&#xff09;&#xff1a;<\/p>\n<p><span class=\"token comment\">\/\/ \u540c\u4e00\u903b\u8f91\u7684\u53e6\u4e00\u79cd\u8fd4\u56de\u5f62\u6001<\/span><br \/>\n<span class=\"token keyword\">return<\/span> <span class=\"token punctuation\">{<\/span> state<span class=\"token punctuation\">,<\/span> toggle <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token comment\">\/\/ \u4f7f\u7528&#xff1a;const { state, toggle } &#061; useToggle()<\/span><\/p>\n<p>\u6570\u7ec4\u5f62\u5f0f\u9002\u5408&#034;\u5143\u7d20\u5c11\u4e14\u4f4d\u7f6e\u56fa\u5b9a&#034;&#xff1b;\u5bf9\u8c61\u5f62\u5f0f\u9002\u5408\u6210\u5458\u591a\u3001\u8981\u6309\u540d\u53d6\u7528\u7684\u573a\u666f\u3002VueUse \u4e24\u79cd\u6df7\u7528&#xff08;\u7b80\u5355\u51fd\u6570\u7528\u6570\u7ec4&#xff0c;\u590d\u6742\u7684\u7528\u5bf9\u8c61&#xff09;&#xff0c;\u4f60\u7684\u9879\u76ee\u5b9a\u6b7b\u4e00\u79cd\u5373\u53ef\u3002<\/p>\n<h3>\u56db\u3001\u8fdb\u9636&#xff1a;\u628a\u4e24\u4e2a\u51fd\u6570\u7ec4\u5408\u8d77\u6765<\/h3>\n<p>\u7ec4\u5408\u5f0f\u51fd\u6570\u7684\u7cbe\u9ad3\u662f\u53ef\u4ee5\u4e92\u76f8\u7ec4\u5408\u3002\u5199\u4e00\u4e2a useDisclosure&#xff08;\u5f39\u7a97\u63a7\u5236\u5668&#xff09;&#xff0c;\u5185\u90e8\u7528 useToggle&#xff1a;<\/p>\n<p><span class=\"token comment\">\/\/ src\/composables\/useDisclosure.js<\/span><br \/>\n<span class=\"token keyword\">import<\/span> <span class=\"token punctuation\">{<\/span> useToggle <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">from<\/span> <span class=\"token string\">&#039;.\/useToggle&#039;<\/span><\/p>\n<p><span class=\"token comment\">\/**<br \/>\n * \u5f39\u7a97\/\u62bd\u5c49\u63a7\u5236\u5668<br \/>\n * &#064;returns {{ isOpen: import(&#039;vue&#039;).Ref&lt;boolean&gt;, open: Function, close: Function, toggle: Function }}<br \/>\n *\/<\/span><br \/>\n<span class=\"token keyword\">export<\/span> <span class=\"token keyword\">function<\/span> <span class=\"token function\">useDisclosure<\/span><span class=\"token punctuation\">(<\/span><span class=\"token parameter\">init <span class=\"token operator\">&#061;<\/span> <span class=\"token boolean\">false<\/span><\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n  <span class=\"token keyword\">const<\/span> <span class=\"token punctuation\">[<\/span>isOpen<span class=\"token punctuation\">,<\/span> rawToggle<span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">useToggle<\/span><span class=\"token punctuation\">(<\/span>init<span class=\"token punctuation\">)<\/span><\/p>\n<p>  <span class=\"token keyword\">return<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    isOpen<span class=\"token punctuation\">,<\/span><br \/>\n    <span class=\"token function-variable function\">open<\/span><span class=\"token operator\">:<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#061;&gt;<\/span> <span class=\"token function\">rawToggle<\/span><span class=\"token punctuation\">(<\/span><span class=\"token boolean\">true<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><br \/>\n    <span class=\"token function-variable function\">close<\/span><span class=\"token operator\">:<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#061;&gt;<\/span> <span class=\"token function\">rawToggle<\/span><span class=\"token punctuation\">(<\/span><span class=\"token boolean\">false<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><br \/>\n    <span class=\"token function-variable function\">toggle<\/span><span class=\"token operator\">:<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#061;&gt;<\/span> <span class=\"token function\">rawToggle<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n  <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>&lt;template&gt;<br \/>\n  &lt;button &#064;click&#061;&#034;open&#034;&gt;\u6253\u5f00\u786e\u8ba4\u5f39\u7a97&lt;\/button&gt;<\/p>\n<p>  &lt;div v-if&#061;&#034;isOpen&#034; class&#061;&#034;mask&#034; &#064;click.self&#061;&#034;close&#034;&gt;<br \/>\n    &lt;div class&#061;&#034;box&#034;&gt;<br \/>\n      \u786e\u5b9a\u8981\u5220\u9664\u5417&#xff1f;<br \/>\n      &lt;button &#064;click&#061;&#034;close&#034;&gt;\u53d6\u6d88&lt;\/button&gt;<br \/>\n      &lt;button &#064;click&#061;&#034;onConfirm&#034;&gt;\u786e\u5b9a&lt;\/button&gt;<br \/>\n    &lt;\/div&gt;<br \/>\n  &lt;\/div&gt;<br \/>\n&lt;\/template&gt;<\/p>\n<p>&lt;script setup&gt;<br \/>\nimport { useDisclosure } from &#039;&#064;\/composables\/useDisclosure&#039;<\/p>\n<p>const { isOpen, open, close } &#061; useDisclosure()<\/p>\n<p>function onConfirm() {<br \/>\n  console.log(&#039;\u6267\u884c\u5220\u9664&#039;)<br \/>\n  close()<br \/>\n}<br \/>\n&lt;\/script&gt;<\/p>\n<p>&lt;style scoped&gt;<br \/>\n.mask { position: fixed; inset: 0; background: rgba(0,0,0,.5);<br \/>\n  display: flex; align-items: center; justify-content: center; }<br \/>\n.box { background: #fff; padding: 20px; border-radius: 8px; }<br \/>\n&lt;\/style&gt;<\/p>\n<p>open \/ close \/ toggle \u8bed\u4e49\u5206\u660e\u7684\u5f39\u7a97\u63a7\u5236\u5668&#xff0c;\u4e00\u4e2a\u9879\u76ee\u91cc\u6bcf\u4e2a\u5f39\u7a97\u90fd\u7528\u5b83\u2014\u2014\u8fd9\u5c31\u662f 031 \u7bc7 Teleport \u5f39\u7a97\u7684\u5b98\u65b9\u642d\u6863\u3002\u51fd\u6570\u8c03\u7528\u51fd\u6570&#xff0c;\u80fd\u529b\u4e0d\u65ad\u53e0\u52a0&#xff0c;\u8fd9\u5c31\u662f&#034;\u7ec4\u5408\u5f0f&#034;\u4e09\u4e2a\u5b57\u7684\u542b\u4e49\u3002<\/p>\n<p>\u3010\u622a\u56fe\u4f4d\u7f6e&#xff1a;\u5f39\u7a97\u6253\u5f00\u3001\u70b9\u906e\u7f69\u5173\u95ed\u7684\u8fd0\u884c\u6548\u679c\u3011<\/p>\n<h3>\u4e94\u3001\u8e29\u5751\u8bb0\u5f55<\/h3>\n<li>useTitle \u5728 SSR\/\u65e0 window \u73af\u5883\u62a5\u9519&#xff1a;document \u4e0d\u5b58\u5728\u2014\u2014\u9700\u8981\u517c\u5bb9 SSR \u65f6\u52a0 typeof document !&#061;&#061; &#039;undefined&#039; \u5224\u65ad&#xff08;\u672c\u7cfb\u5217\u8d70 SPA&#xff0c;\u5148\u77e5\u9053\u6709\u8fd9\u56de\u4e8b&#xff09;<\/li>\n<li>\u8fd4\u56de\u4e86\u6b7b\u503c&#xff1a;return title.value \u8fd4\u56de\u7684\u662f\u5f53\u65f6\u7684\u5b57\u7b26\u4e32&#xff0c;\u8c03\u7528\u65b9\u600e\u4e48\u6539\u9875\u9762\u90fd\u4e0d\u52a8\u2014\u2014\u6c38\u8fdc\u8fd4\u56de ref<\/li>\n<li>toggle \u91cc\u7684\u5751&#xff1a;value ?? !state.value \u82e5\u5199\u6210 value || !state.value&#xff0c;\u4f20 false \u4f1a\u88ab || \u5f53 falsy \u541e\u6389\u53d8\u6210\u53d6\u53cd\u2014\u2014nullish \u5408\u5e76\u7b26&#xff08;??&#xff09;\u624d\u533a\u5206 false \u548c\u7a7a<\/li>\n<li>\u5fd8\u8bb0\u53ef\u9009\u62ec\u53f7&#xff1a;useToggle \u5b9a\u4e49\u6210 function useToggle(init) \u4f46\u8c03\u7528\u5199 useToggle()\u2014\u2014init \u662f undefined&#xff0c;ref(undefined) \u7684\u5e03\u5c14\u5224\u65ad\u662f false&#xff0c;\u6070\u597d\u80fd\u7528\u4f46\u7c7b\u578b\u4e0d\u5e72\u51c0&#xff0c;\u517b\u6210 init &#061; false \u7ed9\u9ed8\u8ba4\u503c\u7684\u4e60\u60ef<\/li>\n<li>\u4e00\u4e2a\u6587\u4ef6\u585e\u5341\u4e2a\u51fd\u6570&#xff1a;useTitle \u548c useToggle \u6df7\u5728\u4e00\u4e2a\u6587\u4ef6\u91cc export\u2014\u2014\u4e00\u4e2a\u51fd\u6570\u4e00\u4e2a\u6587\u4ef6&#xff0c;index.js \u7edf\u4e00\u5bfc\u51fa&#xff0c;\u6309\u9700 import<\/li>\n<h3>\u516d\u3001\u4eca\u65e5\u5c0f\u7ed3<\/h3>\n<ul>\n<li>useTitle&#xff1a;ref &#043; watch(immediate) \u4e09\u4ef6\u5957&#xff0c;\u8fd4\u56de\u6d3b ref \u662f\u94c1\u5f8b<\/li>\n<li>useToggle&#xff1a;[state, toggle] \u6570\u7ec4\u8fd4\u56de\u4fbf\u4e8e\u91cd\u547d\u540d&#xff1b;value ?? !value \u517c\u5bb9\u5b9a\u70b9\u8bbe\u7f6e<\/li>\n<li>\u8fd4\u56de\u5f62\u6001\u4e8c\u9009\u4e00&#xff1a;\u6570\u7ec4&#xff08;\u5c0f\u800c\u56fa\u5b9a&#xff09;\/ \u5bf9\u8c61&#xff08;\u6210\u5458\u591a&#xff09;&#xff0c;\u9879\u76ee\u5185\u7edf\u4e00<\/li>\n<li>useDisclosure \u793a\u8303\u51fd\u6570\u7ec4\u5408\u51fd\u6570\u2014\u2014&#034;\u7ec4\u5408\u5f0f&#034;\u7684\u771f\u6b63\u542b\u4e49<\/li>\n<\/ul>\n<h3>\u4e0b\u7bc7\u9884\u544a<\/h3>\n<p>\u7ee7\u7eed\u9020\u4e0b\u4e00\u7ec4&#xff1a;useMouse \/ useWindowSize\u2014\u2014\u7b2c\u4e00\u6b21\u5728\u7ec4\u5408\u5f0f\u51fd\u6570\u91cc\u64cd\u4f5c window \u4e8b\u4ef6\u76d1\u542c&#xff0c;\u751f\u547d\u5468\u671f\u6e05\u7406\u7684\u6a21\u677f\u5199\u6cd5\u4f1a\u56fa\u5316\u6210\u808c\u8089\u8bb0\u5fc6\u3002\u4e0b\u4e00\u7bc7 037\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u524d\u8a00<br \/>\n\u4e0a\u7bc7\u61c2\u4e86\\&#8221;\u4e3a\u4ec0\u4e48\\&#8221;&#xff0c;\u672c\u7bc7\u5f00\u59cb\\&#8221;\u52a8\u624b\\&#8221;\u3002\u9009\u4e24\u4e2a\u6700\u7b80\u5355\u7684\u4e0b\u624b&#xff1a;useTitle&#xff08;\u54cd\u5e94\u5f0f\u6539\u7f51\u9875\u6807\u9898&#xff09;\u548c useToggle&#xff08;\u5e03\u5c14\u5f00\u5173&#xff09;\u3002\u9ebb\u96c0\u867d\u5c0f\u4e94\u810f\u4ff1\u5168\u2014\u2014\u8fd4\u56de\u503c\u600e\u4e48\u8bbe\u8ba1\u3001\u521d\u59cb\u503c\u600e\u4e48\u515c\u5e95\u3001\u600e\u4e48\u547d\u540d&#xff0c;\u7ec4\u5408\u5f0f\u51fd\u6570\u7684\u5168\u90e8\u57fa\u7840\u89c4\u8303\u90fd\u5728\u8fd9\u4e24\u4e2a\u5341\u884c\u51fd\u6570\u91cc\u3002<br \/>\n\u4e8c\u3001useTitle&#xff1a;\u8ba9\u7f51\u9875\u6807\u9898\u53d8\u6210\u54cd\u5e94\u5f0f\u6570\u636e<br \/>\n\u76ee\u6807 API&#xff1a;const title  useTitle<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[12076,187,625],"topic":[],"class_list":["post-106355","post","type-post","status-publish","format-standard","hentry","category-server","tag-vue--","tag-javascript","tag-vue-js"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Vue3 \u96f6\u57fa\u7840\u6bcf\u65e5\u7b14\u8bb0\uff08036\uff09\uff1a\u52a8\u624b\u5c01\u88c5 useTitle \u4e0e useToggle\u2014\u2014\u6700\u5c0f\u7684\u7ec4\u5408\u5f0f\u51fd\u6570 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/106355.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Vue3 \u96f6\u57fa\u7840\u6bcf\u65e5\u7b14\u8bb0\uff08036\uff09\uff1a\u52a8\u624b\u5c01\u88c5 useTitle \u4e0e useToggle\u2014\u2014\u6700\u5c0f\u7684\u7ec4\u5408\u5f0f\u51fd\u6570 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u4e00\u3001\u524d\u8a00 \u4e0a\u7bc7\u61c2\u4e86&quot;\u4e3a\u4ec0\u4e48&quot;&#xff0c;\u672c\u7bc7\u5f00\u59cb&quot;\u52a8\u624b&quot;\u3002\u9009\u4e24\u4e2a\u6700\u7b80\u5355\u7684\u4e0b\u624b&#xff1a;useTitle&#xff08;\u54cd\u5e94\u5f0f\u6539\u7f51\u9875\u6807\u9898&#xff09;\u548c useToggle&#xff08;\u5e03\u5c14\u5f00\u5173&#xff09;\u3002\u9ebb\u96c0\u867d\u5c0f\u4e94\u810f\u4ff1\u5168\u2014\u2014\u8fd4\u56de\u503c\u600e\u4e48\u8bbe\u8ba1\u3001\u521d\u59cb\u503c\u600e\u4e48\u515c\u5e95\u3001\u600e\u4e48\u547d\u540d&#xff0c;\u7ec4\u5408\u5f0f\u51fd\u6570\u7684\u5168\u90e8\u57fa\u7840\u89c4\u8303\u90fd\u5728\u8fd9\u4e24\u4e2a\u5341\u884c\u51fd\u6570\u91cc\u3002 \u4e8c\u3001useTitle&#xff1a;\u8ba9\u7f51\u9875\u6807\u9898\u53d8\u6210\u54cd\u5e94\u5f0f\u6570\u636e \u76ee\u6807 API&#xff1a;const title useTitle\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/106355.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-09-17T00:48:34+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/106355.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/106355.html\",\"name\":\"Vue3 \u96f6\u57fa\u7840\u6bcf\u65e5\u7b14\u8bb0\uff08036\uff09\uff1a\u52a8\u624b\u5c01\u88c5 useTitle \u4e0e useToggle\u2014\u2014\u6700\u5c0f\u7684\u7ec4\u5408\u5f0f\u51fd\u6570 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-09-17T00:48:34+00:00\",\"dateModified\":\"2026-09-17T00:48:34+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/106355.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/106355.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/106355.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Vue3 \u96f6\u57fa\u7840\u6bcf\u65e5\u7b14\u8bb0\uff08036\uff09\uff1a\u52a8\u624b\u5c01\u88c5 useTitle \u4e0e useToggle\u2014\u2014\u6700\u5c0f\u7684\u7ec4\u5408\u5f0f\u51fd\u6570\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Vue3 \u96f6\u57fa\u7840\u6bcf\u65e5\u7b14\u8bb0\uff08036\uff09\uff1a\u52a8\u624b\u5c01\u88c5 useTitle \u4e0e useToggle\u2014\u2014\u6700\u5c0f\u7684\u7ec4\u5408\u5f0f\u51fd\u6570 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/106355.html","og_locale":"zh_CN","og_type":"article","og_title":"Vue3 \u96f6\u57fa\u7840\u6bcf\u65e5\u7b14\u8bb0\uff08036\uff09\uff1a\u52a8\u624b\u5c01\u88c5 useTitle \u4e0e useToggle\u2014\u2014\u6700\u5c0f\u7684\u7ec4\u5408\u5f0f\u51fd\u6570 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u4e00\u3001\u524d\u8a00 \u4e0a\u7bc7\u61c2\u4e86\"\u4e3a\u4ec0\u4e48\"&#xff0c;\u672c\u7bc7\u5f00\u59cb\"\u52a8\u624b\"\u3002\u9009\u4e24\u4e2a\u6700\u7b80\u5355\u7684\u4e0b\u624b&#xff1a;useTitle&#xff08;\u54cd\u5e94\u5f0f\u6539\u7f51\u9875\u6807\u9898&#xff09;\u548c useToggle&#xff08;\u5e03\u5c14\u5f00\u5173&#xff09;\u3002\u9ebb\u96c0\u867d\u5c0f\u4e94\u810f\u4ff1\u5168\u2014\u2014\u8fd4\u56de\u503c\u600e\u4e48\u8bbe\u8ba1\u3001\u521d\u59cb\u503c\u600e\u4e48\u515c\u5e95\u3001\u600e\u4e48\u547d\u540d&#xff0c;\u7ec4\u5408\u5f0f\u51fd\u6570\u7684\u5168\u90e8\u57fa\u7840\u89c4\u8303\u90fd\u5728\u8fd9\u4e24\u4e2a\u5341\u884c\u51fd\u6570\u91cc\u3002 \u4e8c\u3001useTitle&#xff1a;\u8ba9\u7f51\u9875\u6807\u9898\u53d8\u6210\u54cd\u5e94\u5f0f\u6570\u636e \u76ee\u6807 API&#xff1a;const title useTitle","og_url":"https:\/\/www.wsisp.com\/helps\/106355.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-09-17T00:48:34+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"3 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/106355.html","url":"https:\/\/www.wsisp.com\/helps\/106355.html","name":"Vue3 \u96f6\u57fa\u7840\u6bcf\u65e5\u7b14\u8bb0\uff08036\uff09\uff1a\u52a8\u624b\u5c01\u88c5 useTitle \u4e0e useToggle\u2014\u2014\u6700\u5c0f\u7684\u7ec4\u5408\u5f0f\u51fd\u6570 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-09-17T00:48:34+00:00","dateModified":"2026-09-17T00:48:34+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/106355.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/106355.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/106355.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Vue3 \u96f6\u57fa\u7840\u6bcf\u65e5\u7b14\u8bb0\uff08036\uff09\uff1a\u52a8\u624b\u5c01\u88c5 useTitle \u4e0e useToggle\u2014\u2014\u6700\u5c0f\u7684\u7ec4\u5408\u5f0f\u51fd\u6570"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/106355","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=106355"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/106355\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=106355"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=106355"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=106355"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=106355"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}