{"id":44491,"date":"2025-06-19T09:27:41","date_gmt":"2025-06-19T01:27:41","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/44491.html"},"modified":"2025-06-19T09:27:41","modified_gmt":"2025-06-19T01:27:41","slug":"4%e6%ad%a5%e4%bd%bf%e7%94%a8-vue3-%e8%b7%af%e7%94%b1","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/44491.html","title":{"rendered":"4\u6b65\u4f7f\u7528 vue3 \u8def\u7531"},"content":{"rendered":"<p>\u00a0<span style=\"color:#1369b2\">\u8def\u7531\u7684\u57fa\u672c\u4f7f\u7528\u6b65\u9aa4<\/span><span style=\"color:#595959\">\u5206\u4e3a\u4ee5\u4e0b<\/span><span style=\"color:#595959\">4<\/span><span style=\"color:#595959\">\u6b65<\/span><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"521\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250619012739-6853678be6cbf.png\" width=\"1611\" \/><\/p>\n<h3>\u00a0\u7b2c\u4e00\u6b65&#xff1a;\u5b9a\u4e49\u8def\u7531\u7ec4\u4ef6&#xff1a;\u7565<\/h3>\n<h3>\u00a0\u7b2c\u4e8c\u6b65&#xff1a;\u5b9a\u4e49\u8def\u7531\u94fe\u63a5\u548c\u8def\u7531\u89c6\u56fe&#xff1a;<\/h3>\n<p>&lt;template&gt;<br \/>\n  &lt;div class&#061;&#034;app-container&#034;&gt;<br \/>\n    &lt;h1&gt;App\u6839\u7ec4\u4ef6&lt;\/h1&gt;<br \/>\n    &lt;router-link to&#061;&#034;\/home&#034;&gt;\u9996\u9875&lt;\/router-link&gt;<br \/>\n    &lt;router-link to&#061;&#034;\/about&#034;&gt;\u5173\u4e8e&lt;\/router-link&gt;<br \/>\n    &lt;hr&gt;<br \/>\n    &lt;router-view&gt;&lt;\/router-view&gt;<br \/>\n  &lt;\/div&gt;<br \/>\n&lt;\/template&gt;<\/p>\n<p>\u6211\u4eec\u6f14\u793a\u9879\u76ee\u7684\u5b9a\u4e49\u8def\u7531\u94fe\u63a5\u4e0e\u8def\u7531\u89c6\u56fe\u5982\u4e0b&#xff1a;<\/p>\n<p>&lt;template&gt;<br \/>\n&lt;div class&#061;&#034;home_container&#034;&gt;<br \/>\n    &lt;el-container&gt;<\/p>\n<p>&lt;!&#8211;\u5934\u90e8 start&#8211;&gt;<br \/>\n&lt;el-header class&#061;&#034;top-header&#034;&gt;<br \/>\n&lt;el-text class&#061;&#034;home_title&#034;&gt;\u4e1c\u8f6f\u4e91\u533b\u9662HIS\u7cfb\u7edf&lt;\/el-text&gt;<br \/>\n&lt;div class&#061;&#034;home_userinfoContainer&#034; &gt;<br \/>\n &lt;el-dropdown  &#064;command&#061;&#034;handleCommand&#034;&gt;<br \/>\n      &lt;el-button type&#061;&#034;primary&#034;&gt;<br \/>\n&lt;el-avatar size&#061;&#034;small&#034; style&#061;&#034;margin-right:10px ;&#034;<br \/>\n    src&#061;&#034;https:\/\/cube.elemecdn.com\/0\/88\/03b0d39583f48206768a7534e55bcpng.png&#034; \/&gt;<br \/>\n{{userStore.getUserInfo.value.realName}}&lt;el-icon class&#061;&#034;el-icon&#8211;right&#034;&gt;&lt;arrow-down \/&gt;&lt;\/el-icon&gt;<br \/>\n      &lt;\/el-button&gt;<br \/>\n      &lt;template #dropdown&gt;<br \/>\n        &lt;el-dropdown-menu&gt;<br \/>\n          &lt;el-dropdown-item command&#061;&#034;&#034; &gt;\u6211\u7684\u8bbe\u7f6e&lt;\/el-dropdown-item&gt;<br \/>\n          &lt;el-dropdown-item command&#061;&#034;logout&#034;&gt;\u9000\u51fa\u767b\u5f55&lt;\/el-dropdown-item&gt;<br \/>\n        &lt;\/el-dropdown-menu&gt;<br \/>\n      &lt;\/template&gt;<br \/>\n    &lt;\/el-dropdown&gt;<br \/>\n&lt;\/div&gt;<br \/>\n&lt;\/el-header&gt;<br \/>\n&lt;!&#8211;\u5934\u90e8 end&#8211;&gt;<\/p>\n<p>    &lt;el-container&gt;<br \/>\n&lt;!&#8211;\u5de6\u4fa7\u8fb9\u680f start  &#8211;&gt;<br \/>\n        &lt;el-aside width&#061;&#034;200px&#034; &gt;<br \/>\n   &lt;el-menu<br \/>\n        default-active&#061;&#034;2&#034;<br \/>\n        class&#061;&#034;el-menu-vertical-demo&#034;<br \/>\nrouter  &#064;select&#061;&#034;addTab&#034; style&#061;&#034;height: 700px&#034;&gt;<br \/>\n&lt;el-menu-item&gt;<br \/>\n  &lt;el-icon&gt;&lt;document \/&gt;&lt;\/el-icon&gt;&lt;el-text class&#061;&#034;mx-1&#034; size&#061;&#034;large&#034;&gt;{{menus.meta.title}}&lt;\/el-text&gt;<br \/>\n&lt;\/el-menu-item&gt;<br \/>\n        &lt;el-menu-item  v-for&#061;&#034;(menu,index) in menus.children&#034; :index&#061;&#034;menu.path&#034;  :key&#061;&#034;menu.path&#034; &gt;<br \/>\n          &lt;el-icon&gt;&lt;document \/&gt;&lt;\/el-icon&gt;{{menu.name}}<br \/>\n&lt;\/el-menu-item&gt;<br \/>\n    &lt;\/el-menu&gt;<br \/>\n&lt;\/el-aside&gt;<br \/>\n&lt;!&#8211;\u5de6\u4fa7\u8fb9\u680f end  &#8211;&gt; <\/p>\n<p>&lt;el-container&gt;<br \/>\n          &lt;el-main class&#061;&#034;main&#034;&gt;<br \/>\n&lt;el-tabs v-model&#061;&#034;editableTabsValue&#034; type&#061;&#034;card&#034; editable<br \/>\n &#064;edit&#061;&#034;handleTabsEdit&#034; &#064;tab-click&#061;&#034;clickTag&#034; &gt;<br \/>\n&lt;el-tab-pane<br \/>\nalign&#061;&#034;center&#034;<br \/>\n    :key&#061;&#034;item.name&#034;<br \/>\n    v-for&#061;&#034;(item, index) in editableTabs&#034;<br \/>\n    :label&#061;&#034;item.title&#034;<br \/>\n    :name&#061;&#034;item.name&#034;<br \/>\n    :route&#061;&#034;item.route&#034;<br \/>\n&gt;<br \/>\n&lt;router-view&gt;&lt;\/router-view&gt;<br \/>\n&lt;\/el-tab-pane&gt;<br \/>\n&lt;\/el-tabs&gt;<br \/>\n  &lt;\/el-main&gt;<br \/>\n&lt;\/el-container&gt;<br \/>\n      &lt;\/el-container&gt;<br \/>\n    &lt;\/el-container&gt;<br \/>\n  &lt;\/div&gt;<br \/>\n&lt;\/template&gt;<\/p>\n<p>&lt;script setup&gt;<br \/>\nimport {computed, ref} from &#039;vue&#039;<br \/>\nimport { useRouter } from &#039;vue-router&#039;<br \/>\nimport { postReq } from &#039;..\/utils\/api&#039;<br \/>\nimport { useUserStore } from &#039;..\/store\/user.js&#039;<br \/>\n\/\/\u8def\u7531<br \/>\nconst router &#061; useRouter()<br \/>\n\/\/\u83b7\u53d6\u7528\u6237\u4ed3\u5e93\u5bf9\u8c61<br \/>\nconst userStore&#061;useUserStore()<br \/>\nconst menus&#061;router.options.routes.find(m&#061;&gt;m.role&#061;&#061;userStore.getUserInfo.value.useType);<br \/>\nconst editableTabsValue&#061;ref(&#039;&#039;)<br \/>\nconst editableTabs&#061;ref([])<br \/>\nconst tabIndex&#061;ref(0)<\/p>\n<p>\/\/\u67e5\u627e\u8def\u5f84\u5bf9\u5e94\u7684\u8def\u7531<br \/>\nfunction findCompontByPath(path){<br \/>\nlet a&#061;menus.children.find(m&#061;&gt;m.path&#061;&#061;path);<br \/>\nif (a) {<br \/>\nreturn a;<br \/>\n}<br \/>\nreturn null;<br \/>\n}<\/p>\n<p>\/\/\u6253\u5f00\u65b0\u7a97\u53e3<br \/>\nfunction clickTag(tag,e){<br \/>\n\/\/console.log(tag.paneName)<br \/>\nrouter.push(tag.paneName)<br \/>\n}<br \/>\nfunction addTab(path){<br \/>\nif(path){<br \/>\nlet componet&#061;findCompontByPath(path)<br \/>\nif (componet) {<br \/>\n  if (!editableTabs.value.find(t &#061;&gt; t.name &#061;&#061; componet.path)) {<br \/>\neditableTabs.value.push({<br \/>\ntitle: componet.name,<br \/>\nname: componet.path,<br \/>\nroute: componet.path<br \/>\n});<br \/>\n  }<br \/>\n  editableTabsValue.value &#061; componet.path;<br \/>\n  router.push(componet.path);<br \/>\n}<\/p>\n<p>}<br \/>\n}<\/p>\n<p>function handleTabsEdit(targetName, action) {<br \/>\n  if (action &#061;&#061;&#061; &#039;remove&#039;) {<br \/>\nlet tabs &#061; editableTabs.value;<br \/>\nlet activeName &#061; editableTabsValue.value;<br \/>\nif (activeName &#061;&#061;&#061; targetName) {<br \/>\n  tabs.forEach((tab, index) &#061;&gt; {<br \/>\n\/\/ console.log(tab.name, targetName, tab.name &#061;&#061;&#061; targetName);<br \/>\nif (tab.name &#061;&#061;&#061; targetName) {<br \/>\n  let nextTab &#061; tabs[index &#043; 1] || tabs[index &#8211; 1];<br \/>\n  if (nextTab) {<br \/>\nactiveName &#061; nextTab.name;<br \/>\n  }<br \/>\n}<br \/>\n  });<br \/>\n}<br \/>\neditableTabsValue.value &#061; activeName;<br \/>\neditableTabs.value &#061; tabs.filter(tab &#061;&gt; tab.name !&#061;&#061; targetName);<br \/>\nrouter.push(activeName);<br \/>\n  }<br \/>\n}<\/p>\n<p>function handleCommand(command){<br \/>\npostReq(&#034;\/user\/logout&#034;).then(resp&#061;&gt;{<br \/>\nif(resp.data.result){<br \/>\nuserStore.logOut();<br \/>\nrouter.push(&#034;\/login&#034;)<br \/>\n}<br \/>\n})<\/p>\n<p>}<\/p>\n<p>&lt;\/script&gt;<\/p>\n<p>&lt;style&gt;<br \/>\n.home_container {<br \/>\nheight: 100%;<br \/>\nposition: absolute;<br \/>\ntop: 0px;<br \/>\nleft: 0px;<br \/>\nwidth: 100%;<br \/>\n}<br \/>\n.top-header{<br \/>\nbackground-color: #20a0ff;<br \/>\ncolor: #333;<br \/>\ntext-align: center;<br \/>\ndisplay: flex;<br \/>\nalign-items: center;<br \/>\njustify-content: space-between;<br \/>\n}<br \/>\n.left-aside{<br \/>\nbackground-color: #ECECEC;<br \/>\n}<\/p>\n<p>.main{<br \/>\nwidth: 100%;<br \/>\nheight: 100%;<br \/>\nbackground-color: #fff;<br \/>\ncolor: #000;<br \/>\ntext-align: left;<br \/>\n}<br \/>\n.home_title {<br \/>\ncolor: #fff;<br \/>\nfont-size: 22px;<br \/>\ndisplay: inline;<br \/>\n}<\/p>\n<p>.home_userinfo {<br \/>\ncolor: #fff;<br \/>\ncursor: pointer;<br \/>\n}<\/p>\n<p>.home_userinfoContainer {<br \/>\ndisplay: inline;<br \/>\nmargin-right: 20px;<br \/>\ntext-align: right;<br \/>\n}<br \/>\n&lt;\/style&gt; <\/p>\n<p>\u5728 Vue Router \u4e2d&#xff0c;&lt;router-view&gt;&lt;\/router-view&gt;\u00a0\u7ec4\u4ef6\u7684\u5185\u5bb9\u53d8\u5316\u662f\u57fa\u4e8e\u5f53\u524d\u7684\u8def\u7531\u5339\u914d\u7ed3\u679c\u3002<\/p>\n<h4>\u8def\u7531\u5339\u914d\u4e0e\u89c6\u56fe\u6e32\u67d3\u7684\u5de5\u4f5c\u539f\u7406<\/h4>\n<li>\n<p>\u8def\u7531\u914d\u7f6e&#xff1a;Vue Router \u7684\u8def\u7531\u914d\u7f6e\u5b58\u50a8\u5728\u00a0router.options.routes\u00a0\u4e2d&#xff0c;\u60a8\u7684\u4ee3\u7801\u901a\u8fc7\u00a0menus&#061;router.options.routes.find(m&#061;&gt;m.role&#061;&#061;userStore.getUserInfo.value.useType)\u00a0\u6765\u83b7\u53d6\u5f53\u524d\u7528\u6237\u89d2\u8272\u5bf9\u5e94\u7684\u8def\u7531\u914d\u7f6e\u3002<\/p>\n<\/li>\n<li>\n<p>\u52a8\u6001\u8def\u7531\u5207\u6362&#xff1a;<\/p>\n<ul>\n<li>\u5f53\u7528\u6237\u70b9\u51fb\u5de6\u4fa7\u83dc\u5355\u7684\u83dc\u5355\u9879\u65f6&#xff0c;addTab\u00a0\u65b9\u6cd5\u4f1a\u88ab\u89e6\u53d1<\/li>\n<li>\u8be5\u65b9\u6cd5\u4f1a\u6839\u636e\u8def\u5f84\u627e\u5230\u5bf9\u5e94\u7684\u8def\u7531\u7ec4\u4ef6<\/li>\n<li>\u5982\u679c\u8be5\u8def\u7531\u5c1a\u672a\u5728\u6807\u7b7e\u9875\u4e2d\u6253\u5f00&#xff0c;\u5219\u6dfb\u52a0\u4e00\u4e2a\u65b0\u6807\u7b7e\u9875<\/li>\n<li>\u7136\u540e\u901a\u8fc7\u00a0router.push(componet.path)\u00a0\u89e6\u53d1\u8def\u7531\u5207\u6362<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u6807\u7b7e\u9875\u70b9\u51fb\u4e8b\u4ef6&#xff1a;<\/p>\n<ul>\n<li>\u5f53\u7528\u6237\u70b9\u51fb\u6807\u7b7e\u9875\u65f6&#xff0c;clickTag\u00a0\u65b9\u6cd5\u4f1a\u88ab\u89e6\u53d1<\/li>\n<li>\u8be5\u65b9\u6cd5\u540c\u6837\u901a\u8fc7\u00a0router.push(tag.paneName)\u00a0\u89e6\u53d1\u8def\u7531\u5207\u6362<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u6807\u7b7e\u9875\u5173\u95ed\u4e8b\u4ef6&#xff1a;<\/p>\n<ul>\n<li>\u5f53\u7528\u6237\u5173\u95ed\u6807\u7b7e\u9875\u65f6&#xff0c;handleTabsEdit\u00a0\u65b9\u6cd5\u4f1a\u88ab\u89e6\u53d1<\/li>\n<li>\u5982\u679c\u5173\u95ed\u7684\u662f\u5f53\u524d\u6fc0\u6d3b\u7684\u6807\u7b7e\u9875&#xff0c;\u4f1a\u81ea\u52a8\u5207\u6362\u5230\u4e0b\u4e00\u4e2a\u6216\u4e0a\u4e00\u4e2a\u6807\u7b7e\u9875<\/li>\n<li>\u540c\u6837\u901a\u8fc7\u00a0router.push(activeName)\u00a0\u6765\u66f4\u65b0\u5f53\u524d\u8def\u7531<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u89c6\u56fe\u66f4\u65b0&#xff1a;<\/p>\n<ul>\n<li>\u5f53\u8def\u7531\u53d1\u751f\u53d8\u5316\u65f6&#xff0c;Vue Router \u4f1a\u81ea\u52a8\u67e5\u627e\u4e0e\u5f53\u524d\u8def\u5f84\u5339\u914d\u7684\u7ec4\u4ef6<\/li>\n<li>\u7136\u540e\u5c06\u5339\u914d\u5230\u7684\u7ec4\u4ef6\u6e32\u67d3\u5230\u00a0&lt;router-view&gt;&lt;\/router-view&gt;\u00a0\u7ec4\u4ef6\u6240\u5728\u7684\u4f4d\u7f6e<\/li>\n<li>\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u60a8\u7684\u4ee3\u7801\u4e2d&#xff0c;\u6bcf\u4e2a\u6807\u7b7e\u9875\u7684\u5185\u5bb9\u90fd\u662f\u00a0&lt;router-view&gt;&lt;\/router-view&gt;&#xff0c;\u4f46\u663e\u793a\u7684\u5185\u5bb9\u5374\u4e0d\u540c<\/li>\n<\/ul>\n<\/li>\n<h4>\u5173\u952e\u903b\u8f91\u5206\u6790<\/h4>\n<p>\u5728\u60a8\u7684\u4ee3\u7801\u4e2d&#xff0c;\u8def\u7531\u5207\u6362\u7684\u6838\u5fc3\u903b\u8f91\u662f\u901a\u8fc7\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u6cd5\u5b9e\u73b0\u7684&#xff1a;<\/p>\n<ul>\n<li>addTab(path)&#xff1a;\u5f53\u7528\u6237\u70b9\u51fb\u5de6\u4fa7\u83dc\u5355\u65f6&#xff0c;\u6dfb\u52a0\u6807\u7b7e\u9875\u5e76\u5207\u6362\u8def\u7531<\/li>\n<li>clickTag(tag,e)&#xff1a;\u5f53\u7528\u6237\u70b9\u51fb\u6807\u7b7e\u9875\u65f6&#xff0c;\u5207\u6362\u5230\u5bf9\u5e94\u7684\u8def\u7531<\/li>\n<li>handleTabsEdit(targetName, action)&#xff1a;\u5f53\u7528\u6237\u5173\u95ed\u6807\u7b7e\u9875\u65f6&#xff0c;\u66f4\u65b0\u5f53\u524d\u6fc0\u6d3b\u7684\u6807\u7b7e\u9875\u5e76\u5207\u6362\u8def\u7531<\/li>\n<\/ul>\n<p>\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u901a\u8fc7\u00a0router.push()\u00a0\u6765\u89e6\u53d1\u8def\u7531\u5207\u6362&#xff0c;\u800c\u00a0&lt;router-view&gt;&lt;\/router-view&gt;\u00a0\u4f1a\u6839\u636e\u5f53\u524d\u7684\u8def\u7531\u81ea\u52a8\u6e32\u67d3\u5339\u914d\u7684\u7ec4\u4ef6\u3002<\/p>\n<\/p>\n<h3>\u7b2c\u4e09\u6b65&#xff1a;\u521b\u5efa\u8def\u7531\u6a21\u5757&#xff1a;<\/h3>\n<\/p>\n<p>import {createRouter, createWebHistory, createWebHashHistory} from &#039;vue-router&#039;<br \/>\nimport { defineAsyncComponent } from &#039;vue&#039;<br \/>\nimport { useUserStore } from &#039;..\/store\/user.js&#039;<\/p>\n<p>const router &#061; createRouter({<br \/>\n  history: createWebHistory(),  \/\/ history \u6a21\u5f0f<br \/>\n  routes: [<br \/>\n{<br \/>\n  path: &#039;\/&#039;,<br \/>\n  name: &#039;Login&#039;,<br \/>\n  role:0,<br \/>\n  component: defineAsyncComponent(() &#061;&gt; import(&#096;..\/components\/Login.vue&#096;)),<br \/>\n  meta: {title: &#039;Login&#039;}<br \/>\n},<br \/>\n{<br \/>\n  path: &#039;\/login&#039;,<br \/>\n  name: &#039;Login&#039;,<br \/>\n  role:0,<br \/>\n  component: defineAsyncComponent(() &#061;&gt; import(&#096;..\/components\/Login.vue&#096;)),<br \/>\n  meta: {title: &#039;Login&#039;}<br \/>\n},<br \/>\n{<br \/>\npath:&#039;\/home&#039;,<br \/>\nname:&#039;\u7cfb\u7edf\u4fe1\u606f&#039;,<br \/>\ncomponent:defineAsyncComponent(() &#061;&gt; import(&#096;..\/components\/home.vue&#096;)),<br \/>\nrole:170,<br \/>\nmeta: {title: &#039;\u7cfb\u7edf\u4fe1\u606f&#039;},<br \/>\nchildren:[<br \/>\n{<br \/>\npath:&#039;\/constant&#039;,<br \/>\nname:&#039;\u5e38\u6570\u7c7b\u522b\u7ba1\u7406&#039;,<br \/>\ncomponent:defineAsyncComponent(() &#061;&gt; import(&#096;..\/components\/neusys\/Constant.vue&#096;)),<br \/>\n},<br \/>\n{<br \/>\npath:&#039;\/constantitem&#039;,<br \/>\nname:&#039;\u5e38\u6570\u9879\u7ba1\u7406&#039;,<br \/>\ncomponent:defineAsyncComponent(() &#061;&gt; import(&#096;..\/components\/neusys\/ConstantItem.vue&#096;)),<br \/>\n},<br \/>\n{<br \/>\npath:&#039;\/department&#039;,<br \/>\nname:&#039;\u79d1\u5ba4\u7ba1\u7406&#039;,<br \/>\ncomponent:defineAsyncComponent(() &#061;&gt; import(&#096;..\/components\/neusys\/Department.vue&#096;)),<br \/>\n},<br \/>\n{<br \/>\npath:&#039;\/user&#039;,<br \/>\nname:&#039;\u8d26\u6237\u7ba1\u7406&#039;,<br \/>\ncomponent:defineAsyncComponent(() &#061;&gt; import(&#096;..\/components\/neusys\/User.vue&#096;)),<br \/>\n},<br \/>\n{<br \/>\npath:&#039;\/registlevel&#039;,<br \/>\nname:&#039;\u6302\u53f7\u7ea7\u522b\u7ba1\u7406&#039;,<br \/>\ncomponent:defineAsyncComponent(() &#061;&gt; import(&#096;..\/components\/neusys\/RegistLevel.vue&#096;)),<br \/>\n},<br \/>\n{<br \/>\npath:&#039;\/settlecategory&#039;,<br \/>\nname:&#039;\u7ed3\u7b97\u7ea7\u522b\u7ba1\u7406&#039;,<br \/>\ncomponent:defineAsyncComponent(() &#061;&gt; import(&#096;..\/components\/neusys\/SettleCategory.vue&#096;)),<br \/>\n},<br \/>\n{<br \/>\npath:&#039;\/disecategory&#039;,<br \/>\nname:&#039;\u75be\u75c5\u5206\u7c7b\u7ba1\u7406&#039;,<br \/>\ncomponent:defineAsyncComponent(() &#061;&gt; import(&#096;..\/components\/neusys\/DiseCategory.vue&#096;)),<br \/>\n},<br \/>\n{<br \/>\npath:&#039;\/disease&#039;,<br \/>\nname:&#039;\u8bca\u65ad\u76ee\u5f55\u7ba1\u7406&#039;,<br \/>\ncomponent:defineAsyncComponent(() &#061;&gt; import(&#096;..\/components\/neusys\/Disease.vue&#096;)),<br \/>\n},<br \/>\n{<br \/>\npath:&#039;\/expense&#039;,<br \/>\nname:&#039;\u8d39\u7528\u79d1\u76ee\u8868&#039;,<br \/>\ncomponent:defineAsyncComponent(() &#061;&gt; import(&#096;..\/components\/neusys\/ExpenseClass.vue&#096;)),<br \/>\n},<br \/>\n{<br \/>\npath:&#039;\/fmeditem&#039;,<br \/>\nname:&#039;\u975e\u836f\u54c1\u6536\u8d39\u9879\u76ee&#039;,<br \/>\ncomponent:defineAsyncComponent(() &#061;&gt; import(&#096;..\/components\/neusys\/Fmeditem.vue&#096;)),<br \/>\n},<br \/>\n{<br \/>\npath:&#039;\/rule&#039;,<br \/>\nname:&#039;\u6392\u73ed\u89c4\u5219&#039;,<br \/>\ncomponent:defineAsyncComponent(() &#061;&gt; import(&#096;..\/components\/neusys\/Rule.vue&#096;)),<br \/>\n},<br \/>\n{<br \/>\npath:&#039;\/scheduling&#039;,<br \/>\nname:&#039;\u6392\u73ed\u8ba1\u5212&#039;,<br \/>\ncomponent:defineAsyncComponent(() &#061;&gt; import(&#096;..\/components\/neusys\/Scheduling.vue&#096;)),<br \/>\n}<br \/>\n]<br \/>\n},<br \/>\n{<br \/>\n  path: &#039;\/home&#039;,<br \/>\n  name: &#039;\u6302\u53f7\u6536\u8d39&#039;,<br \/>\n  role:171,<br \/>\n  component: defineAsyncComponent(() &#061;&gt; import(&#096;..\/components\/home.vue&#096;)),<br \/>\n  meta: {title: &#039;\u6302\u53f7\u6536\u8d39&#039;},<br \/>\n  children:[<br \/>\n  {<br \/>\npath:&#039;\/customer&#039;,<br \/>\nname:&#039;\u7528\u6237\u7ba1\u7406&#039;,<br \/>\ncomponent:defineAsyncComponent(() &#061;&gt; import(&#096;..\/components\/neureg\/Customer.vue&#096;)),<br \/>\n  },<br \/>\n  {<br \/>\npath:&#039;\/medicalcard&#039;,<br \/>\nname:&#039;\u7535\u5b50\u5c31\u8bca\u5361&#039;,<br \/>\ncomponent:defineAsyncComponent(() &#061;&gt; import(&#096;..\/components\/neureg\/MedicalCard.vue&#096;)),<br \/>\n  },<br \/>\n  {<br \/>\npath:&#039;\/register&#039;,<br \/>\nname:&#039;\u73b0\u573a\u6302\u53f7&#039;,<br \/>\ncomponent:defineAsyncComponent(() &#061;&gt; import(&#096;..\/components\/neureg\/Register.vue&#096;)),<br \/>\n  },<br \/>\n  {<br \/>\npath:&#039;\/refund&#039;,<br \/>\nname:&#039;\u9000\u53f7&#039;,<br \/>\ncomponent:defineAsyncComponent(() &#061;&gt; import(&#096;..\/components\/neureg\/Refund.vue&#096;)),<br \/>\n  }<\/p>\n<p>  ]<br \/>\n},<br \/>\n{<br \/>\n  path: &#039;\/home&#039;,<br \/>\n  name: &#039;\u95e8\u8bca\u533b\u751f&#039;,<br \/>\n  role:172,<br \/>\n  component: defineAsyncComponent(() &#061;&gt; import(&#096;..\/components\/home.vue&#096;)),<br \/>\n  meta: {title: &#039;\u95e8\u8bca\u533b\u751f&#039;},<br \/>\n  children:[<br \/>\n  {<br \/>\npath:&#039;\/docHome&#039;,<br \/>\nname:&#039;\u95e8\u8bca\u75c5\u5386&#039;,<br \/>\ncomponent:defineAsyncComponent(() &#061;&gt; import(&#096;..\/components\/neudoc\/DocHome.vue&#096;)),<br \/>\n  }<\/p>\n<p>  ]<br \/>\n},<\/p>\n<p>  ]<br \/>\n})<\/p>\n<p>\/\/ \u5168\u5c40\u8def\u7531\u5b88\u536b<br \/>\nrouter.beforeEach((to, from, next) &#061;&gt; {<br \/>\n\/\/ let userLogin &#061;  sessionStorage.getItem(&#034;userLogin&#034;);<br \/>\nconst userStore &#061; useUserStore()<br \/>\n\/\/ let isAuth&#061;userStore.getUserInfo.value.isAuth;<br \/>\nlet isAuth&#061;userStore.getUserInfo.value.isAuth;<br \/>\n\/\/\u5224\u65ad\u8def\u7531\u7684\u522b\u540d\u4e0d\u662f\u767b\u5f55\u4e14\u672a\u8fdb\u884c\u767b\u5f55\u8ba4\u8bc1&#xff0c;\u5c31\u8df3\u8f6c\u53bb\u767b\u5f55<br \/>\nif(to.name !&#061;&#061; &#039;Login&#039; &amp;&amp; !isAuth){<br \/>\n  next({ name: &#039;Login&#039; })<br \/>\n}else if(to.name&#061;&#061;&#034;Login&#034; &amp;&amp; isAuth){<br \/>\n\/\/\u5df2\u767b\u5f55&#xff0c;\u4e0d\u5141\u8bb8\u9000\u56de\u5230\u767b\u5f55\u9875\u9762<br \/>\n   next({ path: &#039;\/home&#039; })<br \/>\n}<br \/>\nelse{<br \/>\n  next()<br \/>\n}<br \/>\n})<\/p>\n<p>router.afterEach((to, from)&#061;&gt;{<br \/>\n  \/\/ console.log(to, from)<br \/>\n  \/\/console.log(&#039;afterEach&#039;)<br \/>\n})<\/p>\n<p>export default router <\/p>\n<p>\u4e0a\u9762\u6211\u4eec\u662f\u7528\u61d2\u52a0\u8f7d\u7684\u65b9\u5f0f\u54df&#xff1a;<\/p>\n<p>{<br \/>\n  path: &#039;\/&#039;,<br \/>\n  name: &#039;Login&#039;,<br \/>\n  role:0,<br \/>\n  component: defineAsyncComponent(() &#061;&gt; import(&#096;..\/components\/Login.vue&#096;)),<br \/>\n  meta: {title: &#039;Login&#039;}<br \/>\n}, <\/p>\n<h4>\u547d\u540d\u8def\u7531<\/h4>\n<p><span style=\"color:#fe2c24\">{ path: &#039;\u8def\u7531\u8def\u5f84&#039;, name: &#039;\u8def\u7531\u540d\u79f0&#039;, component: \u7ec4\u4ef6 }<\/span><\/p>\n<p><span style=\"color:#595959\">\u5728\u5b9a\u4e49\u8def\u7531\u5339\u914d\u89c4\u5219\u65f6&#xff0c;\u4f7f\u7528<\/span><span style=\"color:#1369b3\">name<\/span><span style=\"color:#1369b3\">\u5c5e\u6027<\/span><span style=\"color:#595959\">\u4e3a\u8def\u7531\u5339\u914d\u89c4\u5219\u5b9a\u4e49\u8def\u7531\u540d\u79f0&#xff0c;\u5373\u53ef\u5b9e\u73b0<\/span><span style=\"color:#1369b3\">\u547d\u540d\u8def\u7531<\/span><span style=\"color:#595959\">\u3002\u5f53\u8def\u7531\u5339\u914d\u89c4\u5219\u6709\u4e86\u8def\u7531\u540d\u79f0\u540e&#xff0c;\u5728\u5b9a\u4e49\u8def\u7531\u94fe\u63a5\u6216\u6267\u884c\u67d0\u4e9b\u8df3\u8f6c\u64cd\u4f5c\u65f6&#xff0c;\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7<\/span><span style=\"color:#1369b3\">\u8def\u7531\u540d\u79f0<\/span><span style=\"color:#595959\">\u8868\u793a\u76f8\u5e94\u7684\u8def\u7531&#xff0c;\u4e0d\u518d\u9700\u8981\u901a\u8fc7\u8def\u7531\u8def\u5f84\u8868\u793a\u76f8\u5e94\u7684\u8def\u7531\u3002<\/span><\/p>\n<p><span style=\"color:#595959\">\u6211\u4eec\u901a\u8fc7\u5982\u4e0b\u65b9\u5f0f\u4f7f\u7528\u5b83&#xff1a;<\/span><\/p>\n<\/p>\n<p>router.push(activeName);  \/\/ activeName\u4e3a\u8def\u7531\u540d\u79f0 <\/p>\n<p>\u800c\u4e14\u8fd8\u6709\u8def\u7531\u5d4c\u5957\u3002<\/p>\n<h3>\u7b2c\u56db\u6b65&#xff1a;\u5bfc\u5165\u4e14\u6302\u8f7d\u8def\u7531\u6a21\u5757&#xff1a;<\/h3>\n<p>import { createApp } from &#039;vue&#039;<br \/>\nimport { createPinia } from &#039;pinia&#039;<br \/>\nimport router from &#039;.\/router&#039;<br \/>\nimport ElementPlus from &#039;element-plus&#039;;<br \/>\nimport &#039;element-plus\/theme-chalk\/index.css&#039;;<br \/>\nimport App from &#039;.\/App.vue&#039;<br \/>\nimport Axios from &#039;axios&#039;<\/p>\n<p>const app&#061;createApp(App)<br \/>\napp.use(router)<br \/>\napp.use(createPinia())<br \/>\napp.use(ElementPlus)<br \/>\napp.mount(&#039;#app&#039;)<\/p>\n<h3><span style=\"color:#fe2c24\">\u7f16\u7a0b\u5f0f\u5bfc\u822a<\/span><\/h3>\n<p style=\"margin-left:0in;text-align:justify\"><span style=\"color:#595959\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Vue Router<\/span><span style=\"color:#595959\">\u63d0\u4f9b\u4e86<\/span><span style=\"color:#1369b3\">useRouter()<\/span><span style=\"color:#1369b3\">\u51fd\u6570<\/span><span style=\"color:#595959\">&#xff0c;\u4f7f\u7528\u5b83\u53ef\u4ee5\u83b7\u53d6\u5168\u5c40\u8def\u7531\u5b9e\u4f8b&#xff0c;\u8be5\u5168\u5c40\u8def\u7531\u5b9e\u4f8b\u4e2d\u63d0\u4f9b\u4e86\u5e38\u7528\u7684<\/span><span style=\"color:#1369b3\">push()<\/span><span style=\"color:#1369b3\">\u65b9\u6cd5<\/span><span style=\"color:#595959\">\u3001<\/span><span style=\"color:#1369b3\">replace()<\/span><span style=\"color:#1369b3\">\u65b9\u6cd5<\/span><span style=\"color:#595959\">\u548c<\/span><span style=\"color:#1369b3\">go()<\/span><span style=\"color:#1369b3\">\u65b9\u6cd5<\/span><span style=\"color:#595959\">&#xff0c;\u83b7\u53d6\u5168\u5c40\u8def\u7531\u5b9e\u4f8b\u7684\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\u3002<\/span><\/p>\n<p style=\"margin-left:0in;text-align:justify\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"539\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250619012740-6853678c49067.png\" width=\"1062\" \/><\/p>\n<h4><span style=\"color:#fe2c24\">\u00a0push\u65b9\u6cd5&#xff1a;<\/span><\/h4>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color:#1369b3\">push()\u65b9\u6cd5<\/span><span style=\"color:#595959\">\u4f1a\u5411\u5386\u53f2\u8bb0\u5f55\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u8bb0\u5f55&#xff0c;\u4ee5<\/span><span style=\"color:#1369b3\">\u7f16\u7a0b\u65b9\u5f0f<\/span><span style=\"color:#595959\">\u5bfc\u822a\u5230\u4e00\u4e2a\u65b0\u7684<\/span><span style=\"color:#595959\">URL<\/span><span style=\"color:#595959\">\u3002\u5f53\u7528\u6237\u5355\u51fb\u6d4f\u89c8\u5668<\/span><span style=\"color:#1369b3\">\u540e\u9000<\/span><span style=\"color:#595959\">\u6309\u94ae\u65f6&#xff0c;\u4f1a\u56de\u5230\u4e4b\u524d\u7684<\/span><span style=\"color:#595959\">URL<\/span><span style=\"color:#595959\">\u3002<\/span><span style=\"color:#595959\">push()<\/span><span style=\"color:#595959\">\u65b9\u6cd5\u7684\u53c2\u6570\u53ef\u4ee5\u662f\u4e00\u4e2a<\/span><span style=\"color:#1369b3\">\u5b57\u7b26\u4e32\u8def\u5f84<\/span><span style=\"color:#595959\">&#xff0c;\u6216\u8005\u4e00\u4e2a\u63cf\u8ff0\u5730\u5740\u7684<\/span><span style=\"color:#1369b3\">\u5bf9\u8c61<\/span><span style=\"color:#595959\">&#xff0c;\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\u3002<\/span><\/p>\n<p>router.push(&#039;\/about\/tab1&#039;)\/\/ \u5b57\u7b26\u4e32\u8def\u5f84<br \/>\nrouter.push({ path: &#039;\/about\/tab1&#039; })\/\/ \u5e26\u6709\u8def\u5f84\u7684\u5bf9\u8c61<br \/>\nrouter.push({ name: &#039;user&#039;, params: { userId: &#039;123&#039;} })\/\/ \u547d\u540d\u8def\u7531<br \/>\nrouter.push({ path: &#039;\/user&#039;, query: { id: &#039;1&#039; } })\/\/ \u5e26\u67e5\u8be2\u53c2\u6570&#xff0c;\u5982&#xff1a;\/user?id&#061;1<br \/>\nrouter.push({ path: &#039;\/user&#039;, hash: &#039;#admin&#039; })\/\/ \u5e26\u6709Hash\u503c&#xff0c;\u5982&#xff1a;\/user#admin<\/p>\n<p>\u6211\u4eec\u9879\u76ee\u4e2d\u4e5f\u7528\u5230\u7684 push&#xff1a;<\/p>\n<p>function addTab(path){<br \/>\nif(path){<br \/>\nlet componet&#061;findCompontByPath(path)<br \/>\nif (componet) {<br \/>\n  if (!editableTabs.value.find(t &#061;&gt; t.name &#061;&#061; componet.path)) {<br \/>\neditableTabs.value.push({<br \/>\ntitle: componet.name,<br \/>\nname: componet.path,<br \/>\nroute: componet.path<br \/>\n});<br \/>\n  }<br \/>\n  editableTabsValue.value &#061; componet.path;<br \/>\n  router.push(componet.path);<br \/>\n}<\/p>\n<p>}<br \/>\n} <\/p>\n<h3><span style=\"color:#fe2c24\">\u5bfc\u822a\u5b88\u536b\u00a0<\/span><\/h3>\n<p><span style=\"color:#fe2c24\">\u00b7<\/span><span style=\"color:#1369b3\">\u5bfc\u822a\u5b88\u536b<\/span><span style=\"color:#595959\">\u7528\u4e8e<\/span><span style=\"color:#1369b3\">\u63a7\u5236\u8def\u7531\u7684\u8bbf\u95ee\u6743\u9650<\/span><span style=\"color:#595959\">\u3002\u4f8b\u5982&#xff0c;\u8bbf\u95ee\u540e\u53f0\u4e3b\u9875\u65f6&#xff0c;\u9700\u8981\u7528\u6237\u5904\u4e8e\u5df2\u767b\u5f55\u72b6\u6001&#xff0c;\u5982\u679c\u6ca1\u6709\u767b\u5f55&#xff0c;\u5219\u8df3\u8f6c\u5230\u767b\u5f55\u9875\u9762\u3002\u7528\u6237\u5728\u767b\u5f55\u9875\u9762\u8fdb\u884c\u767b\u5f55\u64cd\u4f5c&#xff0c;\u82e5\u767b\u5f55\u6210\u529f&#xff0c;\u5219\u8df3\u8f6c\u5230\u540e\u53f0\u4e3b\u9875&#xff1b;\u82e5\u767b\u5f55\u5931\u8d25&#xff0c;\u5219\u8fd4\u56de\u767b\u5f55\u9875\u9762\u3002<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"204\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250619012740-6853678c83441.png\" width=\"813\" \/><\/p>\n<p style=\"margin-left:0in;margin-right:0in;text-align:left\"><span style=\"color:#595959\">\u8fd9\u91cc\u6211\u4eec\u53ea\u4ecb\u7ecd\u5168\u5c40\u5b88\u536b&#xff0c;\u5305\u62ec\u5168\u5c40\u524d\u7f6e\u5b88\u536b<\/span><span style=\"color:#595959\">beforeEach<\/span><span style=\"color:#595959\">()<\/span><span style=\"color:#595959\">\u548c\u5168\u5c40\u540e\u7f6e\u5b88\u536b<\/span><span style=\"color:#595959\">afterEach<\/span><span style=\"color:#595959\">()<\/span><span style=\"color:#595959\">&#xff0c;\u5728\u8def\u7531\u5373\u5c06\u6539\u53d8\u524d\u548c\u6539\u53d8\u540e\u8fdb\u884c\u89e6\u53d1\u3002<\/span><\/p>\n<p>\/\/ \u5168\u5c40\u8def\u7531\u5b88\u536b<br \/>\nrouter.beforeEach((to, from, next) &#061;&gt; {<br \/>\n\/\/ let userLogin &#061;  sessionStorage.getItem(&#034;userLogin&#034;);<br \/>\nconst userStore &#061; useUserStore()<br \/>\n\/\/ let isAuth&#061;userStore.getUserInfo.value.isAuth;<br \/>\nlet isAuth&#061;userStore.getUserInfo.value.isAuth;<br \/>\n\/\/\u5224\u65ad\u8def\u7531\u7684\u522b\u540d\u4e0d\u662f\u767b\u5f55\u4e14\u672a\u8fdb\u884c\u767b\u5f55\u8ba4\u8bc1&#xff0c;\u5c31\u8df3\u8f6c\u53bb\u767b\u5f55<br \/>\nif(to.name !&#061;&#061; &#039;Login&#039; &amp;&amp; !isAuth){<br \/>\n  next({ name: &#039;Login&#039; })<br \/>\n}else if(to.name&#061;&#061;&#034;Login&#034; &amp;&amp; isAuth){<br \/>\n\/\/\u5df2\u767b\u5f55&#xff0c;\u4e0d\u5141\u8bb8\u9000\u56de\u5230\u767b\u5f55\u9875\u9762<br \/>\n   next({ path: &#039;\/home&#039; })<br \/>\n}<br \/>\nelse{<br \/>\n  next()<br \/>\n}<br \/>\n}) <\/p>\n<h4>\u5168\u5c40\u8def\u7531\u5b88\u536b beforeEach \u8be6\u89e3<\/h4>\n<h5>\u4e00\u3001\u5bfc\u822a\u5b88\u536b\u7684\u57fa\u672c\u6982\u5ff5<\/h5>\n<p>\u5728 Vue Router \u4e2d&#xff0c;\u5bfc\u822a\u5b88\u536b\u662f\u4e00\u79cd\u62e6\u622a\u8def\u7531\u5bfc\u822a\u8fc7\u7a0b\u7684\u673a\u5236&#xff0c;\u5141\u8bb8\u4f60\u5728\u8def\u7531\u5207\u6362\u524d\u540e\u6267\u884c\u81ea\u5b9a\u4e49\u903b\u8f91\u3002beforeEach\u662f\u5168\u5c40\u524d\u7f6e\u5b88\u536b&#xff0c;\u4f1a\u5728\u6bcf\u6b21\u8def\u7531\u5207\u6362\u524d\u88ab\u8c03\u7528&#xff0c;\u662f\u5b9e\u73b0\u6743\u9650\u63a7\u5236\u3001\u767b\u5f55\u9a8c\u8bc1\u7b49\u529f\u80fd\u7684\u6838\u5fc3\u673a\u5236\u3002<\/p>\n<h5>\u4e8c\u3001beforeEach \u5b88\u536b\u7684\u53c2\u6570\u89e3\u6790<\/h5>\n<\/p>\n<p>router.beforeEach((to, from, next) &#061;&gt; {<br \/>\n  \/\/ \u5b88\u536b\u903b\u8f91<br \/>\n})<\/p>\n<ul>\n<li>to: \u5373\u5c06\u8981\u8fdb\u5165\u7684\u76ee\u6807\u8def\u7531\u5bf9\u8c61<\/li>\n<li>from: \u5f53\u524d\u5bfc\u822a\u6b63\u8981\u79bb\u5f00\u7684\u8def\u7531\u5bf9\u8c61<\/li>\n<li>next: \u5bfc\u822a\u63a7\u5236\u51fd\u6570&#xff0c;\u5fc5\u987b\u8c03\u7528\u6b64\u51fd\u6570\u624d\u80fd\u7ee7\u7eed\u5bfc\u822a\u8fc7\u7a0b<\/li>\n<\/ul>\n<h4>\u4e09\u3001next \u51fd\u6570\u7684\u6838\u5fc3\u4f5c\u7528<\/h4>\n<p>next\u51fd\u6570\u662f\u5bfc\u822a\u5b88\u536b\u7684\u5173\u952e\u63a7\u5236\u63a5\u53e3&#xff0c;\u5b83\u7684\u4f5c\u7528\u662f\u51b3\u5b9a\u8def\u7531\u5bfc\u822a\u7684\u540e\u7eed\u52a8\u4f5c\u3002\u5fc5\u987b\u8c03\u7528 next () \u624d\u80fd\u8ba9\u8def\u7531\u5bfc\u822a\u7ee7\u7eed&#xff0c;\u5426\u5219\u5bfc\u822a\u4f1a\u88ab\u963b\u585e\u3002<\/p>\n<\/p>\n<p>\u6839\u636e\u8c03\u7528\u65b9\u5f0f\u7684\u4e0d\u540c&#xff0c;next () \u53ef\u4ee5\u5b9e\u73b0\u4e09\u79cd\u5bfc\u822a\u63a7\u5236&#xff1a;<\/p>\n<\/p>\n<li>next(): \u5141\u8bb8\u5bfc\u822a\u7ee7\u7eed&#xff0c;\u8fdb\u5165\u76ee\u6807\u8def\u7531<\/li>\n<li>next(false): \u963b\u6b62\u5bfc\u822a&#xff0c;\u8fd4\u56de\u5f53\u524d\u8def\u7531&#xff08;\u5982\u679c\u662f\u4ece\u5176\u4ed6\u8def\u7531\u5bfc\u822a\u8fc7\u6765&#xff09;<\/li>\n<li>next({ path: &#039;\/new-path&#039; }): \u91cd\u5b9a\u5411\u5230\u65b0\u8def\u7531&#xff0c;\u4e2d\u65ad\u5f53\u524d\u5bfc\u822a\u5e76\u5f00\u59cb\u65b0\u7684\u5bfc\u822a\u8fc7\u7a0b<\/li>\n<h4>\u56db\u3001\u4ee3\u7801\u4e2d\u7684 next \u51fd\u6570\u5e94\u7528\u573a\u666f<\/h4>\n<h5>1. \u672a\u767b\u5f55\u65f6\u91cd\u5b9a\u5411\u5230\u767b\u5f55\u9875<\/h5>\n<\/p>\n<p>if (to.name !&#061;&#061; &#039;Login&#039; &amp;&amp; !isAuth) {<br \/>\n  next({ name: &#039;Login&#039; })  \/\/ \u91cd\u5b9a\u5411\u5230\u767b\u5f55\u9875<br \/>\n}<\/p>\n<ul>\n<li>\u5f53\u76ee\u6807\u8def\u7531\u4e0d\u662f\u767b\u5f55\u9875\u4e14\u7528\u6237\u672a\u8ba4\u8bc1\u65f6<\/li>\n<li>\u901a\u8fc7next({ name: &#039;Login&#039; })\u91cd\u5b9a\u5411\u5230\u767b\u5f55\u8def\u7531<\/li>\n<li>\u8fd9\u4f1a\u4e2d\u65ad\u5f53\u524d\u5bfc\u822a&#xff0c;\u5f00\u59cb\u5411\u767b\u5f55\u9875\u7684\u65b0\u5bfc\u822a<\/li>\n<\/ul>\n<h5>2. \u5df2\u767b\u5f55\u65f6\u7981\u6b62\u8bbf\u95ee\u767b\u5f55\u9875<\/h5>\n<\/p>\n<p>else if (to.name &#061;&#061;&#061; &#039;Login&#039; &amp;&amp; isAuth) {<br \/>\n  next({ path: &#039;\/home&#039; })  \/\/ \u91cd\u5b9a\u5411\u5230\u9996\u9875<br \/>\n}<\/p>\n<ul>\n<li>\u5f53\u7528\u6237\u5df2\u767b\u5f55\u5374\u5c1d\u8bd5\u8bbf\u95ee\u767b\u5f55\u9875\u65f6<\/li>\n<li>\u901a\u8fc7next({ path: &#039;\/home&#039; })\u91cd\u5b9a\u5411\u5230\u9996\u9875<\/li>\n<li>\u786e\u4fdd\u5df2\u767b\u5f55\u7528\u6237\u4e0d\u4f1a\u770b\u5230\u767b\u5f55\u754c\u9762<\/li>\n<\/ul>\n<h5>3. \u5141\u8bb8\u6b63\u5e38\u5bfc\u822a<\/h5>\n<\/p>\n<p>else {<br \/>\n  next()  \/\/ \u5141\u8bb8\u5bfc\u822a\u7ee7\u7eed<br \/>\n}<\/p>\n<ul>\n<li>\u5f53\u5bfc\u822a\u7b26\u5408\u6761\u4ef6&#xff08;\u5982\u5df2\u767b\u5f55\u4e14\u8bbf\u95ee\u975e\u767b\u5f55\u9875&#xff09;\u65f6<\/li>\n<li>\u8c03\u7528next()\u8ba9\u5bfc\u822a\u8fc7\u7a0b\u7ee7\u7eed&#xff0c;\u8fdb\u5165\u76ee\u6807\u8def\u7531<\/li>\n<\/ul>\n<h4>\u4e94\u3001next \u51fd\u6570\u7684\u4f7f\u7528\u89c4\u5219\u4e0e\u6ce8\u610f\u4e8b\u9879<\/h4>\n<h5>1. \u5fc5\u987b\u8c03\u7528 next ()<\/h5>\n<ul>\n<li>\u65e0\u8bba\u4f60\u60f3\u5141\u8bb8\u5bfc\u822a\u3001\u963b\u6b62\u5bfc\u822a\u8fd8\u662f\u91cd\u5b9a\u5411&#xff0c;\u90fd\u5fc5\u987b\u8c03\u7528 next ()<\/li>\n<li>\u5982\u679c\u4e0d\u8c03\u7528 next ()&#xff0c;\u5bfc\u822a\u5c06\u88ab\u6c38\u4e45\u963b\u585e&#xff0c;\u9875\u9762\u4e0d\u4f1a\u6709\u4efb\u4f55\u53cd\u5e94<\/li>\n<\/ul>\n<h5>2. next () \u7684\u53c2\u6570\u7c7b\u578b<\/h5>\n<table>\n<tr>\u53c2\u6570\u7c7b\u578b\u793a\u4f8b\u4f5c\u7528\u63cf\u8ff0<\/tr>\n<tbody>\n<tr>\n<td>\u65e0\u53c2\u6570<\/td>\n<td>next()<\/td>\n<td>\u7ee7\u7eed\u5bfc\u822a\u5230\u76ee\u6807\u8def\u7531<\/td>\n<\/tr>\n<tr>\n<td>\u5e03\u5c14\u503c<\/td>\n<td>next(false)<\/td>\n<td>\u963b\u6b62\u5bfc\u822a&#xff0c;\u8fd4\u56de\u539f\u8def\u7531<\/td>\n<\/tr>\n<tr>\n<td>\u8def\u7531\u5bf9\u8c61<\/td>\n<td>next({ path: &#039;\/login&#039; })<\/td>\n<td>\u91cd\u5b9a\u5411\u5230\u6307\u5b9a\u8def\u7531<\/td>\n<\/tr>\n<tr>\n<td>\u9519\u8bef\u5bf9\u8c61<\/td>\n<td>next (new Error (&#039; \u5bfc\u822a\u9519\u8bef &#039;))<\/td>\n<td>\u5bfc\u822a\u5931\u8d25\u5e76\u89e6\u53d1\u9519\u8bef\u5904\u7406<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h5>3. \u8c03\u7528\u65f6\u673a\u4e0e\u987a\u5e8f<\/h5>\n<ul>\n<li>\u5728\u5b88\u536b\u4e2d\u53ef\u4ee5\u6709\u591a\u4e2a\u6761\u4ef6\u5224\u65ad&#xff0c;\u4f46\u6700\u7ec8\u5fc5\u987b\u6709\u4e00\u4e2a next () \u88ab\u8c03\u7528<\/li>\n<li>\u591a\u4e2a\u5168\u5c40\u5b88\u536b\u4f1a\u6309\u7167\u6ce8\u518c\u987a\u5e8f\u4f9d\u6b21\u6267\u884c&#xff0c;\u6bcf\u4e2a\u5b88\u536b\u90fd\u5fc5\u987b\u8c03\u7528 next () \u624d\u80fd\u7ee7\u7eed<\/li>\n<\/ul>\n<h5>4. \u5f02\u6b65\u573a\u666f\u5904\u7406<\/h5>\n<ul>\n<li>\u5982\u679c\u5b88\u536b\u4e2d\u9700\u8981\u5f02\u6b65\u64cd\u4f5c&#xff08;\u5982 API \u8bf7\u6c42\u9a8c\u8bc1&#xff09;&#xff0c;\u5fc5\u987b\u5728\u5f02\u6b65\u64cd\u4f5c\u5b8c\u6210\u540e\u8c03\u7528 next ()<\/li>\n<li>\u793a\u4f8b&#xff1a;\n<\/p>\n<p> router.beforeEach(async (to, from, next) &#061;&gt; {<br \/>\n  try {<br \/>\n    const userData &#061; await fetchUserInfo()<br \/>\n    if (userData.isAuth) {<br \/>\n      next()<br \/>\n    } else {<br \/>\n      next({ name: &#039;Login&#039; })<br \/>\n    }<br \/>\n  } catch (error) {<br \/>\n    next(error)<br \/>\n  }<br \/>\n})\n <\/li>\n<\/ul>\n<h4><\/h4>\n<h4>\u516d\u3001\u5b9e\u9645\u5f00\u53d1\u4e2d\u7684\u6700\u4f73\u5b9e\u8df5<\/h4>\n<li>\n<p>\u6743\u9650\u63a7\u5236&#xff1a;<\/p>\n<ul>\n<li>\u5728 beforeEach \u4e2d\u6821\u9a8c\u7528\u6237\u6743\u9650<\/li>\n<li>\u5bf9\u672a\u6388\u6743\u7528\u6237\u91cd\u5b9a\u5411\u5230\u767b\u5f55\u9875\u6216\u63d0\u793a\u9519\u8bef<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u767b\u5f55\u72b6\u6001\u7ef4\u62a4&#xff1a;<\/p>\n<ul>\n<li>\u68c0\u67e5\u7528\u6237\u767b\u5f55\u72b6\u6001&#xff08;\u5982 token \u6709\u6548\u6027&#xff09;<\/li>\n<li>\u8fc7\u671f\u65f6\u5f3a\u5236\u91cd\u65b0\u767b\u5f55<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u9875\u9762\u8bbf\u95ee\u8bb0\u5f55&#xff1a;<\/p>\n<ul>\n<li>\u8bb0\u5f55\u7528\u6237\u8bbf\u95ee\u8def\u5f84&#xff0c;\u7528\u4e8e\u5386\u53f2\u56de\u6eaf\u6216\u884c\u4e3a\u5206\u6790<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u8d44\u6e90\u9884\u52a0\u8f7d&#xff1a;<\/p>\n<ul>\n<li>\u5728\u5bfc\u822a\u524d\u9884\u52a0\u8f7d\u76ee\u6807\u9875\u9762\u6240\u9700\u7684\u6570\u636e\u6216\u8d44\u6e90<\/li>\n<\/ul>\n<\/li>\n<h4><\/h4>\n<p>next\u51fd\u6570\u662f Vue Router \u5bfc\u822a\u5b88\u536b\u7684 &#034;\u4ea4\u901a\u4fe1\u53f7\u706f&#034;&#xff0c;\u901a\u8fc7\u63a7\u5236\u5b83\u7684\u8c03\u7528\u65b9\u5f0f&#xff0c;\u4f60\u53ef\u4ee5\u7cbe\u786e\u7ba1\u7406\u8def\u7531\u5bfc\u822a\u7684\u6d41\u7a0b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.3k\u6b21\uff0c\u70b9\u8d5e25\u6b21\uff0c\u6536\u85cf12\u6b21\u3002template&gt;App\u6839\u7ec4\u4ef6\u9996\u9875\u5173\u4e8e&#8211;\u5934\u90e8 start&#8211;&gt;\u4e1c\u8f6f\u4e91\u533b\u9662HIS\u7cfb\u7edf\u6211\u7684\u8bbe\u7f6e<\/p>\n","protected":false},"author":2,"featured_media":44488,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[187,625,87],"topic":[],"class_list":["post-44491","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-javascript","tag-vue-js","tag-87"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>4\u6b65\u4f7f\u7528 vue3 \u8def\u7531 - \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\/44491.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"4\u6b65\u4f7f\u7528 vue3 \u8def\u7531 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.3k\u6b21\uff0c\u70b9\u8d5e25\u6b21\uff0c\u6536\u85cf12\u6b21\u3002template&gt;App\u6839\u7ec4\u4ef6\u9996\u9875\u5173\u4e8e--\u5934\u90e8 start--&gt;\u4e1c\u8f6f\u4e91\u533b\u9662HIS\u7cfb\u7edf\u6211\u7684\u8bbe\u7f6e\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/44491.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-06-19T01:27:41+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250619012739-6853678be6cbf.png\" \/>\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=\"8 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/44491.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/44491.html\",\"name\":\"4\u6b65\u4f7f\u7528 vue3 \u8def\u7531 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-06-19T01:27:41+00:00\",\"dateModified\":\"2025-06-19T01:27:41+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/44491.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/44491.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/44491.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"4\u6b65\u4f7f\u7528 vue3 \u8def\u7531\"}]},{\"@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":"4\u6b65\u4f7f\u7528 vue3 \u8def\u7531 - \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\/44491.html","og_locale":"zh_CN","og_type":"article","og_title":"4\u6b65\u4f7f\u7528 vue3 \u8def\u7531 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.3k\u6b21\uff0c\u70b9\u8d5e25\u6b21\uff0c\u6536\u85cf12\u6b21\u3002template&gt;App\u6839\u7ec4\u4ef6\u9996\u9875\u5173\u4e8e--\u5934\u90e8 start--&gt;\u4e1c\u8f6f\u4e91\u533b\u9662HIS\u7cfb\u7edf\u6211\u7684\u8bbe\u7f6e","og_url":"https:\/\/www.wsisp.com\/helps\/44491.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-06-19T01:27:41+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250619012739-6853678be6cbf.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"8 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/44491.html","url":"https:\/\/www.wsisp.com\/helps\/44491.html","name":"4\u6b65\u4f7f\u7528 vue3 \u8def\u7531 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-06-19T01:27:41+00:00","dateModified":"2025-06-19T01:27:41+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/44491.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/44491.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/44491.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"4\u6b65\u4f7f\u7528 vue3 \u8def\u7531"}]},{"@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\/44491","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=44491"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/44491\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/44488"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=44491"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=44491"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=44491"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=44491"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}