{"id":985,"date":"2023-03-23T22:41:43","date_gmt":"2023-03-23T14:41:43","guid":{"rendered":""},"modified":"2023-03-23T22:41:43","modified_gmt":"2023-03-23T14:41:43","slug":"Vue.js \u8def\u7531","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/985.html","title":{"rendered":"Vue.js \u8def\u7531"},"content":{"rendered":"
\n
<script src = \"\/path\/to\/vue.js\"><\/script><\/span>
<script src = \"\/path\/to\/vue-router.js\"><\/script><\/span>
<\/span><\/code><\/pre>\n<\/p><\/div>\n\n \u5982\u679c\u8981\u4e0d\u4e0b\u8f7d\u5c31\u4f7f\u7528\u5b83\uff0c\u53ef\u4ee5\u6309\u4ee5\u4e0b\u65b9\u5f0f\u4f7f\u7528\u5b83:\n <\/div>\n\n <script src=\"https:\/\/unpkg.com\/vue-router\/dist\/vue-router.js\"><\/script><\/span>
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u4f7f\u7528NPM\u5b89\u88c5\u8def\u7531\u5668<\/h3>\n\n \u6253\u5f00Node.js\u547d\u4ee4\u63d0\u793a\u7b26\u5e76\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5vue-router\u3002\n <\/div>\n\n npm install vue-router
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n
<\/p>\n
\u4f7f\u7528GitHub\u5b89\u88c5\u8def\u7531\u5668<\/h3>\n\n \u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u4eceGitHub\u514b\u9686\u8def\u7531\u5668\u5b58\u50a8\u5e93:\n <\/div>\n\n git clone https:\/\/github.com\/vuejs\/vue-router.git node_modules\/vue-router
cd node_modules\/vue-router
npm install
npm run build
<\/span><\/code><\/pre>\n<\/p><\/div>\n\n \u8ba9\u6211\u4eec\u4ee5\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u6765\u4e86\u89e3Vue.js\u4e2d\u8def\u7531\u7684\u6982\u5ff5:\n <\/div>\n\u793a\u4f8b<\/h3>\n\n Index.html\u6587\u4ef6: <\/strong>\n <\/div>\n\n <html><\/span>
<head><\/span>
<title><\/span>Vue.js Routing<\/title><\/span>
<link rel=\"stylesheet\" href=\"index.css\">
<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/vue\/dist\/vue.js\"><\/script><\/span>
<script src=\"https:\/\/unpkg.com\/vue\/dist\/vue.js\"><\/script><\/span>
<script src=\"https:\/\/unpkg.com\/vue-router\/dist\/vue-router.js\"><\/script><\/span>
<\/head><\/span>
<body><\/span>
<div id = \"router_1\">
<h2><\/span>This is Routing Example<\/h2><\/span>
<p><\/span>
<!-- use router-link component for navigation. -->
<!-- `<router-link>` will be rendered as an `<a><\/span>` tag by default -->
<router-link to = \"\/route1\">Click on Router Link 1<\/router-link>
<router-link to = \"\/route2\"> Click on Router Link 2<\/router-link>
<\/p><\/span>
<!-- route outlet -->
<!-- component matched by the route will render here -->
<router-view><\/router-view>
<\/div><\/span>
<script type = \"text\/javascript\">
const Route1 = { template: '<div style = \"border-radius:30px;background-color:lightpink;width:300px;height:100px;margin:10px;font-size:25px;padding:10px;\">You are seeing router link 1<\/div><\/span>' }
const Route2 = { template: '<div style = \"border-radius:20px;background-color:lightgreen;width:300px;height:100px;margin:10px;font-size:25px;padding:10px;\"> You are seeing router link 2<\/div><\/span>' }
const routes = [
{ path: '\/route1', component: Route1 },
{ path: '\/route2', component: Route2 }
];
const router = new VueRouter({
routes \/\/ short for `routes: routes`
});
<\/script><\/span>
<script src=\"index.js\"><\/script><\/span>
<\/body><\/span>
<\/html><\/span>
<\/span><\/code><\/pre>\n<\/p><\/div>\n\n Index.js\u6587\u4ef6: <\/strong>\n <\/div>\n\n var vm = new Vue({
el: '#router_1',
router
})
<\/span><\/code><\/pre>\n<\/p><\/div>\n\n \u8ba9\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u7b80\u5355\u7684CSS\u6587\u4ef6\u6765\u4f7f\u8f93\u51fa\u66f4\u5177\u5438\u5f15\u529b\u3002\n <\/div>\n\n Index.css\u6587\u4ef6: <\/strong>\n <\/div>\n\n html, body {
margin: 5px;
padding: 0;
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n\n \u7a0b\u5e8f\u6267\u884c\u540e\uff0c\u60a8\u5c06\u770b\u5230\u4ee5\u4e0b\u8f93\u51fa:\n <\/div>\n\n \u8f93\u51fa: <\/strong>\n <\/div>\n
<\/p>\n
\n \u5355\u51fb\u8def\u7531\u5668\u94fe\u63a52\uff0c\u60a8\u5c06\u770b\u5230\u5b83\u88ab\u91cd\u5b9a\u5411\u5230\u4ee5\u4e0b\u7ed3\u679c\u3002\n <\/div>\n
<\/p>\n
\u793a\u4f8b\u8bf4\u660e<\/h3>\n\n \u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u60a8\u53ef\u4ee5\u770b\u5230\u6211\u4eec\u5982\u4e0b\u6dfb\u52a0\u4e86vue-router.js\u6587\u4ef6:\n <\/div>\n\n <script src=\"https:\/\/unpkg.com\/vue\/dist\/vue.js\"><\/script><\/span>
<script src=\"https:\/\/unpkg.com\/vue-router\/dist\/vue-router.js\"><\/script><\/span>4
<\/span><\/code><\/pre>\n<\/p><\/div>\n\n \u5728\u4e0b\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e24\u4e2a\u8def\u7531\u5668\u94fe\u63a5\u3002\u6211\u4eec\u5728\u4e3b\u4f53\u90e8\u5206\u4e2d\u5b9a\u4e49\u4e86\u4ee5\u4e0b\u8def\u7531\u5668\u94fe\u63a5:\n <\/div>\n\n <router-link to = \"\/route1\">Click on Router Link 1<\/router-link>
<router-link to = \"\/route2\"> Click on Router Link 2<\/router-link>
<\/span><\/code><\/pre>\n<\/p><\/div>\n\n \u5728\u8fd9\u91cc\uff0c
\n
\n
\n <\/router-link><\/strong>\u662f\u7528\u4e8e\u5bfc\u822a\u5230HTML\u5185\u5bb9\u5e76\u5c06\u5176\u663e\u793a\u7ed9\u7528\u6237\u7684\u7ec4\u4ef6\u3002
\n \u81f3<\/strong>\u5c5e\u6027\u6307\u5b9a\u76ee\u7684\u5730\u3002\u4f8b\u5982\uff0c\u60a8\u5fc5\u987b\u5355\u51fb\u663e\u793a\u94fe\u63a5\u7684\u6587\u4ef6\u3002\n <\/div>\n\n \u8def\u7531\u5668\u5728\u811a\u672c\u90e8\u5206\u4e2d\u521d\u59cb\u5316\uff0c\u5176\u4e2d\u521b\u5efa\u4e86\u4e24\u4e2a\u5e38\u91cf\uff0c\u5982\u4e0b\u6240\u793a:\n <\/div>\n\n const Route1 = { template: '<div style = \"border-radius:30px;background-color:lightpink;width:300px;height:100px;margin:10px;font-size:25px;padding:10px;\">You are seeing router link 1<\/div><\/span>' }
const Route2 = { template: '<div style = \"border-radius:20px;background-color:lightgreen;width:300px;height:100px;margin:10px;font-size:25px;padding:10px;\"> You are seeing router link 2<\/div><\/span>' }
<\/span><\/code><\/pre>\n<\/p><\/div>\n\n \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u6307\u5b9a\u4e86\u5355\u51fb\u8def\u7531\u5668\u94fe\u63a5\u65f6\u5c06\u663e\u793a\u7684\u6a21\u677f\u3002\n <\/div>\n\n \u63a5\u4e0b\u6765\u662f\u8def\u7ebfconst\uff0c\u5176\u4e2d\u6211\u4eec\u5b9a\u4e49\u4e86\u5fc5\u987b\u5728\u5176\u4e2d\u663e\u793a\u7684\u8def\u5f84\u7f51\u5740\u3002\n <\/div>\n\n const routes = [
{ path: '\/route1', component: Route1 },
{ path: '\/route2', component: Route2 }
];
<\/span><\/code><\/pre>\n<\/p><\/div>\n\n \u8def\u7531\u7528\u4e8e\u5b9a\u4e49\u8def\u5f84\u548c\u7ec4\u4ef6\u3002\u7528\u6237\u5355\u51fb\u8def\u7531\u5668\u94fe\u63a5\u65f6\uff0c\u8def\u5f84\u5c06\u663e\u793a\u5728\u8f93\u51fa\u4e2d\u3002\n <\/div>\n\n \u8be5\u7ec4\u4ef6\u91c7\u7528\u8981\u663e\u793a\u7684\u6a21\u677f\u7684\u540d\u79f0\u3002\u8def\u7531\u7684\u8def\u5f84\u9700\u8981\u4e0e\u5230\u5c5e\u6027\u7684\u8def\u7531\u5668\u94fe\u63a5\u76f8\u5339\u914d\u3002\n <\/div>\n\n \u4f8b\u5982\uff0c\n <\/div>\n\n <router-link to = \"name_of_-the_path\"><\/router-link>
<\/span><\/code><\/pre>\n<\/p><\/div>\n\n \u73b0\u5728\uff0c\u6211\u4eec\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u521b\u5efa\u4e86VueRouter\u5b9e\u4f8b:\n <\/div>\n\n const router = new VueRouter({
routes \/\/ short for `routes: routes`
});
<\/span><\/code><\/pre>\n<\/p><\/div>\n\n VueRouter\u6784\u9020\u51fd\u6570\u5c06\u8def\u7531\u4f5c\u4e3a\u53c2\u6570\u3002\u6211\u4eec\u5df2\u7ecf\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5c06\u8def\u7531\u5668\u5bf9\u8c61\u5206\u914d\u7ed9\u4e3bVue\u5b9e\u4f8b\u3002\n <\/div>\n\n var vm = new Vue({
el: '#router_1',
router
})
<\/span><\/code><\/pre>\n<\/p><\/div>\n\n \u6267\u884c\u4e0a\u9762\u7684\u793a\u4f8b\u540e\uff0c\u60a8\u5c06\u5728\u6d4f\u89c8\u5668\u4e2d\u770b\u5230\u8f93\u51fa\u3002\u5982\u679c\u60a8\u53ef\u4ee5\u68c0\u67e5\u548c\u68c0\u67e5\u8def\u7531\u5668\u94fe\u63a5\uff0c\u6211\u4eec\u5c06\u53d1\u73b0\u5b83\u4e3a\u6d3b\u52a8\u5143\u7d20\u6dfb\u52a0\u4e86\u7c7b\u3002\u6dfb\u52a0\u7684\u7c7b\u4e3aclass =\"router-link-exact-active router-link-active\"\u3002\n <\/div>\n\u5c06\u5c5e\u6027\u4f20\u9012\u5230\u8def\u7531\u5668\u94fe\u63a5<\/h3>\n\n \u8ba9\u6211\u4eec\u770b\u770b\u5982\u4f55\u4f20\u9012\u8def\u7531\u5668\u94fe\u63a5\u7684\u5c5e\u6027\u3002\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5df2\u5c06\u5c5e\u6027\" to\"\u4f20\u9012\u7ed9\u8def\u7531\u5668\u94fe\u63a5\uff0c\u5982\u4e0b\u6240\u793a:\n <\/div>\n\n <router-link to = \"\/route1\">Click on Router Link 1<\/router-link>
<router-link to = \"\/route2\"> Click on Router Link 2<\/router-link>
<\/span><\/code><\/pre>\n<\/p><\/div>\n\n \u6b64\u5c5e\u6027\u53ef\u4ee5\u901a\u8fc7\u8bb8\u591a\u5176\u4ed6\u65b9\u5f0f\u4f20\u9012\u5230\u8def\u7531\u5668\u94fe\u63a5\u3002\n <\/div>\nto\u5c5e\u6027<\/h3>\n\n \u5b83\u7528\u4e8e\u6307\u5b9a\u6307\u5b9a\u7ed9<router-link>\u3002\u5f53\u60a8\u5355\u51fb\u94fe\u63a5\u65f6\uff0c\u5b83\u5c06\u5728\u5185\u90e8\u5c06\u503c\u4f20\u9012\u7ed9
\n router.push()<\/strong>\u3002\u8be5\u503c\u5fc5\u987b\u662f\u5b57\u7b26\u4e32\u6216\u4f4d\u7f6e\u5bf9\u8c61\u3002\u4f7f\u7528\"
\n \u81f3\" <\/strong>\u5c5e\u6027\u53ef\u4ee5\u901a\u8fc7\u4e09\u79cd\u65b9\u5f0f\u4f20\u9012\u503c\u3002\n <\/div>\n\n \u793a\u4f8b1: <\/strong>\n <\/div>\n\n <router-link to = \"\/route1\">Click on Router Link 1<\/router-link>
It is rendered as:
<a href = \"#\/route\">Router Link <\/a><\/span>
<\/span><\/code><\/pre>\n<\/p><\/div>\n\n \u793a\u4f8b2: <\/strong>\n <\/div>\n\n <router-link v-bind:to = \"{path:'\/route1'}\">Click on Router Link 2<\/router-link> \/\/ When using an object, it is recommended to bind it as shown in e.g. 2.
<\/span><\/code><\/pre>\n<\/p><\/div>\n\n \u793a\u4f8b3: <\/strong>\n <\/div>\n\n <router-link v-bind:to = \"{path:'\/route1', query: { name: 'Alex' }}\">Click on Router Link 3<\/router-link>\/\/You can pass a query string as shown in e.g. 3. This is an example of router link with query string.
<\/span><\/code><\/pre>\n<\/p><\/div>\n\n \u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u793a\u4f8b1\u7684\u65b9\u6cd5\u3002\u5728\u4e0b\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u8ba9\u6211\u4eec\u770b\u7b2c\u4e8c\u79cd\u65b9\u6cd5\u548c\u7b2c\u4e09\u79cd\u65b9\u6cd5\u3002\n <\/div>\n\n Index.html\u6587\u4ef6: <\/strong>\n <\/div>\n\n <html><\/span>
<head><\/span>
<title><\/span>Vue.js Routing<\/title><\/span>
<link rel=\"stylesheet\" href=\"index.css\">
<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/vue\/dist\/vue.js\"><\/script><\/span>
<script src=\"https:\/\/unpkg.com\/vue\/dist\/vue.js\"><\/script><\/span>
<script src=\"https:\/\/unpkg.com\/vue-router\/dist\/vue-router.js\"><\/script><\/span>
<\/head><\/span>
<body><\/span>
<div id = \"router_1\">
<h2><\/span>This is Routing Example<\/h2><\/span>
<p><\/span>
<!-- use router-link component for navigation. -->
<!-- `<router-link>` will be rendered as an `<a><\/span>` tag by default -->
<router-link v-bind:to = \"{path:'\/route2'}\">Click on Router Link 2<\/router-link>
<router-link v-bind:to = \"{path:'\/route3', query: { name: 'Alex' }}\">Click on Router Link 3<\/router-link>
<\/p><\/span>
<!-- route outlet -->
<!-- component matched by the route will render here -->
<router-view><\/router-view>
<\/div><\/span>
<script type = \"text\/javascript\">
const Route2 = { template: '<div style = \"border-radius:30px;background-color:lightpink;width:300px;height:100px;margin:10px;font-size:25px;padding:10px;\">You are seeing router link 2<\/div><\/span>' }
const Route3 = { template: '<div style = \"border-radius:20px;background-color:lightgreen;width:300px;height:100px;margin:10px;font-size:25px;padding:10px;\"> You are seeing router link 3<\/div><\/span>' }
const routes = [
{ path: '\/route2', component: Route2 },
{ path: '\/route3', component: Route3 }
];
const router = new VueRouter({
routes \/\/ short for `routes: routes`
});
<\/script><\/span>
<script src=\"index.js\"><\/script><\/span>
<\/body><\/span>
<\/html><\/span>
<\/span><\/code><\/pre>\n<\/p><\/div>\n\n Index.js\u6587\u4ef6: <\/strong>\n <\/div>\n\n var vm = new Vue({
el: '#router_1',
router
})
<\/span><\/code><\/pre>\n<\/p><\/div>\n\n \u7a0b\u5e8f\u6267\u884c\u540e\uff0c\u60a8\u5c06\u770b\u5230\u4ee5\u4e0b\u8f93\u51fa:\n <\/div>\n\n \u8f93\u51fa: <\/strong>\n <\/div>\n
<\/p>\n
\n \u73b0\u5728\uff0c\u5355\u51fb\u8def\u7531\u5668\u94fe\u63a53\uff0c\u60a8\u5c06\u5728URL\u4e2d\u770b\u5230\u8bbe\u7f6e\u540d\u79f0\u4f5c\u4e3a\u67e5\u8be2\u5b57\u7b26\u4e32\u3002\u53c2\u89c1\u8f93\u51fa:\n <\/div>\n
<\/p>\n
replace\u5c5e\u6027<\/h3>\n\n replace\u5c5e\u6027\u7528\u4e8e\u66ff\u6362\u8def\u7531\u5668\u94fe\u63a5\u5e76\u8c03\u7528router.replace()\u800c\u4e0d\u662frouter.push()\u3002\u5982\u679c\u60a8\u4f7f\u7528replace prop\uff0c\u5219\u4e0d\u4f1a\u5b58\u50a8\u5bfc\u822a\u5386\u53f2\u8bb0\u5f55\u3002\n <\/div>\n\n Index.html\u6587\u4ef6: <\/strong>\n <\/div>\n\n <html><\/span>
<head><\/span>
<title><\/span>Vue.js Routing<\/title><\/span>
<link rel=\"stylesheet\" href=\"index.css\">
<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/vue\/dist\/vue.js\"><\/script><\/span>
<script src=\"https:\/\/unpkg.com\/vue\/dist\/vue.js\"><\/script><\/span>
<script src=\"https:\/\/unpkg.com\/vue-router\/dist\/vue-router.js\"><\/script><\/span>
<\/head><\/span>
<body><\/span>
<div id = \"router_1\">
<h2><\/span>This is Routing Example<\/h2><\/span>
<p><\/span>
<!-- use router-link component for navigation. -->
<!-- `<router-link>` will be rendered as an `<a><\/span>` tag by default -->
<router-link v-bind:to = \"{path:'\/route2'}\">Click on Router Link 2<\/router-link>
<router-link v-bind:to = \"{path:'\/route3', query: { name: 'Panda' }}\" replace>This link is Replaced<\/router-link>
<\/p><\/span>
<!-- route outlet -->
<!-- component matched by the route will render here -->
<router-view><\/router-view>
<\/div><\/span>
<script type = \"text\/javascript\">
const Route2 = { template: '<div style = \"border-radius:30px;background-color:lightpink;width:300px;height:100px;margin:10px;font-size:25px;padding:10px;\">You are seeing router link 2<\/div><\/span>' }
const Route3 = { template: '<div style = \"border-radius:20px;background-color:lightgreen;width:300px;height:100px;margin:10px;font-size:25px;padding:10px;\"> You are seeing router link 3<\/div><\/span>' }
const routes = [
{ path: '\/route2', component: Route2 },
{ path: '\/route3', component: Route3 }
];
const router = new VueRouter({
routes \/\/ short for `routes: routes`
});
<\/script><\/span>
<script src=\"index.js\"><\/script><\/span>
<\/body><\/span>
<\/html><\/span>
<\/span><\/code><\/pre>\n<\/p><\/div>\n\n Index.js\u6587\u4ef6<\/strong>\u5c06\u662f\u76f8\u540c\u7684\u3002\n <\/div>\n\n \u7a0b\u5e8f\u6267\u884c\u540e\uff0c\u60a8\u5c06\u770b\u5230\u4ee5\u4e0b\u8f93\u51fa:\n <\/div>\n\n \u8f93\u51fa: <\/strong>\n <\/div>\n
<\/p>\n
\n \u5355\u51fb\u5373\u53ef\u770b\u5230\n <\/div>\nappend\u5c5e\u6027<\/h3>\n\n \u9644\u52a0\u6807\u7b7e\u7528\u4e8e\u4e0e
\n
\n \u6dfb\u52a0\u5230\u4f7f\u8def\u5f84\u76f8\u5bf9\u3002\u8ba9\u6211\u4eec\u4e3e\u4e2a\u4f8b\u5b50\u3002
\n <\/router-link>\n <\/div>\n\n \u793a\u4f8b: <\/strong>\n <\/div>\n\n Index.html\u6587\u4ef6: <\/strong>\n <\/div>\n\n <html><\/span>
<head><\/span>
<title><\/span>Vue.js Routing<\/title><\/span>
<link rel=\"stylesheet\" href=\"index.css\">
<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/vue\/dist\/vue.js\"><\/script><\/span>
<script src=\"https:\/\/unpkg.com\/vue\/dist\/vue.js\"><\/script><\/span>
<script src=\"https:\/\/unpkg.com\/vue-router\/dist\/vue-router.js\"><\/script><\/span>
<\/head><\/span>
<body><\/span>
<div id = \"router_1\">
<h2><\/span>This is Routing Example<\/h2><\/span>
<p><\/span>
<!-- use router-link component for navigation. -->
<!-- `<router-link>` will be rendered as an `<a><\/span>` tag by default -->
<router-link v-bind:to = \"{path:'\/route2'}\">Click on Router Link 2<\/router-link>
<router-link v-bind:to = \"{ path: '\/route3'}\" append>This is an appended link<\/router-link>
<\/p><\/span>
<!-- route outlet -->
<!-- component matched by the route will render here -->
<router-view><\/router-view>
<\/div><\/span>
<script type = \"text\/javascript\">
const Route2 = { template: '<div style = \"border-radius:30px;background-color:lightpink;width:300px;height:100px;margin:10px;font-size:25px;padding:10px;\">You are seeing router link 2<\/div><\/span>' }