computed是Vue中的计算属性,与之前的data、methods同级,适合处理复杂的逻辑关系,优先级大于methods
示例1:使用computed计算购物车中商品价格
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>Vue.js computed练习——计算购物车总价</title>
<!--通过cdn引入Vue.js -->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<style type="text/css">
.container {
display: flex;
flex-direction: column;
}
.item {
display: flex;
border: 1px solid #5F9EA0;
border-radius: 4px;
width: 95%;
height: 100px;
/* 垂直方向居中 */
align-items: center;
/* 水平方向居中
justify-content:center ; */
margin-bottom: 10px;
padding-left: 20px;
padding-right: 20px;
}
.item-id {
flex: 1 1 10%;
}
.item-img {
flex: 1 1 20%;
}
.item-img img {
width: 30%;
height: 30%;
}
.item-name {
flex: 1 1 25%;
font-size: 14px;
}
.item-price {
flex: 1 1 20%;
color: crimson
}
.item-count {
flex: 1 1 30%;
}
.goods-count {
width: 15px;
text-align: center;
}
.settlement {
width: 95%;
display: flex;
justify-content: space-between;
align-items: center;
}
.btn {
width: 100px;
height: 40px;
background-color: #FF5000;
border-radius: 10px;
border: none;
outline: none;
color: #FFF;
font-size: 16px;
}
a:link {
color: black;
text-decoration: none;
}
</style>
</head>
<body>
<!-- Vue-app的根容器 -->
<div id="app">
<div class="container">
<div class="item" v-for="goods in goodsList">
<div class="item-id">
{{goods.id}}
</div>
<div class="item-img">
<img :src="goods.pic">
</div>
<div class="item-name">
<a :href="goods.url">{{goods.name}}</a>
</div>
<div class="item-price">
¥{{goods.price}}
</div>
<div class="item-count">
<button type="button" @click="goods.count-=1" :disabled="goods.count ===0">-</button>
<input type="text" class="goods-count" v-model="goods.count" />
<button type="button" @click="goods.count+=1">+</button>
</div>
</div>
<hr>
<div class="settlement">
<h3>Total Price: ¥{{totalPrice}}</h3>
<button type="button" class="btn" @click="settlement()">结算</button>
</div>
<div v-if="show">
你购买了{{settlement}}件商品,需要支付总价为:{{totalPrice}}元
</div>
</div>
</div>
<script type="text/javascript">
//实例化一个Vue对象
var app = new Vue({
el: '#app',
data: {
show: false,
goodsList: [{
id: 1,
name: '蓝牙无线耳机',
pic: 'https://images-cn.ssl-images-amazon.com/images/I/4118+OyzLgL._AA200_.jpg',
price: 729,
url: 'https://www.amazon.cn/dp/B01M032X28/ref=lp_1397649071_1_4?s=music-players&ie=UTF8&qid=1552439873&sr=1-4',
count: 1
},
{
id: 2,
name: '数码相框',
pic: 'https://images-cn.ssl-images-amazon.com/images/I/41Jptto1bQL._AA200_.jpg',
price: 359,
url: 'https://www.amazon.cn/dp/B00F5HHSIA/ref=lp_1994998051_1_2?s=music-players&ie=UTF8&qid=1552440081&sr=1-2',
count: 1
},
{
id: 3,
name: 'USB连接线',
pic: 'https://images-cn.ssl-images-amazon.com/images/I/41lPp5aprnL._AA200_.jpg',
price: 60,
url: 'https://www.amazon.cn/dp/B006GWO5NE/ref=lp_760238051_1_3?s=music-players&ie=UTF8&qid=1552440314&sr=1-3',
count: 1
},
]
},
methods: {
},
computed: {
totalPrice: function() {
var totalPrice = 0;
for (var i = 0, len = this.goodsList.length; i < len; i++) {
totalPrice += this.goodsList[i].price * this.goodsList[i].count;
}
return totalPrice;
},
settlement: function() {
this.show = true;
var totalCount = 0;
var len = this.goodsList.length;
for (var i = 0; i < len; i++) {
totalCount += this.goodsList[i].count;
}
return totalCount;
}
}
})
</script>
</body>
</html>
示例2:使用computed实现搜索过滤
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Vue.js computed和过滤的练习-搜索页面的实时响应</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<style type="text/css">
.container {
width: 90%;
margin: 0 auto;
}
.input-box {
width: 100%;
height: 30px;
margin-top: 5px;
margin-bottom: 10px;
}
.item {
display: flex;
height: 100px;
border: 1px solid #eee;
border-radius: 8px;
margin-bottom: 8px;
padding: 5px;
}
.item-title {
flex: 1 1 80%;
}
.item-thumbnail {
flex: 1 1 20%
}
.item-thumbnail img {
width: 100%;
height: 100%;
border-radius: 5px;
}
a {
color: #000;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<div id="app">
<div class="container">
<input type="text" v-model="searchString" placeholder="请输入" class="input-box" />
<div class="item" v-for="article in filteredArticles">
<a :href="article.url" class="item-title">
{{article.title}}
</a>
<div class="item-thumbnail">
<img :src="article.image">
</div>
</div>
</div>
</div>
<script type="text/javascript">
var app = new Vue({
el: '#app',
data: {
searchString: "",
// 数据模型
articles: [{
"title": "堪称神器的3款在线工具,你一定用得上!",
"url": "//www.greatytc.com/p/e83e7999346b",
"image": "https://upload-images.jianshu.io/upload_images/11438996-56b25f32c9307b4b?imageMogr2/auto-orient/strip%7CimageView2/2/w/640/format/webp"
},
{
"title": "经典面试题:从 URL 输入到页面展现到底发生什么?",
"url": "//www.greatytc.com/p/45ba3e0d0c7e",
"image": "https://upload-images.jianshu.io/upload_images/3973862-d90954249a6f6ccd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp"
},
{
"title": "如何免翻墙使用谷歌搜索和Chrome应用商店",
"url": "//www.greatytc.com/p/484f8e6c88f6",
"image": "https://upload-images.jianshu.io/upload_images/858154-015a4b083685a3d1.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/800/format/webp"
},
{
"title": "四款前所未有好用的黑科技APP,绝对的良心实用,赶紧告诉家人",
"url": "//www.greatytc.com/p/2aec84d269fe",
"image": "https://upload-images.jianshu.io/upload_images/16042993-168b2cb17fd7ec0c?imageMogr2/auto-orient/strip%7CimageView2/2/w/640/format/webp"
},
{
"title": "坚持学英语的方法有哪些",
"url": "//www.greatytc.com/p/0a6a61b0933c",
"image": "https://upload-images.jianshu.io/upload_images/3525704-c7293758fc59e56b.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/960/format/webp"
}
]
},
computed: {
// 计算函数,匹配搜索
filteredArticles: function() {
var articles_array = this.articles;
var searchString = this.searchString;
//搜索关键词为空,则返回原始数据集
if (!searchString) {
return this.articles;
}
//搜索关键词去除无用空格,转换为小写
searchString = searchString.trim().toLowerCase();
//过滤数组中每个元素
articles_array = articles_array.filter(function(item) {
if (item.title.toLowerCase().indexOf(searchString) !== -1) {
return item;
}
})
// 返回转化后的数组
return articles_array;
}
}
})
</script>
</body>
</html>