{"id":3086,"date":"2020-09-24T16:25:13","date_gmt":"2020-09-24T08:25:13","guid":{"rendered":"http:\/\/www.yuguoxy.com\/geekhome\/?p=3086"},"modified":"2026-08-18T12:35:33","modified_gmt":"2026-08-18T04:35:33","slug":"vue3-0%e5%85%a8%e5%ae%b6%e6%a1%b6%e6%9c%80%e5%85%a8%e5%85%a5%e9%97%a8%e6%8c%87%e5%8d%97-vue-router4-x%e5%92%8cvuex4-x-34","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=3086","title":{"rendered":"Vue3.0\u5168\u5bb6\u6876\u6700\u5168\u5165\u95e8\u6307\u5357 &#8211; vue-router@4.x\u548cvuex@4.x (3\/4)"},"content":{"rendered":"<section id=\"wenyan\" style=\"font-family: system-ui, 'Apple Color Emoji', 'Segoe UI', 'Segoe UI Symbol', 'Noto Sans', Roboto, sans-serif; line-height: 1.75; font-size: 16px; color: #000000; caret-color: #000000;\" data-provider=\"WenYan\">\n<p style=\"margin: 1em 0px;\">Vue2 \u642d\u914d\u7684\u662f <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">vue-router@3.x<\/code> \u548c <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">vuex@3.x<\/code>\uff0cVue3 \u5219\u6362\u6210 <a href=\"mailto:vue-router@4.x\">vue-router@4.x<\/a> \u548c <a href=\"mailto:vuex@4.x\">vuex@4.x<\/a>\u3002\u7248\u672c\u53f7\u90fd\u662f 4.x\uff0c\u4e0d\u8981\u548c Vue 3.x \u6df7\u4e3a\u4e00\u8c08\u3002\u672c\u6587\u662f\u5168\u5bb6\u6876\u7cfb\u5217\u7b2c 3 \u7bc7\uff0c\u5bf9\u7167\u5199\u6cd5\u770b\u8fc1\u79fb\u70b9\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u4e3a\u65b9\u4fbf\u53d9\u8ff0\uff0c\u6587\u4e2d\u7528 <strong style=\"color: #4870ac;\">vue2.x router<\/strong>\u3001<strong style=\"color: #4870ac;\">vue3.x router<\/strong> \u4ee3\u66ff <a href=\"mailto:vue-router@3.x\">vue-router@3.x<\/a> \u548c <a href=\"mailto:vue-router@4.x\">vue-router@4.x<\/a>\uff0c\u7528 <strong style=\"color: #4870ac;\">vue2.x vuex<\/strong>\u3001<strong style=\"color: #4870ac;\">vue3.x vuex<\/strong> \u4ee3\u66ff <a href=\"mailto:vuex@3.x\">vuex@3.x<\/a> \u548c <a href=\"mailto:vuex@4.x\">vuex@4.x<\/a>\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u7cfb\u5217\u76ee\u5f55\uff1a<\/p>\n<ul style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem; list-style-type: disc;\">\n<li>\n<section><a href=\"https:\/\/ai.zhousir.top\/archives\/3074\">Vue3.0\u5168\u5bb6\u6876\u6700\u5168\u5165\u95e8\u6307\u5357 &#8211; \u5feb\u901f\u642d\u5efa (1\/4)<\/a><\/section>\n<\/li>\n<li>\n<section><a href=\"https:\/\/ai.zhousir.top\/archives\/3082\">Vue3.0\u5168\u5bb6\u6876\u6700\u5168\u5165\u95e8\u6307\u5357 &#8211; vue3.0\u65b0\u7279\u6027 (2\/4)<\/a><\/section>\n<\/li>\n<li>\n<section><a href=\"https:\/\/ai.zhousir.top\/archives\/3086\">Vue3.0\u5168\u5bb6\u6876\u6700\u5168\u5165\u95e8\u6307\u5357 &#8211; vue-router@4.x\u548cvuex@4.x (3\/4)<\/a><\/section>\n<\/li>\n<li>\n<section><a href=\"https:\/\/juejin.im\/post\/6867123749565497358\">Vue3.0\u5168\u5bb6\u6876\u6700\u5168\u5165\u95e8\u6307\u5357 &#8211;<\/a> <a href=\"https:\/\/juejin.im\/post\/6867123749565497358\">3.x\u8ddf2.x\u7684\u5176\u4ed6\u5dee\u5f02<\/a> <a href=\"https:\/\/juejin.im\/post\/6867123749565497358\">(4\/4)<\/a><\/section>\n<\/li>\n<\/ul>\n<table style=\"display: table; text-align: left; overflow: auto; border-collapse: collapse; margin: 1.4em auto; max-width: 100%; table-layout: fixed; overflow-wrap: break-word; word-break: break-all;\" width=\"800\">\n<thead>\n<tr>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u70b9<\/th>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">vue2.x<\/th>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">vue3.x<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u521b\u5efa\u8def\u7531<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">Vue.use(Router); new Router<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">createRouter + createWebHistory<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u6eda\u52a8<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">{ x, y, selector, offset }<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">{ el, left, top, behavior }<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u7236\u7ea7 redirect<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u8def\u7531\u9009\u9879 redirect<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u5b50\u8def\u7531 path: &#8221; + redirect<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u901a\u914d<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">path: &#8216;\/user\/:a*&#8217;<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\/:catchAll(.*)<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u5f53\u524d\u5339\u914d\u7ec4\u4ef6<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">router.getMatchedComponents<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">currentRoute.value.matched<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u62ff store\/router<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">this.$store \/ this.$router<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">useStore \/ useRouter\uff0c\u4e0d\u8981\u7528 ctx<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e00\u3001<a href=\"mailto:vue-router@4.x\">vue-router@4.x<\/a><\/h2>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">\u521b\u5efa\u5b9e\u4f8b<\/h3>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">\/\/ vue2.x router\nimport Vue from 'vue'\nimport Router from 'vue-router'\n\nconst routes = [\n  {\n    path: '\/',\n    name: 'Home',\n    component: Home\n  },\n  {\n    path: '\/about',\n    name: 'About',\n    \/\/ route level code-splitting\n    \/\/ this generates a separate chunk (about.[hash].js) for this route\n    \/\/ which is lazy-loaded when the route is visited.\n    component: () => import(\/* webpackChunkName: \"about\" *\/ '..\/views\/About.vue')\n  }\n]\n\nVue.use(Router)\n\nconst router = new Router({\n  base: process.env.BASE_URL,\n  mode: 'history',\n  scrollBehavior: () => ({ y: 0 }),\n  routes\n})\n\nexport default router\n\n\/\/ vue3.x router\nimport { createRouter, createWebHistory } from 'vue-router'\nimport Home from '..\/views\/Home.vue'\n\nconst routes = [\n  {\n    path: '\/',\n    name: 'Home',\n    component: Home\n  },\n  {\n    path: '\/about',\n    name: 'About',\n    \/\/ route level code-splitting\n    \/\/ this generates a separate chunk (about.[hash].js) for this route\n    \/\/ which is lazy-loaded when the route is visited.\n    component: () => import(\/* webpackChunkName: \"about\" *\/ '..\/views\/About.vue')\n  }\n]\n\nconst router = createRouter({\n  history: createWebHistory(process.env.BASE_URL),\n  routes\n})\n\nexport default router<\/code><\/pre>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">scrollBehavior \u6eda\u52a8\u884c\u4e3a<\/h3>\n<p style=\"margin: 1em 0px;\">vue3 \u5f03\u7528 vue2 \u7684 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">{ selector, x, y, offset }<\/code>\uff0c\u6539\u7528\u66f4\u63a5\u8fd1\u539f\u751f DOM \u7684 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">{ el, left, top, behavior }<\/code>\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">\/\/ vue2.x router\nconst router = new Router({\n  base: process.env.BASE_URL,\n  mode: 'history',\n  scrollBehavior: () => ({ x: 0, y: 0 }),\n  routes\n})\n\n\/\/ vue3.x router\nconst router = createRouter({\n  history: createWebHistory(process.env.BASE_URL),\n  routes,\n  scrollBehavior(to, from, savedPosition) {\n    \/\/ scroll to id `#app` + 200px, and scroll smoothly (when supported by the browser)\n    return {\n      el: '#app',\n      top: 0,\n      left: 0,\n      behavior: 'smooth'\n    }\n  }\n})<\/code><\/pre>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">\u8def\u7531\u7ec4\u4ef6\u8df3\u8f6c<\/h3>\n<p style=\"margin: 1em 0px;\">vue2 \u7528\u8def\u7531\u9009\u9879 redirect \u81ea\u52a8\u8df3\uff1bvue3 \u53bb\u6389\u8be5\u9009\u9879\uff0c\u6539\u4e3a\u5728\u5b50\u8def\u7531\u91cc\u52a0\u4e00\u4e2a\u7a7a\u8def\u5f84\u6765\u5339\u914d\u8df3\u8f6c\u3002<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">\/\/ vue2.x router\n[\n  {\n    path: '\/',\n    component: Layout,\n    name: 'WebHome',\n    meta: { title: '\u5e73\u53f0\u9996\u9875' },\n    redirect: '\/dashboard', \/\/ \u8fd9\u91cc\u5199\u8df3\u8f6c\n    children: [\n      {\n        path: 'dashboard',\n        name: 'Dashboard',\n        meta: { title: '\u5de5\u4f5c\u53f0' },\n        component: () => import('..\/views\/dashboard\/index.vue')\n      }\n    ]\n  }\n]\n\n\/\/ vue3.x router\n[\n  {\n    path: '\/',\n    component: Layout,\n    name: 'WebHome',\n    meta: { title: '\u5e73\u53f0\u9996\u9875' },\n    children: [\n      { path: '', redirect: 'dashboard' }, \/\/ \u8fd9\u91cc\u5199\u8df3\u8f6c\n      {\n        path: 'dashboard',\n        name: 'Dashboard',\n        meta: { title: '\u5de5\u4f5c\u53f0' },\n        component: () => import('..\/views\/dashboard\/index.vue')\n      }\n    ]\n  }\n]<\/code><\/pre>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">\u6355\u83b7\u6240\u6709\u8def\u7531\uff1a\/:catchAll(.*)<\/h3>\n<p style=\"margin: 1em 0px;\">\u6355\u83b7 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">(\/*)<\/code> \u65f6\u5fc5\u987b\u7528\u81ea\u5b9a\u4e49\u6b63\u5219\u53c2\u6570 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">\/:catchAll(.*)<\/code>\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">\/\/ vue2.x router\nconst router = new VueRouter({\n  mode: 'history',\n  routes: [\n    { path: '\/user\/:a*' },\n  ]\n})\n\n\/\/ vue3.x router\nconst router = createRouter({\n  history: createWebHistory(),\n  routes: [\n    { path: '\/user\/:a:catchAll(.*)', component: component },\n  ]\n})<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u5f53\u8def\u7531\u4e3a <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">\/user\/a\/b<\/code> \u65f6\uff0c\u6355\u83b7\u5230\u7684 params \u4e3a <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">{{\"a\": \"a\", \"catchAll\": \"\/b\"}}<\/code>\u3002<\/p>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">router.resolve<\/h3>\n<p style=\"margin: 1em 0px;\"><code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">router.match<\/code> \u4e0e <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">router.resolve<\/code> \u5408\u5e76\u4e3a <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">router.resolve<\/code>\uff0c\u7b7e\u540d\u7565\u6709\u4e0d\u540c\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">\/\/ vue2.x router\n...\nresolve ( to: RawLocation, current?: Route, append?: boolean) {\n  ...\n  return {\n    location,\n    route,\n    href,\n    normalizedTo: location,\n    resolved: route\n  }\n}\n\n\/\/ vue3.x router\nfunction resolve(\n    rawLocation: Readonly&lt;RouteLocationRaw>,\n    currentLocation?: Readonly&lt;RouteLocationNormalizedLoaded>\n  ): RouteLocation &amp; { href: string } {\n  ...\n  let matchedRoute = matcher.resolve(matcherLocation, currentLocation)\n  ...\n  return {\n    fullPath,\n    hash,\n    query: normalizeQuery(rawLocation.query),\n    ...matchedRoute,\n    redirectedFrom: undefined,\n    href: routerHistory.base + fullPath,\n  }\n}<\/code><\/pre>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">\u83b7\u53d6\u5f53\u524d\u8def\u7531<\/h3>\n<p style=\"margin: 1em 0px;\">\u5220\u9664 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">router.getMatchedComponents<\/code>\uff0c\u6539\u4ece <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">router.currentRoute.value.matched<\/code> \u53d6\u3002\u539f\u6587\u8bf4\u660e\uff1a\u8be5\u65b9\u6cd5\u8fd4\u56de\u76ee\u6807\u4f4d\u7f6e\u6216\u5f53\u524d\u8def\u7531\u5339\u914d\u7684\u7ec4\u4ef6\u6570\u7ec4\uff08\u662f\u5b9a\u4e49\/\u6784\u9020\u7c7b\uff0c\u4e0d\u662f\u5b9e\u4f8b\uff09\uff0c\u5e38\u7528\u4e8e SSR \u6570\u636e\u9884\u52a0\u8f7d\u3002<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">[{\n  aliasOf: undefined\n  beforeEnter: undefined\n  children: []\n  components: {default: {\u2026}, other: {\u2026}}\n  instances: {default: null, other: Proxy}\n  leaveGuards: []\n  meta: {}\n  name: undefined\n  path: \"\/\"\n  props: \u0192 (to)\n  updateGuards: []\n}]<\/code><\/pre>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">\u4f7f\u7528\u4e0e\u5c31\u7eea<\/h3>\n<p style=\"margin: 1em 0px;\">\u5982\u679c\u4f7f\u7528\u8fc7\u6e21\uff0c\u53ef\u80fd\u9700\u8981\u7b49 router \u5c31\u7eea\u518d\u6302\u8f7d\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">app.use(router)\n\/\/ Note: on Server Side, you need to manually push the initial location\nrouter.isReady().then(() => app.mount('#app'))<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u4e00\u822c\u76f4\u63a5\u6302\u8f7d\u4e5f\u80fd\u7528\u3002\u4f46\u5bfc\u822a\u5df2\u662f\u5f02\u6b65\u7684\uff0c\u521d\u59cb\u5316\u65f6\u82e5\u6709\u5b88\u536b\uff0c\u5728 resolve \u4e4b\u524d\u4f1a\u51fa\u73b0\u4e00\u6b21\u521d\u59cb\u6e32\u67d3\u8fc7\u6e21\uff0c\u7c7b\u4f3c\u7ed9\u8fc7\u6e21\u63d0\u4f9b <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">appear<\/code>\u3002<\/p>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">SSR \u8981\u4f20\u5408\u9002\u7684 history mode<\/h3>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">const history = isServer ? createMemoryHistory() : createWebHistory()\nconst router = createRouter({ routes, history })\n\/\/ on server only\nrouter.push(req.url) \/\/ request url\nrouter.isReady().then(() => {\n  \/\/ resolve the request\n})<\/code><\/pre>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">push \/ resolve \u4e0d\u5b58\u5728\u7684\u547d\u540d\u8def\u7531\u4f1a\u62a5\u9519<\/h3>\n<p style=\"margin: 1em 0px;\">vue2 \u91cc push \u4e00\u4e2a\u4e0d\u5b58\u5728\u7684 name\uff0c\u4f1a\u5bfc\u822a\u5230\u6839\u8def\u7531 \/ \u4e14\u4e0d\u6e32\u67d3\u5185\u5bb9\uff0c\u63a7\u5236\u53f0\u8b66\u544a\uff0cURL \u8df3\u5230 \/\u3002<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">const router = new VueRouter({\n  mode: 'history',\n  routes: [{ path: '\/', name: 'foo', component: Foo }]\n}\nthis.$router.push({ name: 'baz' })<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">vue3 \u540c\u6837\u5199\u6cd5\u4f1a\u629b\u9519\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">const router = createRouter({\n  history: routerHistory(),\n  routes: [{ path: '\/', name: 'foo', component: Foo }]\n})\n...\nimport { useRouter } from 'vue-next-router'\n...\nconst router = userRouter()\nrouter.push({ name: 'baz' })) \/\/ \u8fd9\u884c\u4ee3\u7801\u4f1a\u62a5\u9519<\/code><\/pre>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">\u83b7\u53d6\u8def\u7531\uff1a\u4e0d\u8981\u7528 ctx<\/h3>\n<p style=\"margin: 1em 0px;\">\u7f51\u4e0a\u6709\u6559\u7a0b\u901a\u8fc7 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">ctx<\/code> \u8bbf\u95ee router \u548c store\u3002\u8fd9\u53ea\u5728 development \u6709\u6548\uff0cproduction \u7f16\u8bd1\u540e\u90a3\u4e9b\u5c5e\u6027\u8bbf\u95ee\u4e0d\u5230\uff0c\u5bb9\u6613\u8bef\u5bfc\u3002<\/p>\n<p style=\"margin: 1em 0px;\"><strong style=\"color: #4870ac;\">\u9519\u8bef\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">import { getCurrentInstance } from 'vue'\n\nexport default {\n  setup () {\n    const { ctx } = getCurrentInstance()\n    console.log(ctx)\n    console.log(ctx.$router.currentRoute.value)\n    const userId = computed(() => ctx.$store.state.app.userId)\n    return {\n      userId\n    }\n  }\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\"><strong style=\"color: #4870ac;\">\u6b63\u786e\u4f7f\u7528\uff1a<\/strong>\u7528 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">useRouter<\/code>\u3001<code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">useRoute<\/code>\u3001<code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">useStore<\/code>\u3002<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">import { getCurrentInstance } from 'vue'\nimport { useRouter, useRoute } from 'vue-router'\nimport { useStore } from 'vuex'\n\nexport default {\n  setup () {\n    const { ctx } = getCurrentInstance()\n    console.log(ctx)\n    const router = useRouter()\n    const route = useRoute()\n    const store = userStore()\n    console.log(router, route, store)\n    console.log(router.currentRoute.value)\n    const userId = computed(() => store.state.app.userId)\n    return {\n      userId\n    }\n  }\n}<\/code><\/pre>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e8c\u3001<a href=\"mailto:vuex@4.x\">vuex@4.x<\/a><\/h2>\n<p style=\"margin: 1em 0px;\">vuex 4.x breaking change \u5f88\u5c11\uff0c\u6574\u4f53\u6539\u52a8\u4e0d\u5927\u3002<\/p>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">\u521b\u5efa\u5b9e\u4f8b<\/h3>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">\/\/ vue2.x vuex\nimport Vue from 'vue'\nimport Vuex from 'vuex'\n\nVue.use(Vuex)\n\nexport default new Vuex.Store({\n  state: {},\n  mutations: {},\n  actions: {},\n  getters: {},\n  modules: {}\n}\n\n\/\/ vue3.x vuex\nimport Vuex from 'vuex'\n\nexport default Vuex.createStore({\n  state: {},\n  mutations: {},\n  actions: {},\n  getters: {},\n  modules: {}\n})<\/code><\/pre>\n<h3 style=\"padding: 0px; color: #4870ac; margin: 1.2em 0px 1em; font-size: 1.3em;\">\u83b7\u53d6 store<\/h3>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">\/\/ vue3.x vuex\nimport { getCurrentInstance } from 'vue'\nimport { useRouter, useRoute } from 'vue-router'\nimport { useStore } from 'vuex'\n\nexport default {\n  setup () {\n    const { ctx } = getCurrentInstance()\n    console.log(ctx)\n    const router = useRouter()\n    const route = useRoute()\n    const store = userStore()\n    console.log(router, route, store)\n    console.log(router.currentRoute.value)\n    const userId = computed(() => store.state.app.userId)\n    return {\n      userId\n    }\n  }\n}<\/code><\/pre>\n<blockquote style=\"display: block; font-size: 0.9em; border-left: 3px solid #4870ac; padding: 0.5em 1em; margin: 0px; background: #f6f8fa; color: #40464f;\">\n<p style=\"margin: 1em 0px;\"><strong style=\"color: #4870ac;\">\u4e00\u53e5\u8bdd\u603b\u7ed3\uff1aVue3 \u8def\u7531\u7528 createRouter \/ createWebHistory\uff0c\u901a\u914d\u6539 catchAll\uff0c\u5b88\u536b\u5f02\u6b65\u8981\u7b49 isReady\uff1b\u53d6\u5b9e\u4f8b\u7528 useRouter\/useStore\uff0c\u4e0d\u8981\u4f9d\u8d56 setup \u91cc\u7684 ctx\u3002<\/strong><\/p>\n<\/blockquote>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>Vue2 \u642d\u914d\u7684\u662f vue-router@3.x \u548c vuex@3.x\uff0cVue3 \u5219\u6362\u6210 vue-router [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[106,105],"tags":[205,206],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/3086"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3086"}],"version-history":[{"count":1,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/3086\/revisions"}],"predecessor-version":[{"id":3087,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/3086\/revisions\/3087"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3086"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3086"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3086"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}