使用vuejs框架进行列表渲染

 

 爱编程爱分享,原创文章,转载请注明出处,谢谢!http://www.cnblogs.com/fozero/p/6170706.html 

 

1、通过Script引入Vuejs框架

<script type="text/javascript" src="https://unpkg.com/vue@2.1.4/dist/vue.js"/>

 

2、实例化Vue并配置Vue选项 

var vm = new Vue({
          el: '#app',
          data: {
              shops:''
          },
          created:function(){//实例创建时被调用
              this.getShopList();
          },
          methods:{
              getShopList:function(){//获取店铺列表
                  $.get(WEB_API_URL+"/Api/Shop",{
                    r:Math.random()
                },function(result){
//                    console.log(JSON.stringify(result)); 
                    if(result.errno==0){
                         $.each(result.data,function(index,item){
                             //数组对象添加imgurl元素
                             var img_url=shop_icons[Math.floor(Math.random()*shop_icons.length)];
                             item["imgurl"]=img_url;
                         });
                         vm.shops = result.data;
                    }else{
                        alert("服务器出错啦~");
                    }
                });
              }
          }
          
        });

 

说明:

选项中的el属性绑定页面中id为app的div

Vuejs框架提供一系列钩子函数 ,created方法在Vue实例创建时被调用

我们所有的方法定义在methods选项中,这里我们定义获取店铺列表的方法getShopList,然后在created方法中调用

最后数据请求成功之后进行数据绑定

 

 

3、使用v-for指定对列表渲染

<li v-for="shop in shops">
                    <a href="store_detail.html" v-bind:id="shop.ID" v-bind:baiduid="shop.baidu_id" v-bind:meituanid="shop.meituan_id">
                        <div class="left mend_img">
                            <img v-bind:src="shop.imgurl"/>
                        </div>
                        <div class="left name">
                            <h1>{{shop.shop_name}}</h1>
                            <label>{{shop.shop_address}}</label>
                        </div>
                        <div class="clearfix"></div>
                    </a>
                </li>

 

 4、显示效果

 

转载于:https://www.cnblogs.com/fozero/p/6170706.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值