{"id":4327,"date":"2025-06-12T08:54:59","date_gmt":"2025-06-17T01:34:58","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=4327"},"modified":"2025-06-17T08:54:59","modified_gmt":"2025-06-17T01:34:58","slug":"%e5%ad%b8%e7%bf%92vue-js%e7%b5%84%e4%bb%b6%e5%b5%8c%e5%a5%97%e8%b7%af%e7%94%b1%ef%bc%9a%e6%8e%8c%e6%8f%a1router-view%e7%9a%84%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/vue-js\/%e5%ad%b8%e7%bf%92vue-js%e7%b5%84%e4%bb%b6%e5%b5%8c%e5%a5%97%e8%b7%af%e7%94%b1%ef%bc%9a%e6%8e%8c%e6%8f%a1router-view%e7%9a%84%e5%8a%9f%e8%83%bd\/","title":{"rendered":"\u5168\u9762\u638c\u63e1 Vue.js \u7d44\u4ef6\u5d4c\u5957\u8def\u7531\uff08router-view\uff09\uff1a2025 \u6700\u65b0\u6559\u5b78\u8207\u5be6\u4f5c\u7bc4\u4f8b"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Vue.js, \u7d44\u4ef6\u5d4c\u5957\u8def\u7531, router-view, Vue Router, \u7db2\u9801\u958b\u767c\"><\/p>\n<h2>\u7c21\u4ecb<\/h2>\n<p>Vue.js \u7684\u7d44\u4ef6\u5d4c\u5957\u8def\u7531\uff08router-view\uff09\u662f\u4e00\u500b\u5f37\u5927\u7684\u529f\u80fd\uff0c\u8b93\u958b\u767c\u8005\u80fd\u5920\u5c07\u7db2\u9801\u7684\u5167\u5bb9\u5206\u5272\u6210\u4e0d\u540c\u7684\u7d44\u4ef6\uff0c\u4e26\u8f15\u9b06\u5730\u5c07\u5b83\u5011\u7d44\u5408\u5728\u4e00\u8d77\uff0c\u4ee5\u69cb\u5efa\u51fa\u4e00\u500b\u5b8c\u6574\u7684\u7db2\u7ad9\u3002\u900f\u904e\u9019\u7bc7\u6587\u7ae0\uff0c\u60a8\u5c07\u5b78\u6703\u5982\u4f55\u4f7f\u7528 Vue.js \u7684\u7d44\u4ef6\u5d4c\u5957\u8def\u7531\u4f86\u69cb\u5efa\u4e00\u500b\u529f\u80fd\u5b8c\u5584\u7684\u7db2\u7ad9\uff0c\u4e26\u4e86\u89e32025\u5e74\u7684\u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2>\u5b89\u88dd Vue Router<\/h2>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5b89\u88dd Vue Router\uff0c\u9019\u662f\u4e00\u500b\u5c08\u70ba Vue.js \u8a2d\u8a08\u7684\u8def\u7531\u7ba1\u7406\u5668\u3002\u53ef\u4ee5\u4f7f\u7528 npm \u6216 yarn \u4f86\u5b89\u88dd\uff1a<\/p>\n<pre><code class=\"language-shell\">npm install vue-router\n<\/code><\/pre>\n<p>\u6216<\/p>\n<pre><code class=\"language-shell\">yarn add vue-router\n<\/code><\/pre>\n<h2>\u5b9a\u7fa9\u8def\u7531<\/h2>\n<p>\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u6211\u5011\u53ef\u4ee5\u958b\u59cb\u5b9a\u7fa9\u8def\u7531\u3002\u8def\u7531\u662f\u4e00\u500b\u5c0d\u61c9 URL \u548c\u7d44\u4ef6\u7684\u5c0d\u61c9\u8868\uff0c\u5b83\u53ef\u4ee5\u8b93\u6211\u5011\u5c07\u4e0d\u540c\u7684 URL \u5c0d\u61c9\u5230\u4e0d\u540c\u7684\u7d44\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u8def\u7531\u5b9a\u7fa9\u7bc4\u4f8b\uff1a<\/p>\n<pre><code class=\"language-javascript\">const routes = [\n  {\n    path: '\/',\n    component: Home\n  },\n  {\n    path: '\/about',\n    component: About\n  }\n]\n<\/code><\/pre>\n<h2>\u5275\u5efa VueRouter \u5be6\u4f8b<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u9700\u8981\u5275\u5efa\u4e00\u500b VueRouter \u5be6\u4f8b\uff0c\u4e26\u5c07\u525b\u525b\u5b9a\u7fa9\u7684\u8def\u7531\u50b3\u5165\uff1a<\/p>\n<pre><code class=\"language-javascript\">const router = new VueRouter({\n  routes\n})\n<\/code><\/pre>\n<h2>\u5c07\u8def\u7531\u6574\u5408\u5230 Vue \u6839\u5be6\u4f8b<\/h2>\n<p>\u6700\u5f8c\uff0c\u6211\u5011\u9700\u8981\u5c07 VueRouter \u5be6\u4f8b\u50b3\u5165 Vue \u6839\u5be6\u4f8b\uff1a<\/p>\n<pre><code class=\"language-javascript\">new Vue({\n  router\n}).$mount('#app')\n<\/code><\/pre>\n<h2>\u4f7f\u7528 <code>&lt;router-view&gt;<\/code><\/h2>\n<p>\u73fe\u5728\uff0c\u6211\u5011\u5df2\u7d93\u53ef\u4ee5\u5728\u7db2\u9801\u4e2d\u4f7f\u7528 Vue.js \u7684\u7d44\u4ef6\u5d4c\u5957\u8def\u7531\u4e86\u3002\u53ea\u9700\u5728\u7db2\u9801\u4e2d\u6dfb\u52a0\u4e00\u500b <code>&lt;router-view&gt;<\/code> \u6a19\u7c64\uff0c\u5c31\u53ef\u4ee5\u5c07\u5c0d\u61c9\u7684\u7d44\u4ef6\u986f\u793a\u51fa\u4f86\uff1a<\/p>\n<pre><code class=\"language-html\">&lt;div id=\"app\"&gt;\n  &lt;router-view&gt;&lt;\/router-view&gt;\n&lt;\/div&gt;\n<\/code><\/pre>\n<h2>\u932f\u8aa4\u6392\u9664\u8207\u5e38\u898b\u554f\u984c<\/h2>\n<p>\u5728\u4f7f\u7528 Vue Router \u6642\uff0c\u958b\u767c\u8005\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u554f\u984c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5efa\u8b70\u7684\u89e3\u6c7a\u65b9\u6848\uff1a<\/p>\n<ul>\n<li><strong>\u8def\u7531\u672a\u6b63\u78ba\u8f09\u5165\uff1a<\/strong>\u78ba\u8a8d\u8def\u7531\u662f\u5426\u6b63\u78ba\u5b9a\u7fa9\uff0c\u4e26\u6aa2\u67e5\u7d44\u4ef6\u662f\u5426\u5df2\u6b63\u78ba\u5f15\u7528\u3002<\/li>\n<li><strong>URL \u8b8a\u66f4\u672a\u66f4\u65b0\u8996\u5716\uff1a<\/strong>\u78ba\u4fdd Vue Router \u5be6\u4f8b\u5df2\u6b63\u78ba\u50b3\u5165 Vue \u6839\u5be6\u4f8b\uff0c\u4e26\u6aa2\u67e5 <code>&lt;router-view&gt;<\/code> \u662f\u5426\u6b63\u78ba\u653e\u7f6e\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u5b78\u6703\u4f7f\u7528\u7d44\u4ef6\u5d4c\u5957\u8def\u7531\u5f8c\uff0c\u60a8\u53ef\u4ee5\u9032\u4e00\u6b65\u63a2\u7d22\u4ee5\u4e0b\u5ef6\u4f38\u61c9\u7528\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528\u5c0e\u822a\u5b88\u885b\u4f86\u63a7\u5236\u8def\u7531\u8a2a\u554f\u3002<\/li>\n<li>\u5be6\u73fe\u52d5\u614b\u8def\u7531\u4ee5\u652f\u6301\u591a\u7a2e\u5167\u5bb9\u5c55\u793a\u3002<\/li>\n<li>\u8207 Vuex \u6574\u5408\u9032\u884c\u72c0\u614b\u7ba1\u7406\u3002<\/li>\n<\/ul>\n<h2>\u7d50\u8ad6<\/h2>\n<p>Vue.js \u7684\u7d44\u4ef6\u5d4c\u5957\u8def\u7531\uff08router-view\uff09\u662f\u4e00\u500b\u9748\u6d3b\u4e14\u9ad8\u6548\u7684\u5de5\u5177\uff0c\u4f7f\u958b\u767c\u8005\u80fd\u5920\u8f15\u9b06\u69cb\u5efa\u548c\u7dad\u8b77\u7db2\u7ad9\u3002\u5e0c\u671b\u9019\u7bc7\u6587\u7ae0\u80fd\u5e6b\u52a9\u60a8\u638c\u63e1\u6700\u65b0\u7684 Vue Router \u4f7f\u7528\u6280\u5de7\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q1: Vue Router \u548c\u5176\u4ed6\u8def\u7531\u76f8\u6bd4\u6709\u4ec0\u9ebc\u512a\u52e2\uff1f<\/h3>\n<p>A1: Vue Router \u662f\u5c08\u70ba Vue.js \u8a2d\u8a08\u7684\u8def\u7531\u7ba1\u7406\u5668\uff0c\u63d0\u4f9b\u4e86\u8207 Vue.js \u6df1\u5ea6\u96c6\u6210\u7684\u529f\u80fd\uff0c\u4e26\u652f\u6301\u52d5\u614b\u8def\u7531\u3001\u5d4c\u5957\u8def\u7531\u548c\u5c0e\u822a\u5b88\u885b\u7b49\u7279\u6027\u3002<\/p>\n<h3>Q2: \u5728 Vue.js \u4e2d\u5982\u4f55\u9032\u884c\u8def\u7531\u904e\u6e21\u6548\u679c\uff1f<\/h3>\n<p>A2: \u60a8\u53ef\u4ee5\u4f7f\u7528 <code>&lt;transition&gt;<\/code> \u6a19\u7c64\u4f86\u5305\u88f9 <code>&lt;router-view&gt;<\/code>\uff0c\u4e26\u4f7f\u7528 CSS \u985e\u4f86\u5b9a\u7fa9\u904e\u6e21\u6548\u679c\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1aVue.js\u7d44\u4ef6\u5d4c\u5957\u8def\u7531\uff08Router-View\uff09\u662f\u4e00\u7a2e\u6280\u8853\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5c07\u7db2\u9801\u5167\u5bb9\u5206\u5272\u6210\u4e0d\u540c\u7684\u7d44\u4ef6\uff0c\u4e26\u5c07\u5b83\u5011\u7d44\u5408\u5728\u4e00\u8d77\uff0c\u4ee5\u69cb\u5efa\u66f4\u52a0\u8907\u96dc\u7684\u7db2\u9801\u61c9\u7528\u7a0b\u5e8f\u3002\u672c\u6587\u5c07\u4ecb\u7d39Router-View\u7684\u529f\u80fd\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u6280\u5de7\uff0c\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528\u5b83\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[180,179],"tags":[178,177],"class_list":["post-4327","post","type-post","status-publish","format-standard","hentry","category-vue","category-vue-js","tag-vue","tag-vue-js"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-17N","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4327","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=4327"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4327\/revisions"}],"predecessor-version":[{"id":4328,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4327\/revisions\/4328"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=4327"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=4327"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=4327"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}