计算属性关键词: computed,计算属性在处理一些复杂逻辑时是很有用的。
我们可以使用 methods 来替代 computed,效果上两个都是一样的,但是 computed 是基于它的依赖缓存,只有相关依赖发生改变时才会重新取值。而使用 methods ,在重新渲染的时候,函数总会重新调用执行。使用 computed 性能会更好,但是如果你不希望缓存,可以使用 methods 属性。
实例1——购物车价格计算
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>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 {
display: flex;
flex-direction: column;
}
.item {
display: flex;
border: 1px solid #333333;
width: 100%;
height: 80px;
margin-bottom: 10px;
/* 垂直方向居中 */
align-items: center;
}
.s {
width: 100%;
display: flex;
justify-content: space-between;
}
.item-id {
flex: 1 1 15%;
}
.item-name {
flex: 1 1 15%;
}
.item-price {
flex: 1 1 15%;
}
.item-href {
flex: 1 1 15%;
margin-left: 20px;
}
.item-image {
flex: 1 1 15%;
margin-right: 10px;
}
.item-count {
display: flex;
}
.goods-count {
width: 15px;
}
/* .des {
margin-left: 10px;
} */
.right {
display: flex;
}
.item-image img {
width: 50px;
height: 50px;
}
.btn {
width: 100px;
height: 35px;
border-radius: 10px;
background-color: rgb(255, 80, 0);
color: white;
border: none;
outline: none;
margin-top: 10px;
}
.left {
display: flex;
}
a {
text-decoration: none;
color: #333333;
}
a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<!-- vue-app根容器 -->
<div id="app">
<div class="container">
<div class="item" v-for="goods in goods">
<div class="item-id">
{{goods.id}}
</div>
<div class="item-image">
<img :src="goods.image">
</div>
<div class="item-name">
{{goods.name}}
</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" :disabled="goods.count===0">+</button>
</div>
<div class="item-href">
<a v-bind:href="goods.url">{{goods.title}}</a>
</div>
</div>
</div>
<div class="s">
<div class="left">
<h3>总价:¥{{totalPrice}}</h3>
</div>
<div class="right">
<button type="button" class="btn" @click="settlement()">结算</button>
</div>
</div>
<div class="result" v-if="show">
你购买了{{settlement}}件商品,需要支付总价为:{{totalPrice}}元
</div>
</div>
<script type="text/javascript">
//实例化一个vue对象
var app = new Vue({
el: '#app',
data: {
goods: [{
id: 1,
image: 'img/i1.jpg',
name: 'iphone8',
price: 6000,
count: 1,
url: 'https://detail.tmall.com/item.htm?spm=a230r.1.14.1.7f185bfcTzMnaI&id=558550356564&cm_id=140105335569ed55e27b&abbucket=12&sku_properties=10004:709990523;5919063:6536025;12304035:3222911',
title: '查看详情'
},
{
id: 2,
image: 'img/i2.jpg',
name: 'iphonex',
price: 7000,
count: 1,
url: 'https://detail.tmall.com/item.htm?spm=a230r.1.14.1.3aed59cemGzBuN&id=562099309982&cm_id=140105335569ed55e27b&abbucket=12&sku_properties=10004:709990523;5919063:6536025;12304035:3222911',
title: '查看详情'
},
{
id: 3,
image: 'img/i3.png',
name: 'iphonexs',
price: 8000,
count: 1,
url: 'https://detail.tmall.com/item.htm?id=580731960102&ali_refid=a3_430583_1006:1102515942:N:iphone%20xs:26b08da512a5d348a1f0302803ba4b95&ali_trackid=1_26b08da512a5d348a1f0302803ba4b95&spm=a230r.1.14.1&sku_properties=10004:709990523;5919063:6536025',
title: '查看详情'
}
],
show: false
},
methods: {
},
computed: {
totalPrice: function() {
//定义一个变量
var totalPrice = 0;
for (var i = 0, len = this.goods.length; i < len; i++) {
totalPrice += this.goods[i].price * this.goods[i].count;
}
return totalPrice
},
settlement: function() {
this.show = true;
var totalCount = 0;
var len = this.goods.length;
for (var i = 0; i < len; i++) {
totalCount += this.goods[i].count;
}
return totalCount;
}
}
})
</script>
</body>
</html>
运行结果:
效果图.gif
实例2——搜索页面
<!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: 50%;
height: 25px;
margin-bottom: 10px;
}
.item {
display: flex;
width:100%;
height: 100px;
border: 1px solid #eee;
border-radius: 8px;
margin-bottom: 8px;
}
.item-title {
flex: 1 1 80%;
}
.item-thumbnail {
flex: 1 1 20%
}
.item-thumbnail img {
width: 100%;
height: 100%;
}
a {
text-decoration: none;
color: #333333;
}
a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<div id="app">
<div class="container">
<div class="header">
<input type="text" v-model="name" placeholder="请输入" class="input-box" />
<button type="button" @click="sou" >搜索</button>
</div>
<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: {
show:false,
searchString:'',
name:'',
searchString: "",
// 数据模型
articles: [{
"title": "堪称神器的3款在线工具,你一定用得上!",
"url": "https://www.jianshu.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": "https://www.jianshu.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": "https://www.jianshu.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": "https://www.jianshu.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": "https://www.jianshu.com/p/0a6a61b0933c",
"image": "https://upload-images.jianshu.io/upload_images/3525704-c7293758fc59e56b.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/960/format/webp"
},
{
"title": "知网、CSDN、百度文库、千图网、筑龙网,这一次我全都要!",
"url": "https://www.jianshu.com/p/b6076984fcf7",
"image": "https://upload-images.jianshu.io/upload_images/14886197-dabeb522fa95596f?imageMogr2/auto-orient/strip|imageView2/1/w/360/h/240"
},
{
"title": "知网、CSDN、百度文库、千图网、筑龙网,这一次我全都要!",
"url": "https://www.jianshu.com/p/b6076984fcf7",
"image": "https://upload-images.jianshu.io/upload_images/14886197-dabeb522fa95596f?imageMogr2/auto-orient/strip|imageView2/1/w/360/h/240"
}
]
},
methods:{
sou:function(){
this.searchString=this.name;
}
},
computed: {
// 计算函数,匹配搜索
filteredArticles: function() {
var articles_array = this.articles,
searchString = this.searchString;
//搜索关键词为空,则返回原始数据集
if (!searchString) {
return articles_array;
}
//搜索关键词去除无用空格,转换为小写
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>
运行结果:
效果图.gif