1.methods和computed的区别
如以下代码:computed1.html
运行结果: 7.png<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>计算</title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<button type="button" @click="show1()">调用methods方法点击</button>
<button type="button" @click="show2()">调用computer方法点击</button>
</div>
<script type="text/javascript">
/* 实例化一个Vue对象 /
var app = new Vue({
el: '#app',
data: {
name1:'methods',
name2:'computed'
},
methods:{
show1:function(){
/ console.log(this.name1); */
alert(this.name1);
}
},
computed:{
show2:function(){
alert(this.name2);
}
}
})
</script>
</body>
</html>
2.购物车:computed.html
<!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">
header{
background: #FF7F50;
width: 100%;
height: 120px;
color: #EEEEEE;
left: 20px;
}
.container{
left: 10px;
position: relative;
top: -20px;
display: flex;
flex-direction: column;
}
.item{
display: flex;
border: 1px solid #008000;
border-radius: 10px;
width: 85%;
height: 50px;
margin-bottom: 10px;
/* 垂直方向居中 */
align-items: center;
/* 水平方向居中 */
/* justify-content: center; */
/* 内边距 */
padding-left: 10px;
padding-right: 10px;
background: #EEEEEE;
}
.item-id{
flex: 1 1 20%;
}
.item-name{
flex: 1 1 20%;
}
.item-price{
flex: 1 1 20%;
}
.item-count{
flex: 1 1 20%;
}
.goods-count{
width: 15px;
text-align: center;
}
.accounts{
display: flex;
position: absolute;
background: #FF7F50;
width: 60px;
height: 40px;
/* 垂直方向居中 */
align-items: center;
border: 1px #008000;
border-radius: 10px;
left: 270px;
top: 220px;
outline:none;
justify-content: center;
}
.totle{
position: relative;
top: -55px;
left: 100px;
}
#photo{
position: relative;
width: 40px;
height: 40px;
left: 15px;
}
.settlement {
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;
}
</style>
</head>
<body>
<header>
<h3>购物车</h3>
<p>共3件宝贝</p>
</header>
<!-- vue-app的根容器 -->
<div id="app">
<div class="container">
<div class="item" v-for="goods in goodsList">
<div class="item-id">
{{goods.id}}
<img :src="goods.photo" id="photo"/>
</div>
<div class="item-name">
{{goods.name}}
</div>
<div class="item-price">
{{goods.price}}
</div>
<div class="item-count">
<!-- :disabled="goods.count===0"减到0不能再减了-->
<button type="button" @click="goods.count-=1" :disabled="goods.count===0">-</button>
<input type="text" v-model="goods.count" class="goods-count"/>
<button type="button" @click="goods.count+=1">+</button>
</div>
</div>
<hr >
<!-- <h3>Total price:</h3>
<p class="totle">¥{{totalPrice}}</p>
<button class="accounts">结算</button> -->
<div class="settlement">
<h3>总价:¥{{totalPrice}}</h3>
<button type="button" class="btn" @click="settlement()">结算</button>
</div>
<div class="result" v-if="show">
你购买了{{settlement}}件商品,需要支付总价为:{{totalPrice}}元
</div>
</div>
</div>
<script type="text/javascript">
// 实例化一个vue对象
var app=new Vue({
el:'#app',
data:{
goodsList:[
{
id:1,
name:'iphone 8',
price:6000,
count:1,
photo:'img/xphone.jpg'
},
{
id:2,
name:'iphone X',
price:7000,
count:2,
photo:'img/iphone2.jpg'
},
{
id:3,
name:'iphone XS Max',
price:8000,
count:1,
photo:'img/iphone3.jpg'
}
],
show:false
},
methods:{
},
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;
},
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>
运行结果:
8.png
3.搜索按钮进行页面搜索:computed.html
<!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>
<style type="text/css">
.container {
width: 90%;
margin: 0 auto;
}
.input-box {
width: 50%;
height: 25px;
margin-bottom: 10px;
}
.item {
display: flex;
height: 100px;
border: 1px solid #eee;
border-radius: 8px;
margin-bottom: 8px;
}
.item-title {
flex: 1 1 80%;
color: #00FFFF;
}
.item-thumbnail {
flex: 1 1 20%;
}
.item-thumbnail img {
width: 100%;
height: 100%;
}
a {
text-decoration: none;
}
a:link {
color: #8E8E8E;
text-decoration: underline;
}
a:visited {
color: #008000;
text-decoration: none;
}
a:hover {
color: #0000FF;
text-decoration: none;
}
a:active {
color: #FF7F50;
text-decoration: none;
}
.search {
position: relative;
top: -41px;
width: 60px;
height: 32px;
background: #7FFF00;
left: 50%;
margin-bottom: 10px;
/* 垂直方向居中 */
align-items: center;
display: flex;
border: 1px solid #DDDDDD;
outline: none;
}
</style>
</head>
<body>
<div id="app">
<div class="container">
<input type="text" v-model="searchString" placeholder="请输入" class="input-box" />
<button class="search" @click="handleClick()">搜索</button>
<div class="item" v-for="article in filteredArticles" v-show="show">
<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: "",
show: false,
// 数据模型
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: {
handleClick: function() {
this.show = true;
}
},
})
</script>
</body>
</html>
运行结果:
9.png
搜索按钮可以按,但是刚开始没有主页面,这个好像是一次性的。
form表单练习:form.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Vue.js表单练习</title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<input v-model="message1" placeholder="编辑我……">
<p>输入内容是: {{ message1 }}</p>
<textarea v-model="message2" placeholder="多行文本输入……"></textarea>
<p>多行文本域内容是:{{ message2 }}</p>
<input type="checkbox" id="baidu" value="BAIDU" v-model="checkedNames">
<label for="baidu">百度</label>
<input type="checkbox" id="google" value="GOOGLE" v-model="checkedNames">
<label for="google">谷歌</label>
<input type="checkbox" id="tencent" value="TENTCENT" v-model="checkedNames">
<label for="tencent">腾讯</label>
<span>选择的值为: {{ checkedNames }}</span>
<label>你一共选了{{checkedNames.length}}个</label>
<input type="radio" id="male" value="male" v-model="picked">
<label for="male">男</label>
<input type="radio" id="female" value="female" v-model="picked">
<label for="female">女</label>
<span>选中值为: {{ picked }}</span>
<select v-model="selected" name="fruit">
<option value="">选择一个网站</option>
<option value="www.baidu.com">百度</option>
<option value="www.taobao.com">淘宝</option>
<option value="www.niit.edu.cn">南工院</option>
</select>
<p> 选择的网站是: {{selected}}</p>
<button type="button" @click="commit">提交</button>
<hr >
<h2>结果显示</h2>
<div class="result">
<p>姓名:{{message1}}</p>
<p>性别:{{picked}}</p>
<h3>爱好</h3>
<ul>
<li v-for="item in checkedNames">
{{item}}
</li>
</ul>
<p>班级:{{selected}}</p>
</div>
</div>
<script type="text/javascript">
var app = new Vue({
el: '#app',
data: {
message1: 'Hello Vue',
message2: 'Vue官方教程',
checkedNames: [],
picked: 'male',
selected: ''
},
methods: {
commit: function() {
alert(this.message1 + this.picked + this.selected);
}
}
})
</script>
</body>
</html>
运行结果:
10.png
网友评论