how2j.cn

相关下载
文件名 文件大小
vue-router.min.js 23k
步骤 1 : vue.js 里的路由概念   
步骤 2 : vue-router.min.js   
步骤 3 : 路由代码讲解   

步骤 1 :

vue.js 里的路由概念

vue.js 里的路由相当于就是局部刷新。
如效果所示,点击左边的菜单,右边的内容在不刷新整个页面的情况下,进行局部刷新
"); window.frames["iframe_show7829"].document.write(decodeHtml($("textarea#stepcodeTextarea7829").val())); window.frames["iframe_show7829"].document.close(); $(window.frames["iframe_show7829"]).load(function(){ $("#iframe_show7829").height($("#iframe_show7829").contents().find("body").height()+showittryitheight); }); $("#iframe_show7829").height($("#iframe_show7829").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
<script src="http://127.0.0.1/study/vue.min.js"></script> <script src="http://127.0.0.1/study/vue-router.min.js"></script> <head> <style> a{ text-decoration: none; } a.router-link-active{ /* color:blue; */ background-color: lightGray; } div.menu{ border:1px solid gray; float:left; } div.menu a{ display:block; } div.workingRoom{ margin-left:100px; } div#app{ width:500px; padding:10px; margin:10px auto; } </style> </head> <div id="app"> <div class="menu"> <!-- router-link 相当于就是超链 to 相当于就是 href --> <router-link to="/user">用户管理</router-link> <router-link to="/product">产品管理</router-link> <router-link to="/order">订单管理</router-link> </div> <div class="workingRoom"> <!-- 点击上面的/user,那么/user 对应的内容就会显示在router-view 这里 --> <router-view></router-view> </div> </div> <script> /* * 申明三个模板( html 片段 ) */ var user = { template: '<p>用户管理页面的内容</p>' }; var second = { template: '<p>产品管理页面的内容</p>' }; var order = { template: '<p>订单管理页面的内容</p>' }; /* * 定义路由 */ var routes = [ { path: '/', redirect: '/user'}, // 这个表示会默认渲染 /user,没有这个就是空白 { path: '/user', component: user }, { path: '/product', component: second }, { path: '/order', component: order } ]; /* * 创建VueRouter实例 */ var router = new VueRouter({ routes:routes }); /* * 给vue对象绑定路由 */ new Vue({ el:"#app", router }) </script>
"); window.frames["iframe7829"].document.write(decodeHtml(code7829)); window.frames["iframe7829"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe7829"]).load(function(){ $("#iframe7829").height($("#iframe7829").contents().find("body").height()+showittryitheight); }); $("#iframe7829").height($("#iframe7829").contents().find("body").height()+showittryitheight); alreadyWriteCode7829 = code7829; $("#rendering7829").hide(); $("#rendered7829").show(); } var tRereshRetry2DemoPanel7829 = setInterval(rereshRetry2DemoPanel7829,1000); var binded7829 = false; $("textarea#stepcodeTextarea7829").keyup(function(){ if(!binded7829){ $(window).bind('beforeunload',function(){ binded7829 = true; return "xxxx"; }); } var newCode = $(this).val() code7829 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code7829!=newCode){ // window.frames["iframe7829"].document.write("
"); // window.frames["iframe7829"].document.write(decodeHtml($("textarea#stepcodeTextarea7829").val())); // window.frames["iframe7829"].document.close(); // $(window.frames["iframe7829"]).load(function(){ // $("#iframe7829").height($("#iframe7829").contents().find("body").height()+showittryitheight); // }); // code7829 = newCode; // } }); $(".tryButton7829").click(function(){ $("#tryDiv7829").show(); $("#stepcodeTextarea7829").focus(); $("#stepcodeTextarea7829").height(200); $("#iframe7829").height(0); window.frames["iframe7829"].document.write("
"); window.frames["iframe7829"].document.write(decodeHtml($("textarea#stepcodeTextarea7829").val())); window.frames["iframe7829"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe7829"]).load(function(){ $("#iframe7829").height($("#iframe7829").contents().find("body").height()+showittryitheight); }); $("#iframe7829").height($("#iframe7829").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor7829.focus(); editor7829.setSize(null, "250"); $("#rendering7829").hide(); $("#rendered7829").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor7829 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea7829"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor7829.on("change",function(doc){ if(!binded7829){ $(window).bind('beforeunload',function(){ binded7829 = true; return "xxxx"; }); } var newCode = doc.getValue(); code7829 = newCode; $("textarea#stepcodeTextarea7829").val(newCode); if(alreadyWriteCode7829!=code7829){ lastModifedTime7829 = new Date().getTime(); $("#rendering7829").show(); $("#rendered7829").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor7829 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor7829.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv7829").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果
步骤 2 :

vue-router.min.js

为了实现路由,需要一个额外的库: vue-router.min.js

<script src="http://127.0.0.1/study/vue-router.min.js"></script>

如果要在本地测试,可以从右上角下载
步骤 3 :

路由代码讲解

代码的讲解都写在注释里了,挨个看过去就好了
"); window.frames["iframe_show7830"].document.write(decodeHtml($("textarea#stepcodeTextarea7830").val())); window.frames["iframe_show7830"].document.close(); $(window.frames["iframe_show7830"]).load(function(){ $("#iframe_show7830").height($("#iframe_show7830").contents().find("body").height()+showittryitheight); }); $("#iframe_show7830").height($("#iframe_show7830").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script src="http://127.0.0.1/study/vue.min.js"></script> <script src="http://127.0.0.1/study/vue-router.min.js"></script> <head> <style> a{ text-decoration: none; } a.router-link-active{ /* color:blue; */ background-color: lightGray; } div.menu{ border:1px solid gray; float:left; } div.menu a{ display:block; } div.workingRoom{ margin-left:100px; } div#app{ width:500px; padding:10px; margin:10px auto; } </style> </head> <div id="app"> <div class="menu"> <!-- router-link 相当于就是超链 to 相当于就是 href --> <router-link to="/user">用户管理</router-link> <router-link to="/product">产品管理</router-link> <router-link to="/order">订单管理</router-link> </div> <div class="workingRoom"> <!-- 点击上面的/user,那么/user 对应的内容就会显示在router-view 这里 --> <router-view></router-view> </div> </div> <script> /* * 申明三个模板( html 片段 ) */ var user = { template: '<p>用户管理页面的内容</p>' }; var second = { template: '<p>产品管理页面的内容</p>' }; var order = { template: '<p>订单管理页面的内容</p>' }; /* * 定义路由 */ var routes = [ { path: '/', redirect: '/user'}, // 这个表示会默认渲染 /user,没有这个就是空白 { path: '/user', component: user }, { path: '/product', component: second }, { path: '/order', component: order } ]; /* * 创建VueRouter实例 */ var router = new VueRouter({ routes:routes }); /* * 给vue对象绑定路由 */ new Vue({ el:"#app", router }) </script>
"); window.frames["iframe7830"].document.write(decodeHtml(code7830)); window.frames["iframe7830"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe7830"]).load(function(){ $("#iframe7830").height($("#iframe7830").contents().find("body").height()+showittryitheight); }); $("#iframe7830").height($("#iframe7830").contents().find("body").height()+showittryitheight); alreadyWriteCode7830 = code7830; $("#rendering7830").hide(); $("#rendered7830").show(); } var tRereshRetry2DemoPanel7830 = setInterval(rereshRetry2DemoPanel7830,1000); var binded7830 = false; $("textarea#stepcodeTextarea7830").keyup(function(){ if(!binded7830){ $(window).bind('beforeunload',function(){ binded7830 = true; return "xxxx"; }); } var newCode = $(this).val() code7830 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code7830!=newCode){ // window.frames["iframe7830"].document.write("
"); // window.frames["iframe7830"].document.write(decodeHtml($("textarea#stepcodeTextarea7830").val())); // window.frames["iframe7830"].document.close(); // $(window.frames["iframe7830"]).load(function(){ // $("#iframe7830").height($("#iframe7830").contents().find("body").height()+showittryitheight); // }); // code7830 = newCode; // } }); $(".tryButton7830").click(function(){ $("#tryDiv7830").show(); $("#stepcodeTextarea7830").focus(); $("#stepcodeTextarea7830").height(200); $("#iframe7830").height(0); window.frames["iframe7830"].document.write("
"); window.frames["iframe7830"].document.write(decodeHtml($("textarea#stepcodeTextarea7830").val())); window.frames["iframe7830"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe7830"]).load(function(){ $("#iframe7830").height($("#iframe7830").contents().find("body").height()+showittryitheight); }); $("#iframe7830").height($("#iframe7830").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor7830.focus(); editor7830.setSize(null, "250"); $("#rendering7830").hide(); $("#rendered7830").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor7830 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea7830"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor7830.on("change",function(doc){ if(!binded7830){ $(window).bind('beforeunload',function(){ binded7830 = true; return "xxxx"; }); } var newCode = doc.getValue(); code7830 = newCode; $("textarea#stepcodeTextarea7830").val(newCode); if(alreadyWriteCode7830!=code7830){ lastModifedTime7830 = new Date().getTime(); $("#rendering7830").show(); $("#rendered7830").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor7830 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor7830.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv7830").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果


HOW2J公众号,关注后实时获知布最新的教程和优惠活动,谢谢。


关于 前端部分-Vue.js-路由 的提问

尽量提供截图代码异常信息,有助于分析和解决问题。 也可进本站QQ群交流: 620943819
提问尽量提供完整的代码,环境描述,越是有利于问题的重现,您的问题越能更快得到解答。
对教程中代码有疑问,请提供是哪个步骤,哪一行有疑问,这样便于快速定位问题,提高问题得到解答的速度
在已经存在的几千个提问里,有相当大的比例,是因为使用了和站长不同版本的开发环境导致的,比如 jdk, eclpise, idea, mysql,tomcat 等等软件的版本不一致。
请使用和站长一样的版本,可以节约自己大量的学习时间。 站长把教学中用的软件版本整理了,都统一放在了这里, 方便大家下载: http://how2j.cn/k/helloworld/helloworld-version/1718.html

上传截图