關於vue.js中列表渲染練習

來源:互聯網
上載者:User

標籤:meta   lang   ext   utf-8   message   vue   bsp   class   標籤   

html:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>列表渲染</title>
</head>
<body>
<!-- v-for可以將一組數組渲染到列表當中 -->
<!-- 以item in items的形式 其中 items是來源資料 item是他的別名 -->
<ul id="app-1">
<li v-for="item in items">
<!-- 注意:這裡的item和message定義要前後一致 -->
{{item.message}}
</li>
</ul>
<!-- 在v-for模組中我們有對父範圍的完整存取 其中還可以增加一個參數 索引index -->
<ul id="app-2">
<!-- v-for使用在li中 -->
<li v-for="(item2,index) in items2">
{{parentMessage}}-{{index}}-{{item2.message2}}
</li>
</ul>
<!-- 注意:還可以用of 代替 in -->
<!-- 如同v-if v-for也可以與template一起使用來渲染多個元素塊 -->
<ul id="app-3">
<template v-for="item3 of items3">
<!-- 標籤中{{}}不能忘記!!! -->
<li>{{item3.message3}}</li>
<li v-bind:class="{active: isActive}"></li>
</template>
</ul>

<!-- v-for還可以與對象的屬性連用 -->
<ul id="app-4">
<li v-for="value in object">
{{value}}
</li>
</ul>
<!-- 還可以添加鍵名參數 -->
<ul id="app-5">
<li v-for="(value , key) in object2">
{{ key }} : {{ value }}
</li>
</ul>
<!-- 還可以添加index -->
<ul id="app-6">
<li v-for="(value , key ,index) in object3">
{{ key }} : {{ value }} : {{ index }}
</li>
</ul>

<!-- v-for還可以用於整數 -->
<div id="app-7">
<!-- 注意:唯寫這些代碼是運行不了的 還要進行new Vue -->
<span v-for="n in 10">{{ n }}</span>
</div>

<script src="js/vue.js"></script>
<script src="js/vuetext.js"></script>
</body>
</html>

 

js:

var app=new Vue({
el:‘#app-1‘,
data:{
items:[
{message:‘我是列表1‘},
{message:‘我是列表2‘}
]
}
});
var app2 = new Vue({
el: ‘#app-2‘,
data: {
parentMessage: ‘Parent‘,
items2: [
{ message2: ‘Foo‘ },
{ message2: ‘Bar‘ }
]
}
});
var app4=new Vue({
el:‘#app-4‘,
data:{
object:{
FirstName: ‘John‘,
LastName: ‘Doe‘,
Age: 30
}
}
});
var app5=new Vue({
el:‘#app-5‘,
data:{
object2:{
FirstName: ‘John‘,
LastName: ‘Doe‘,
Age: 30
}
}
});
var app6=new Vue({
el:‘#app-6‘,
data:{
object3:{
FirstName: ‘John‘,
LastName: ‘Doe‘,
Age: 30
}
}
});
var app7=new Vue({
el:‘#app-7‘,
});

 

未完...

關於vue.js中列表渲染練習

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.