how2j.cn

步骤 1 : v-for 循环语句   
步骤 2 : index用法   
步骤 3 : 纯数字遍历   

步骤 1 :

v-for 循环语句

首选准备一个数组

var data = [
{name:"盖伦",hp:341},
{name:"提莫",hp:225},
{name:"安妮",hp:427},
{name:"死歌",hp:893}
];

然后在构建Vue的时候把这个数组作为参数传递进去:

data: {
heros:data
}

最后在视图上,通过v-for 遍历这个数组。

<tr v-for="hero in heros">
<td>{{hero.name}}</td>
<td>{{hero.hp}}</td>
</tr>
"); window.frames["iframe_show7798"].document.write(decodeHtml($("textarea#stepcodeTextarea7798").val())); window.frames["iframe_show7798"].document.close(); $(window.frames["iframe_show7798"]).load(function(){ $("#iframe_show7798").height($("#iframe_show7798").contents().find("body").height()+showittryitheight); }); $("#iframe_show7798").height($("#iframe_show7798").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script src="http://127.0.0.1/study/vue.min.js"></script> <style> table tr td{ border:1px solid gray; } table{ border-collapse:collapse; width:300px; } tr.firstLine{ background-color: lightGray; } </style> <div id="div1"> <table align="center" > <tr class="firstLine"> <td>name</td> <td>hp</td> </tr> <tr v-for="hero in heros"> <td>{{hero.name}}</td> <td>{{hero.hp}}</td> </tr> </table> </div> <script> var data = [ {name:"盖伦",hp:341}, {name:"提莫",hp:225}, {name:"安妮",hp:427}, {name:"死歌",hp:893} ]; new Vue({ el: '#div1', data: { heros:data } }) </script>
"); window.frames["iframe7798"].document.write(decodeHtml(code7798)); window.frames["iframe7798"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe7798"]).load(function(){ $("#iframe7798").height($("#iframe7798").contents().find("body").height()+showittryitheight); }); $("#iframe7798").height($("#iframe7798").contents().find("body").height()+showittryitheight); alreadyWriteCode7798 = code7798; $("#rendering7798").hide(); $("#rendered7798").show(); } var tRereshRetry2DemoPanel7798 = setInterval(rereshRetry2DemoPanel7798,1000); var binded7798 = false; $("textarea#stepcodeTextarea7798").keyup(function(){ if(!binded7798){ $(window).bind('beforeunload',function(){ binded7798 = true; return "xxxx"; }); } var newCode = $(this).val() code7798 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code7798!=newCode){ // window.frames["iframe7798"].document.write("
"); // window.frames["iframe7798"].document.write(decodeHtml($("textarea#stepcodeTextarea7798").val())); // window.frames["iframe7798"].document.close(); // $(window.frames["iframe7798"]).load(function(){ // $("#iframe7798").height($("#iframe7798").contents().find("body").height()+showittryitheight); // }); // code7798 = newCode; // } }); $(".tryButton7798").click(function(){ $("#tryDiv7798").show(); $("#stepcodeTextarea7798").focus(); $("#stepcodeTextarea7798").height(200); $("#iframe7798").height(0); window.frames["iframe7798"].document.write("
"); window.frames["iframe7798"].document.write(decodeHtml($("textarea#stepcodeTextarea7798").val())); window.frames["iframe7798"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe7798"]).load(function(){ $("#iframe7798").height($("#iframe7798").contents().find("body").height()+showittryitheight); }); $("#iframe7798").height($("#iframe7798").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor7798.focus(); editor7798.setSize(null, "250"); $("#rendering7798").hide(); $("#rendered7798").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 editor7798 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea7798"), { 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); } } }); editor7798.on("change",function(doc){ if(!binded7798){ $(window).bind('beforeunload',function(){ binded7798 = true; return "xxxx"; }); } var newCode = doc.getValue(); code7798 = newCode; $("textarea#stepcodeTextarea7798").val(newCode); if(alreadyWriteCode7798!=code7798){ lastModifedTime7798 = new Date().getTime(); $("#rendering7798").show(); $("#rendered7798").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor7798 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor7798.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv7798").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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

index用法

通过如下方式可以获取遍历下标

<tr v-for="hero,index in heros">
"); window.frames["iframe_show7799"].document.write(decodeHtml($("textarea#stepcodeTextarea7799").val())); window.frames["iframe_show7799"].document.close(); $(window.frames["iframe_show7799"]).load(function(){ $("#iframe_show7799").height($("#iframe_show7799").contents().find("body").height()+showittryitheight); }); $("#iframe_show7799").height($("#iframe_show7799").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script src="http://127.0.0.1/study/vue.min.js"></script> <style> table tr td{ border:1px solid gray; } table{ border-collapse:collapse; width:300px; } tr.firstLine{ background-color: lightGray; } </style> <div id="div1"> <table align="center" > <tr class="firstLine"> <td>number</td> <td>name</td> <td>hp</td> </tr> <tr v-for="hero,index in heros"> <td>{{index+1}}</td> <td>{{hero.name}}</td> <td>{{hero.hp}}</td> </tr> </table> </div> <script> var data = [ {name:"盖伦",hp:341}, {name:"提莫",hp:225}, {name:"安妮",hp:427}, {name:"死歌",hp:893} ]; new Vue({ el: '#div1', data: { heros:data } }) </script>
"); window.frames["iframe7799"].document.write(decodeHtml(code7799)); window.frames["iframe7799"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe7799"]).load(function(){ $("#iframe7799").height($("#iframe7799").contents().find("body").height()+showittryitheight); }); $("#iframe7799").height($("#iframe7799").contents().find("body").height()+showittryitheight); alreadyWriteCode7799 = code7799; $("#rendering7799").hide(); $("#rendered7799").show(); } var tRereshRetry2DemoPanel7799 = setInterval(rereshRetry2DemoPanel7799,1000); var binded7799 = false; $("textarea#stepcodeTextarea7799").keyup(function(){ if(!binded7799){ $(window).bind('beforeunload',function(){ binded7799 = true; return "xxxx"; }); } var newCode = $(this).val() code7799 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code7799!=newCode){ // window.frames["iframe7799"].document.write("
"); // window.frames["iframe7799"].document.write(decodeHtml($("textarea#stepcodeTextarea7799").val())); // window.frames["iframe7799"].document.close(); // $(window.frames["iframe7799"]).load(function(){ // $("#iframe7799").height($("#iframe7799").contents().find("body").height()+showittryitheight); // }); // code7799 = newCode; // } }); $(".tryButton7799").click(function(){ $("#tryDiv7799").show(); $("#stepcodeTextarea7799").focus(); $("#stepcodeTextarea7799").height(200); $("#iframe7799").height(0); window.frames["iframe7799"].document.write("
"); window.frames["iframe7799"].document.write(decodeHtml($("textarea#stepcodeTextarea7799").val())); window.frames["iframe7799"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe7799"]).load(function(){ $("#iframe7799").height($("#iframe7799").contents().find("body").height()+showittryitheight); }); $("#iframe7799").height($("#iframe7799").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor7799.focus(); editor7799.setSize(null, "250"); $("#rendering7799").hide(); $("#rendered7799").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 editor7799 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea7799"), { 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); } } }); editor7799.on("change",function(doc){ if(!binded7799){ $(window).bind('beforeunload',function(){ binded7799 = true; return "xxxx"; }); } var newCode = doc.getValue(); code7799 = newCode; $("textarea#stepcodeTextarea7799").val(newCode); if(alreadyWriteCode7799!=code7799){ lastModifedTime7799 = new Date().getTime(); $("#rendering7799").show(); $("#rendered7799").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor7799 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor7799.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv7799").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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

纯数字遍历

通过如下方式可以进行纯数字遍历:

<div v-for="i in 10">
"); window.frames["iframe_show7800"].document.write(decodeHtml($("textarea#stepcodeTextarea7800").val())); window.frames["iframe_show7800"].document.close(); $(window.frames["iframe_show7800"]).load(function(){ $("#iframe_show7800").height($("#iframe_show7800").contents().find("body").height()+showittryitheight); }); $("#iframe_show7800").height($("#iframe_show7800").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script src="http://127.0.0.1/study/vue.min.js"></script> <div id="div1"> <div v-for="i in 10"> {{ i }} </div> </div> <script> new Vue({ el: '#div1' }) </script>
<script src="http://127.0.0.1/study/vue.min.js"></script>
  
<div id="div1">
    <div v-for="i in 10">
     {{ i }}
    </div>
</div>
   
<script>
 
new Vue({
      el: '#div1'
    })
   
</script>
"); window.frames["iframe7800"].document.write(decodeHtml(code7800)); window.frames["iframe7800"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe7800"]).load(function(){ $("#iframe7800").height($("#iframe7800").contents().find("body").height()+showittryitheight); }); $("#iframe7800").height($("#iframe7800").contents().find("body").height()+showittryitheight); alreadyWriteCode7800 = code7800; $("#rendering7800").hide(); $("#rendered7800").show(); } var tRereshRetry2DemoPanel7800 = setInterval(rereshRetry2DemoPanel7800,1000); var binded7800 = false; $("textarea#stepcodeTextarea7800").keyup(function(){ if(!binded7800){ $(window).bind('beforeunload',function(){ binded7800 = true; return "xxxx"; }); } var newCode = $(this).val() code7800 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code7800!=newCode){ // window.frames["iframe7800"].document.write("
"); // window.frames["iframe7800"].document.write(decodeHtml($("textarea#stepcodeTextarea7800").val())); // window.frames["iframe7800"].document.close(); // $(window.frames["iframe7800"]).load(function(){ // $("#iframe7800").height($("#iframe7800").contents().find("body").height()+showittryitheight); // }); // code7800 = newCode; // } }); $(".tryButton7800").click(function(){ $("#tryDiv7800").show(); $("#stepcodeTextarea7800").focus(); $("#stepcodeTextarea7800").height(200); $("#iframe7800").height(0); window.frames["iframe7800"].document.write("
"); window.frames["iframe7800"].document.write(decodeHtml($("textarea#stepcodeTextarea7800").val())); window.frames["iframe7800"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe7800"]).load(function(){ $("#iframe7800").height($("#iframe7800").contents().find("body").height()+showittryitheight); }); $("#iframe7800").height($("#iframe7800").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor7800.focus(); editor7800.setSize(null, "250"); $("#rendering7800").hide(); $("#rendered7800").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 editor7800 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea7800"), { 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); } } }); editor7800.on("change",function(doc){ if(!binded7800){ $(window).bind('beforeunload',function(){ binded7800 = true; return "xxxx"; }); } var newCode = doc.getValue(); code7800 = newCode; $("textarea#stepcodeTextarea7800").val(newCode); if(alreadyWriteCode7800!=code7800){ lastModifedTime7800 = new Date().getTime(); $("#rendering7800").show(); $("#rendered7800").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor7800 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor7800.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv7800").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

上传截图