使用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、顯示效果

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM