day07 computed方法

  • computed和methods

在vue.js中,有methods和computed两种方式来动态当作方法来用的:首先最明显的不同 就是调用的时候,methods要加上();我们可以使用 methods 来替代 computed,效果上两个都是一样的,但是 computed 是基于它的依赖缓存,只有相关依赖发生改变时才会重新取值。而使用 methods ,在重新渲染的时候,函数总会重新调用执行

  • 代码
<html>
    <head>
        <meta charset="utf-8" />
        <title></title>
        <title>computed练习</title>
        <!-- 开发环境版本,包含了有帮助的命令行警告 -->
        <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    </head>
    <body>
        <!-- vue-app的根容器 -->
        <div id="app">
            
            <button type="button" @click="show2">调用methods</button>
            <button tuype="button" @click="show1">调用computed</button>
        </div>
        <script type="text/javascript">
            var app=new Vue({
                el:'#app',
                data:{
                    name1:'methods',
                    name2:'这是Vue.jsd的computed计算练习'
                },
                methods:{
                    show2:function(){
                        alert(this.name1);
                        
                    }
                    },
                computed:{
                    show:function(){
                        alert(this.name2);
                        
                    }
                }   
                
            })
        </script>
    </body>
</html>
  • 简单购物车
<html>
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
        <title></title>
        <title>购物车练习</title>
        <!-- 开发环境版本,包含了有帮助的命令行警告 -->
        <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
        <style type="text/css">
            .a{
                width: 40%;
                margin: 0 auto;
            }
            .container{
                display: flex;
                flex-direction: column;
            }
            .item{
                display: flex;
                border: 1px solid #008000;
                border-radius: 10px;
                width: 350px;
                height: 50px;
                margin-bottom: 10px;
                /* 垂直方向居中 */
                align-items: center;
                /* 水平方向居中 */
                /* justify-content: center; */
                padding-left: 10px;
                padding-right: 10px;
            }
            .item-id{
                flex: 1 1 20%;
            }
            .item-name{
                flex: 1 1 20%;
            }
            .item-price{
                flex: 1 1 30%;
            }
            .item-count{
                /* display: flex; */
                flex: 1 1 30%;
            }
            .goods-count{
                width: 15px;
            }
            .btn{
                border: 1px #3083ff solid;
                border-radius: 4px;
                background-color: #3487ff;
                box-shadow: 0 5px 8px 0 rgba(24,95,255,.1);
                color: #fff;
                text-align: center;
                font-weight: lighter;
                background-image: linear-gradient(0deg,#398bff,#3083ff);
                width: 60px;
                height: 40px;
                /* margin: 40px 0 8px; */
                font-size: 18px;
            }
            img{
                width: 45px;
                height: 45px;
            }
            .item-img{
                flex: 1 1 30%;
            }
            .commit{
                width: 350px;
                display: flex;
                justify-content: space-between;
                align-content: center;
            }
            a:link{
                text-decoration: none;
            }
        </style>
    </head>
    <body>
        <!-- vue-app的根容器 -->
        <div id="app" class="a">
            <div class="container">
                
                <div class="item" v-for="goods in goodsList">
                    <div class="item-id" >
                        {{goods.id}}
                    </div>
                    <div class="item-name">
                        <a :href="goods.url">
                        {{goods.name}}
                        </a>
                    </div>
                    <div class="item-img">
                        <img :src="goods.image" />
                    </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" v-model="goods.count" class="goods-count"/>
                        <button type="button" @click="goods.count+=1">+</button>
                    </div>
                </div>
            </div>
            <hr/>
            <div class="commit">
                <p>总价:¥{{totalPrice}}</p>
                <button type="button" class="btn" @click="settl">结算</button>
            </div>
        </div>
        <script type="text/javascript">
            var app=new Vue({
                el:'#app',
                data:{
                    goodsList:[{
                        id:1,
                        name:'iPhone7',
                        price:5000,
                        count:1,
                        image:'img/iPhone7-2.jpg',
                        url:'https://item.jd.com/3133811.html'
                    },
                    {
                        id:2,
                        name:'iPhoneX',
                        price:7000,
                        count:2,
                        image:'img/iPhoneX.jpg',
                        url:'https://item.jd.com/5089253.html'
                    },
                    {
                        id:3,
                        name:'iPhone8',
                        price:6000,
                        count:1,
                        image:'img/iPhone8.jpg',
                        url:'https://item.jd.com/5089269.html'
                    }
                    ]
                },
                methods:{
                    settl:function(){
                        alert("您好,您本次购买手机"+this.totalCount+"部,共消费"+this.totalPrice+"元。");
                    }
                    },
                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;
                    },
                    totalCount: 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>

效果图:


day07-1.png
  • 文章搜索
<html>
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
        <title>搜索页面的实现</title>
        <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
        <style type="text/css">
            .container {
                width: 60%;
                margin: 0 auto;
            }

            .input-box {
                width: 50%;
                height: 25px;
                margin-bottom: 10px;
            }
            .btn{
                width: 50px;
                height: 25px;
                border: 1px #3083ff solid;
                border-radius: 4px;
                background-color: #3487ff;
                box-shadow: 0 5px 8px 0 rgba(24,95,255,.1);
                text-align: center;
                font-weight: lighter;
                background-image: linear-gradient(0deg,#398bff,#3083ff);
                font-size: 18px;
                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%;
            }
            a:link{
                color:#333333;
                text-decoration: none;
            }
        </style>
    </head>
    <body>
        <div id="app">
            <div class="container">
                <input type="text" v-model="searchString" placeholder="请输入" class="input-box" />
                <!-- <button type="button" class="btn" @click="filteredArticles">搜索</button> -->
                <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": "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"
                        }
                    ]
                },
                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>

效果图:


day07-2.png
day07-3.png
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 196,264评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,549评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 143,389评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,616评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,461评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,351评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,776评论 3 387
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,414评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,722评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,760评论 2 314
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,537评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,381评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,787评论 3 300
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,030评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,304评论 1 252
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,734评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,943评论 2 336

推荐阅读更多精彩内容

  • 一:什么是闭包?闭包的用处? (1)闭包就是能够读取其他函数内部变量的函数。在本质上,闭包就 是将函数内部和函数外...
    xuguibin阅读 9,451评论 1 52
  • vue概述 在官方文档中,有一句话对Vue的定位说的很明确:Vue.js 的核心是一个允许采用简洁的模板语法来声明...
    li4065阅读 7,178评论 0 25
  • 这篇笔记主要包含 Vue 2 不同于 Vue 1 或者特有的内容,还有我对于 Vue 1.0 印象不深的内容。关于...
    云之外阅读 5,042评论 0 29
  • 33、JS中的本地存储 把一些信息存储在当前浏览器指定域下的某一个地方(存储到物理硬盘中)1、不能跨浏览器传输:在...
    萌妹撒阅读 2,054评论 0 2
  • 在上一节中我们了解了 DRF ,现在我们要开始学习 Vue 了,这一前端神器。同样的,这不是官方文档复读机,我们会...
    ucag阅读 1,996评论 1 5