ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
#### v-for指令的使用方法 <div id="hdcms"> <table border="1"> <tr> <th>序号</th> <th>编号</th> <th>标题</th> <th>老师</th> </tr> <tbody> <tr v-for="(field,key) of news"> <td>{{key+1}}</td> <td>{{field.id}}</td> <td>{{field.title}}</td> <td>{{name}}</td> </tr> </tbody> </table> </div> <script> var app = new Vue({ el: '#hdcms', data: { name:'向军老师', news:[ {id:22,title:'houdunren.com'}, {id:77,title:'后盾人'} ] } }); </script> #### v-for操作对象与数值 <div id="hdcms"> <li v-for="(item,key,index) in user"> {{index}} - {{key}} - {{item}} </li> <table border="1" width="100%"> <tr v-for="v in 20"> <td>{{v}}</td> </tr> </table> </div> <script> var app = new Vue({ el: '#hdcms', data: { user: { name: '向军', age: '22', sex: 'boy' } } }); </script> #### v-for与computed结合功能实例讲解 <div id="hdcms"> <li v-for="v in stus"> {{v.name}} - {{v.sex}} </li> {{type}} <input type="radio" v-model="type" value="girl"> 女孩 <input type="radio" v-model="type" value="boy"> 男孩 </div> <script> var app = new Vue({ el: '#hdcms', computed:{ stus(){ if(this.type=='all'){ return this.user; }else{ return this.user.filter((v)=>{ return v.sex ==this.type; }); } } }, data: { type:'all', user:[ {name:'小明',sex:'boy'}, {name:'小强',sex:'boy'}, {name:'小丽',sex:'girl'}, {name:'小梅',sex:'girl'} ] } }); </script>