1.Vue動態渲染列表------v-for用法詳解:html
Html:vue
<figure v-for="list in lists">
<div>
<a v-bind:href=" list.big">
<img v-bind:src="list.small">
</a>
</div>
</figure>ajax
<template v-for="item in items">
<li>{{ item.msg }}</li>
<li class="divider"></li>
</template>
渲染在頁面上的樣式: < figure >...</ figure >json
< figure >...</ figure >數組
< figure >...</ figure >ide
用法:函數
v-for=‘[自定義名字] in [json中數組名字]’this
(1)元素內部的屬性用v-bind:[屬性] = ‘[自定義名字].json數組中的屬性’url
(2)標籤內容引用 {{ [自定義名字].json數組中的屬性 }}htm
JS-vue-ajax:
var vm = new Vue({
el:'#main',
data:{
lists : '',
honors:''
},
methods:{
listMessage: function () {
var _self = this;
$.ajax({
type: 'GET',
url: 'js/json/photolist.json',
success:function(data) {
_self.lists = data.lists;
}
});
},
listMessageTwo: function () {
var _self = this;
$.ajax({
type: 'GET',
url: 'js/json/photolist.json',
success:function(data) {
_self.honors = data.honors;
}
});
}
},
ready:function(){
var _this =this;
_this.listMessage();
_this.listMessageTwo();
}
})
用法:
data:{
lists : '',
honors:''
}
json中:
{{
"lists": [
{
"big": "images/photo-end.jpg",
"small":"images/photo2.jpg"
} ],
"honors":[
{
"big": "images/photo-end.jpg",
"small":"images/photo.png"
}
]
}
success:function(data) {
_self.lists = data.lists;
}
成功獲取數據後,將 json中的數組名賦值給 data中的數組名,在經過html中v-for的引用,將json數組中的數據傳值到html中
注意:
methods方法中的函數須要調用才能執行,若是頁面是在已進入就執行後臺數據渲染的,須要在methods方法下添加 ready函數,並在其中引用ajax函數
ready:function(){
var _this =this;
_this.listMessage();
_this.listMessageTwo();
索引用法:
<li v-on:click="typeNavClick($event,$index)" v-for="(index,main) in mainList"