条件判断:
- v-if的使用:
- v-if和v-else的使用:
- v-if和v-else-if和v-else的使用
- 用户登录切换的案例:
- v-show的使用:
循环遍历:
- v-for遍历数组:
- v-for遍历对象:
- v-for使用过程添加key:
- 哪些数组的方法是响应式的:
因为 Vue 是响应式的,所以当数据发生变化时, Vue 会自动检测数据变化,视图会发生对应的更新。Vue 中包含了一组观察数组编译的方法,使用它们改变数组也会触发视图的更新。
<div id="app">
<ul>
<li v-for="item in letters">{
{item}}</li>
</ul>
<button @click="btnClick">按鈕</button>
</div>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
letters: ['A', 'B', 'C', 'D', 'E']
},
methods: {
btnClick: function () {
//1.push方法
// this.letters.push('aaa');
//2.pop():删除数组中的最后一个元素
// this.letters.pop();
//3.shift():删除数组中的第一个元素
// this.letters.shift();
//4.unshift():在数组最前面添加元素
// this.letters.unshift('aaa')
//4.splice作用:删除元素、插入元素、替换元素
//删除元素:第二个参数传入你要删除几个元素(如果没有传,就删除后面所有的元素)
//替换元素:第二个参数,表示我们要替换几个元素,后面是用于替换前面的元素
//插入元素:第二个参数,传入0,后面要跟上插入的元素
//splice(start)
//splice(start)
// this.letters.splice(1,3,'m','n','1','x');
// this.letters.splice(1,0,'x','y','z');
//5.sort()
// this.letters.sort();
//6.reverse()
// this.letters.reverse();
//注意:通過索引值修改數組中的元素
//this.letters[0]='bbbb';
// this.letters.splice(0,1,'bbbbbb')
//set(要修改的对象,索引值,修改后的值)
Vue.set(this.letters,0,'bbbbb')
}
}
})
</script>
书籍购物车案例:
style.css:
table {
border: 1px solid #e9e9e9;
border-collapse: collapse;
border-spacing: 0;
}
th, td {
padding: 8px 16px;
border: 1px solid #e9e9e9;
text-align: left;
}
th {
background-color: #f7f7f7;
color: #5c6b77;
font-weight: 600;
}
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="app">
<div v-if="books.length>0">
<table>
<thead>
<tr>
<th></th>
<th>书籍名称</th>
<th>出版日期</th>
<th>价格</th>
<th>购买数量</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr v-for="(book,index) in books">
<td>{
{book.id}}</td>
<td>{
{book.name}}</td>
<td>{
{book.date}}</td>
<td>{
{book.price | showPrice}}</td>
<td>
<button @click="decrement(index)" v-bind:disabled="book.count<=1">-</button>
{
{book.count}}
<button @click="increment(index)">+</button>
</td>
<td>
<button @click="remove(index)">移除</button>
</td>
</tr>
</tbody>
</table>
<h2>总价格:{
{totalPrice | showPrice}}</h2>
</div>
<h2 v-else>购物车为空</h2>
</div>
<script src="../js/vue.js"></script>
<script src="main.js"></script>
<script>
</script>
</body>
</html>
main.js:
const app = new Vue({
el: '#app',
data: {
books: [{
id: 1,
name: '《算法导论》',
date: '2006-9',
price: 85.00,
count: 1
},
{
id: 2,
name: '《UNIX编程艺术》',
date: '2006-2',
price: 59.00,
count: 1
},
{
id: 3,
name: '《编程珠玑》',
date: '2008-10',
price: 39.00,
count: 1
},
{
id: 4,
name: '《代码大全》',
date: '2006-3',
price: 128.00,
count: 1
},
]
},
methods: {
// getFinalPrice(price){
// return '¥'+price.toFixed(2);
// }
decrement(index){
this.books[index].count--;
},
increment(index){
this.books[index].count++;
},
remove(index){
this.books.splice(index,1);
}
},
computed: {
totalPrice(){
let totalPrice = 0;
for (let i =0;i<this.books.length;i++){
totalPrice += this.books[i].price * this.books[i].count;
}
return totalPrice;
}
},
filters: {
showPrice(price){
return '¥'+price.toFixed(2);
}
}
})