Vue.js中computed練習(xí)

1.編寫售賣界面

1.1代碼編寫

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>computed計(jì)算屬性練習(xí)</title>

<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<style type="text/css">
.container {
display: flex;
flex-direction: column;
width: 90%;
margin: 0 auto;
}

        .item {
            display: flex;
            border: 1px solid #eee;
            border-radius: 10px;
            width: 95%;
            height: 150px;
            margin-bottom: 8px;
            /* 垂直方向居中 */
            align-items: center;
            /* 水平方向居中 */
            /* justify-content: center; */
            padding-left: 10px;
            padding-right: 10px;
        }

        .item-id {
            flex: 1 1 10%;
        }

        .item-name {
            flex: 1 1 20%;
            font-weight: bold;
            font-size: 20px;
        }

        .item-img {
            flex: 1 1 20%
        }

        .item-img img {
            width: 40%;
            height: 40%;
        }

        .item-price {
            flex: 1 1 20%;
            font-weight: bold;
            font-size: 20px;
        }

        .item-count {
            flex: 1 1 30%;
        }

        .goods-count {
            width: 35px;
            height: 20px;
            text-align: center;
        }
        .item-count button{
            width: 30px;
            height: 30px;
            font-weight: bold;
        }
        .setbt{
            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>
    <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.cover">
                </div>
                <div class="item-name">
                    <a :href="goods.ul">{{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="setbt">
                <h3>總價(jià):¥{{totalPrice}}</h3>
                <button type="button" class="btn" @click="setbt()">結(jié)算</button>
            </div>
            <div class="result" v-if="show">
                你購買了{(lán){setbt}}件商品,需要支付總價(jià)為:{{totalPrice}}元
            </div>
        </div>
    </div>
    <script type="text/javascript">
        var app = new Vue({
            el: '#app',
            data: {
                goodsList: [{
                        id: 1,
                        name: 'iPhone 8',
                        cover: 'img/001.jpg',
                        price: 6000,
                        count: 1,
                        "ul":"https://gouwu.sogou.com/compare?query=202144365&cateid=198;199;200&adsUrl=http%3A%2F%2Fitem.jd.com%2F6077638.html&isads=1&bids=&pid=sogou-wsse-492114f6915a69aa-0000&host=sogou.com",
                    },
                    {
                        id: 2,
                        name: 'iPhone 6',
                        cover: 'img/002.jpg',
                        price: 7000,
                        count: 1,
                        "ul":"https://gouwu.sogou.com/compare?query=159696816&cateid=198;199;200&adsUrl=http%3A%2F%2Fwww.zol.com%2Fdetail%2Fcell_phone%2Fapple%2F28098081.html&isads=1&bids=&pid=sogou-wsse-492114f6915a69aa-0000&host=sogou.com",
                    },
                    {
                        id: 3,
                        name: 'iPhone X',
                        cover: 'img/003.jpg',
                        price: 8000,
                        count: 1,
                        "ul":"https://gouwu.sogou.com/compare?query=202144330&cateid=198;199;200&adsUrl=http%3A%2F%2Fitem.jd.com%2F6072692.html&isads=1&bids=&pid=sogou-wsse-492114f6915a69aa-0000&host=sogou.com",
                    }
                ],
                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;
                },
                setbt: 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>

1.2結(jié)果效果圖

image.png

1.3使其在手機(jī)上掃碼運(yùn)行

1.3.1使用計(jì)算機(jī)cmd命令輸入ipconfig,出現(xiàn)以下界面,復(fù)制其中IPv4地址

image.png

1.3.2將代碼運(yùn)行到瀏覽器,并將復(fù)制的地址覆蓋下圖中的紅框部分,然后將最后覆蓋后的整體鏈接復(fù)制下來
image.png

1.3.3將復(fù)制好的整體鏈接放入HBuilder中的web瀏覽器中,敲回車,然后點(diǎn)擊紅色圈類的二維碼,用手機(jī)瀏覽器掃碼獲取

image.png

1.3.4最后手機(jī)成功圖片

image.png

2.練習(xí)-搜索頁面的實(shí)現(xiàn)

2.1代碼編寫

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Vue.js computed練習(xí)-搜索頁面的實(shí)現(xiàn)</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%;
        }

        .item-thumbnail {
            flex: 1 1 20%
        }

        .item-thumbnail img {
            width: 100%;
            height: 100%;
        }
    </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">
                    <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: {
                searchString: "",
                // 數(shù)據(jù)模型
                articles: [{
                        "title": "堪稱神器的3款在線工具,你一定用得上!",
                        "url": "http://www.itdecent.cn/p/e83e7999346b",
                        "image": "https://upload-images.jianshu.io/upload_images/11438996-56b25f32c9307b4b?imageMogr2/auto-orient/strip%7CimageView2/2/w/640/format/webp",
                    },
                    {
                        "title": "經(jīng)典面試題:從 URL 輸入到頁面展現(xiàn)到底發(fā)生什么?",
                        "url": "http://www.itdecent.cn/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應(yīng)用商店",
                        "url": "http://www.itdecent.cn/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,絕對的良心實(shí)用,趕緊告訴家人",
                        "url": "http://www.itdecent.cn/p/2aec84d269fe",
                        "image": "https://upload-images.jianshu.io/upload_images/16042993-168b2cb17fd7ec0c?imageMogr2/auto-orient/strip%7CimageView2/2/w/640/format/webp",
                    },
                    {
                        "title": "堅(jiān)持學(xué)英語的方法有哪些",
                        "url": "http://www.itdecent.cn/p/0a6a61b0933c",
                        "image": "https://upload-images.jianshu.io/upload_images/3525704-c7293758fc59e56b.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/960/format/webp",
                    }
                ]
            },
            computed: {
                // 計(jì)算函數(shù),匹配搜索
                filteredArticles: function() {
                    var articles_array = this.articles,
                        searchString = this.searchString;
                    //搜索關(guān)鍵詞為空,則返回原始數(shù)據(jù)集
                    if (!searchString) {
                        return articles_array;
                    }
                    //搜索關(guān)鍵詞去除無用空格,轉(zhuǎn)換為小寫
                    searchString = searchString.trim().toLowerCase();
                    //過濾數(shù)組中每個(gè)元素,如果
                    articles_array = articles_array.filter(function(item) {
                        if (item.title.toLowerCase().indexOf(searchString) !== -1) {
                            return item;
                        }
                    })
                    // 返回轉(zhuǎn)化后的數(shù)組
                    return articles_array;;
                }
            }
        })
    </script
</body>

</html>

2.2頁面效果

image.png

2.3重復(fù)1中步驟,即可實(shí)現(xiàn)在手機(jī)端顯示運(yùn)行

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML標(biāo)準(zhǔn)。 注意:講述HT...
    kismetajun閱讀 28,824評論 1 45
  • 前端開發(fā)面試題 <a name='preface'>前言</a> 只看問題點(diǎn)這里 看全部問題和答案點(diǎn)這里 本文由我...
    自you是敏感詞閱讀 904評論 0 3
  • 問答題47 /72 常見瀏覽器兼容性問題與解決方案? 參考答案 (1)瀏覽器兼容問題一:不同瀏覽器的標(biāo)簽?zāi)J(rèn)的外補(bǔ)...
    _Yfling閱讀 14,162評論 1 92
  • 前端開發(fā)知識點(diǎn) HTML&CSS對Web標(biāo)準(zhǔn)的理解、瀏覽器內(nèi)核差異、兼容性、hack、CSS基本功:布局、盒子模型...
    Hebborn_hb閱讀 896評論 0 1
  • 查詢?nèi)孔侄?,按某個(gè)字段排序 查詢指定字段,按某個(gè)字段排序
    Jlan閱讀 576評論 0 0

友情鏈接更多精彩內(nèi)容