计算属性关键词:computed,计算属性在处理一些复杂逻辑时是很有用的。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>computed,计算属性</title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<p>原始字符串: {{ message }}</p>
<p>计算后反转字符串: {{ reversedMessage }}</p>
</div>
<script>
var vm = new Vue({
el: '#app',
data: {
message: 'Runoob!'
},
computed: {
// 计算属性的 getter
reversedMessage: function() {
// `this` 指向 vm 实例
return this.message.split('').reverse().join('')
}
}
})
</script>
</body>
</html>
computed vs menthods
我们可以使用 methods 来替代 computed,效果上两个都是一样的,但是 computed 是基于它的依赖缓存,只有相关依赖发生改变时才会重新取值。而使用 methods ,在重新渲染的时候,函数总会重新调用执行。使用 computed 性能会更好,但是如果你不希望缓存,可以使用 methods 属性。
实例1——购物车价格计算
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>vue.js computed 计算购物车总价</title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<style type="text/css">
.container {
display: flex;
flex-direction: column;
}
.item {
display: flex;
/* 富集容器 */
border: 1px solid aqua;
width: 400px;
height: 30px;
border-radius: 10px;
margin-bottom: 10px;
align-items: center;
/* 前提是flex布局 垂直方向居中 */
/* justify-content: center; */
/* 水平方向居中 */
padding-left: 10px;
/* 内边距 */
padding-right: 10px;
/* 外边距 */
}
.item-id {
flex: 1 1 20%;
}
.item-photo {
flex: 1 1 20%;
}
.photo {
width: 50%;
height: 50%;
}
.item-name {
flex: 1 1 30%;
}
.item-price {
flex: 1 1 20%;
}
.item-count {
display: flex;
flex: 1 1 30%;
}
.goods-count {
width: 15px;
}
.total {
display: flex;
margin-left: 100px;
}
.account {
width: 50px;
height: 50px;
color: rgb(234, 111, 90);
border-radius: 20px;
font-size: 14px;
background-color: #FFF;
border: 1px solid rgb(234, 111, 90);
}
.btn {
width: 50px;
height: 30px;
color: rgb(234, 111, 90);
border-radius: 20px;
font-size: 14px;
background-color: #FFF;
border: 1px solid rgb(234, 111, 90);
}
.a {
color: #00FFFF;
text-decoration: none;
}
</style>
</head>
<body>
<span id="app">
<div class="container">
<div class="item" v-for="goods in goodsList">
<div class="item-id">
{{goods.id}}
</div>
<div class="item-photo">
<img :src="goods.photo" class="photo">
</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>
<!-- disabled 按钮不可点 -->
<input type="text" v-model="goods.count" class="goods-count" />
<button type="button" @click="goods.count +=1">+</button>
</div>
<div class="item-details">
<button type="button" class="btn"><a class="a" href="https://item.jd.com/32967779008.html?extension_id=eyJhZCI6IjE0NzYiLCJjaCI6IjIiLCJza3UiOiIzMjk2Nzc3OTAwOCIsInRzIjoiMTU1MjQ0MTMyMiIsInVuaXFpZCI6IntcImNsaWNrX2lkXCI6XCI3N2I0ODU5OS00Zjc4LTQ4YWEtODQ0OC1iMTA2ZmUyNDgzMWVcIixcIm1hdGVyaWFsX2lkXCI6XCIzMzkwNTk5NDFcIixcInBvc19pZFwiOlwiMTQ3NlwiLFwic2lkXCI6XCI0Y2U2MDMwYS05MTlmLTRkMWMtYmYwOS1iNzcxNzU3MDQ2ODdcIn0ifQ==&jd_pop=77b48599-4f78-48aa-8448-b106fe24831e&abt=0">详情</a></button>
</div>
</div>
<hr />
<div class="total">
<div class="total1">
<h3>Total price : </h3>
</div>
<div class="total2">
<p>¥{{totalPrice}}</p>
</div>
<div class="total3">
<button type="button" @click="handeClike" class="account">结算</button>
</div>
</div>
</div>
</span>
</body>
<script type="text/javascript">
var app = new Vue({
el: '#app',
data: {
goodsList: [{
id: 1,
photo: 'img/i8.jpg',
name: 'iPhone8',
price: 6000,
count: 4,
}, {
id: 2,
photo: 'img/i8.jpg',
name: 'iPhone9',
price: 8000,
count: 1,
}, {
id: 3,
photo: 'img/i8.jpg',
name: 'iPhone10',
price: 9000,
count: 1,
}]
},
computed: {
totalPrice: function() {
var totalPrice = 0;
var len = this.goodsList.length;
for (var i = 0; i < len; i++) {
totalPrice += this.goodsList[i].price * this.goodsList[i].count;
}
return totalPrice;
},
},
methods: {
handeClike: function() {
alert("您购买的商品为:\n" + this.goodsList[0].name + "\t数量为:" + this.goodsList[0].count + "\n" +
this.goodsList[1].name + "\t数量为:" + this.goodsList[1].count + "\n" +
this.goodsList[2].name + "\t数量为:" + this.goodsList[2].count + "\n" +
"总价格为:" + this.totalPrice + "元");
}
}
})
</script>
</html>
购物车.png
结算.png
实例2——搜索页面
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Vue.js computed练习-搜索页面的实现</title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<link rel="stylesheet" type="text/css" href="css/font-awesome.min.css" />
<style type="text/css">
.container {
width: 90%;
margin: 0 auto;
}
.sousuo {
display: flex;
align-items: center;
width: 50%;
height: 35px;
}
.sousuo-text {
width: 100%;
height: 80%;
}
.sousuo-picture {
align-items: center;
}
.input-box {
width: 80%;
height: 100%;
margin-bottom: 10px;
}
.item {
display: flex;
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%;
border-bottom-right-radius: 10px;
border-top-right-radius: 10px;
}
a {
color: black;
text-decoration: none;
}
</style>
</head>
<body>
<div id="app">
<div class="container">
<div class="sousuo">
<div class="sousuo-text">
<input type="text" v-model="title" placeholder="请输入" class="input-box" />
</div>
<div class="sousuo-picture">
<a @click="sousuo"><i class="icon-search"></i></a>
</div>
</div>
<div class="item" v-for="article in filteredArticles">
<a :href="article.url" class="item-title">
{{article.title}}
</a>
<div class="item-thumbnail">
<a :href="article.url" class="item-title">
<img :src="article.image">
</a>
</div>
</div>
</div>
</div>
<script type="text/javascript">
var app = new Vue({
el: '#app',
data: {
show: false,
title: '',
searchString: "",
// 数据模型
articles: [{
"title": "堪称神器的3款在线工具,你一定用得上!",
"url": "https://www.jianshu.com/p/e83e7999346b",
"image": "https://img.haomeiwen.com/i11438996/56b25f32c9307b4b?imageMogr2/auto-orient/strip%7CimageView2/2/w/640/format/webp"
},
{
"title": "经典面试题:从 URL 输入到页面展现到底发生什么?",
"url": "https://www.jianshu.com/p/45ba3e0d0c7e",
"image": "https://img.haomeiwen.com/i3973862/d90954249a6f6ccd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp"
},
{
"title": "如何免翻墙使用谷歌搜索和Chrome应用商店",
"url": "https://www.jianshu.com/p/484f8e6c88f6",
"image": "https://img.haomeiwen.com/i858154/015a4b083685a3d1.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/800/format/webp"
},
{
"title": "四款前所未有好用的黑科技APP,绝对的良心实用,赶紧告诉家人",
"url": "https://www.jianshu.com/p/2aec84d269fe",
"image": "https://img.haomeiwen.com/i16042993/168b2cb17fd7ec0c?imageMogr2/auto-orient/strip%7CimageView2/2/w/640/format/webp"
},
{
"title": "坚持学英语的方法有哪些",
"url": "https://www.jianshu.com/p/0a6a61b0933c",
"image": "https://img.haomeiwen.com/i3525704/c7293758fc59e56b.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/960/format/webp"
}
]
},
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;;
}
},
methods: {
sousuo: function() {
this.searchString = this.title;
}
}
})
</script>
</body>
</html>
网友评论