vue2購物車ch1-(安裝依賴、簡單配置、 axios擷取api的類比資料)

來源:互聯網
上載者:User

標籤:教程   vertica   res   err   箭頭   2.4   --   依賴   nod   

0--項目說明

說明此項目源自某課網購物車教程,但是在開發過程中,發現在開發過程中用的還是 vue-resource(宣布不更新的類$.ajx()外掛程式),為了以後的發展使用axios.js,詳情參考

1 初始靜態頁面

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <!-- Always force latest IE rendering engine or request Chrome Frame -->    <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">    <title>購物車</title>    <link href="css/base2.css" rel="stylesheet" >    <link href="css/checkout.css" rel="stylesheet" >    <link href="css/modal.css" rel="stylesheet" >    <style>        .quentity input{            width: 40px;            padding: 5px 10px;            text-align: center;        }        .partContent{            width:100px;            height: 50px;        }        .partContent .left{            width:50px;            height:50px;            display: table-cell;            vertical-align: middle;            text-align: center;            float:left;        }        .partContent .right{            width:50px;            height: 50px;            float:left;            cursor:pointer;        }    </style></head>    <body class="checkout">        <div id="app">            <svg style="position: absolute; width: 0; height: 0; overflow: hidden;" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">                <defs>                    <symbol id="icon-add" viewBox="0 0 32 32">                        <title>add2</title>                        <path class="path1" d="M15 17h-13.664c-0.554 0-1.002-0.446-1.002-1 0-0.552 0.452-1 1.002-1h13.664v-13.664c0-0.554 0.446-1.002 1-1.002 0.552 0 1 0.452 1 1.002v13.664h13.664c0.554 0 1.002 0.446 1.002 1 0 0.552-0.452 1-1.002 1h-13.664v13.664c0 0.554-0.446 1.002-1 1.002-0.552 0-1-0.452-1-1.002v-13.664z"></path>                    </symbol>                    <symbol id="icon-ok" viewBox="0 0 39 32">                        <title>ok</title>                        <path class="path1" d="M14.084 20.656l-7.845-9.282c-1.288-1.482-3.534-1.639-5.016-0.351s-1.639 3.534-0.351 5.016l10.697 12.306c1.451 1.669 4.057 1.623 5.448-0.096l18.168-22.456c1.235-1.527 0.999-3.765-0.528-5.001s-3.765-0.999-5.001 0.528l-15.573 19.337z"></path>                    </symbol>                    <symbol id="icon-edit" viewBox="0 0 32 32">                        <title>edit</title>                        <path class="path1" d="M25.599 11.292l-4.892-4.892 3.825-3.825 4.892 4.892-3.825 3.825zM4.732 23.308l3.959 3.959-5.939 1.98 1.98-5.939zM10.666 26.225l-4.892-4.892 13.425-13.425 4.892 4.892-13.425 13.425zM31.687 6.713l-6.4-6.4c-0.417-0.417-1.091-0.417-1.508 0l-20.267 20.267c-0.114 0.115-0.191 0.25-0.242 0.393-0.003 0.009-0.012 0.015-0.015 0.025l-3.2 9.6c-0.128 0.383-0.029 0.806 0.257 1.091 0.203 0.204 0.476 0.313 0.754 0.313 0.112 0 0.227-0.017 0.337-0.054l9.6-3.2c0.011-0.003 0.017-0.013 0.027-0.016 0.142-0.052 0.276-0.128 0.39-0.242l20.267-20.267c0.417-0.416 0.417-1.091 0-1.508v0z"></path>                    </symbol>                    <symbol id="icon-del" viewBox="0 0 26 32">                        <title>delete</title>                        <path class="path1" d="M17.723 28c0.543 0 0.984-0.448 0.984-1v-12c0-0.552-0.441-1-0.984-1s-0.985 0.448-0.985 1v12c0 0.552 0.441 1 0.985 1v0zM7.877 28c0.543 0 0.984-0.448 0.984-1v-12c0-0.552-0.441-1-0.984-1s-0.985 0.448-0.985 1v12c0 0.552 0.441 1 0.985 1v0zM12.8 28c0.543 0 0.985-0.448 0.985-1v-12c0-0.552-0.441-1-0.985-1s-0.984 0.448-0.984 1v12c0 0.552 0.441 1 0.984 1v0zM23.631 4h-5.908v-2c0-1.104-0.882-2-1.969-2h-5.908c-1.087 0-1.969 0.896-1.969 2v2h-5.908c-1.087 0-1.969 0.896-1.969 2v2c0 1.104 0.882 2 1.969 2v18c0 2.208 1.765 4 3.939 4h13.784c2.174 0 3.938-1.792 3.938-4v-18c1.087 0 1.969-0.896 1.969-2v-2c0-1.104-0.882-2-1.969-2v0zM9.846 3c0-0.552 0.441-1 0.984-1h3.938c0.544 0 0.985 0.448 0.985 1v1h-5.908v-1zM21.662 28c0 1.104-0.882 2-1.969 2h-13.784c-1.087 0-1.97-0.896-1.97-2v-18h17.723v18zM22.646 8h-19.692c-0.543 0-0.985-0.448-0.985-1s0.441-1 0.985-1h19.692c0.543 0 0.984 0.448 0.984 1s-0.441 1-0.984 1v0z"></path>                    </symbol>                    <symbol id="icon-clock" viewBox="0 0 32 32">                        <title>clock</title>                        <path class="path1" d="M29.333 16c0-7.364-5.97-13.333-13.333-13.333s-13.333 5.97-13.333 13.333c0 7.364 5.97 13.333 13.333 13.333s13.333-5.97 13.333-13.333v0 0 0 0 0 0zM0 16c0-8.837 7.163-16 16-16s16 7.163 16 16c0 8.837-7.163 16-16 16s-16-7.163-16-16zM14.667 14.667v1.333h2.667v-10.667h-2.667v9.333zM24 18.667h1.333v-2.667h-10.667v2.667h9.333z"></path>                    </symbol>                </defs>            </svg>            <div class="container">                <div class="cart">                    <div class="checkout-title">                        <span>購物車</span>                    </div>                    <!-- table -->                    <div class="item-list-wrap">                        <div class="cart-item">                            <div class="cart-item-head">                                <ul>                                    <li>商品資訊</li>                                    <li>商品金額</li>                                    <li>商品數量</li>                                    <li>總金額</li>                                    <li>編輯</li>                                </ul>                            </div>                            <!--購物車商品列表 start-->                            <ul class="cart-item-list">                                <li>                                    <div class="cart-tab-1">                                        <div class="cart-item-check">                                            <a href="javascript:void 0" class="item-check-btn">                                            <svg class="icon icon-ok"><use xlink:href="#icon-ok"></use></svg>                                        </a>                                    </div>                                    <div class="cart-item-pic">                                        <img src="img/goods-1.jpg">                                    </div>                                    <div class="cart-item-title">                                        <div class="item-name">黃鶴樓香煙</div>                                    </div>                                    <div class="item-include">                                        <dl>                                            <dt>贈送:</dt>                                            <dd>                                                <div class="partContent">                                                    <div class="left">                                                        <span>打火機</span>                                                    </div>                                                    <div class="right">                                                        <img style="width:50px"/>                                                    </div>                                                </div>                                            </dd>                                        </dl>                                    </div>                                </div>                                <div class="cart-tab-2">                                    <div class="item-price">19</div>                                </div>                                <div class="cart-tab-3">                                    <div class="item-quantity">                                        <div class="select-self select-self-open">                                            <div class="quentity">                                                <a href="javascript:void 0">-</a>                                                <input type="text" disabled value="1">                                                <a href="javascript:void 0">+</a>                                            </div>                                        </div>                                        <div class="item-stock">有貨</div>                                    </div>                                </div>                                <div class="cart-tab-4">                                    <div class="item-price-total">0.00</div>                                </div>                                <div class="cart-tab-5">                                    <div class="cart-item-opration">                                        <a href="javascript:void 0" class="item-edit-btn">                                        <svg class="icon icon-del"><use xlink:href="#icon-del"></use></svg>                                    </a>                                </div>                            </div>                        </li>                            </ul>                            <!--購物車商品列表 end-->                        </div>                    </div>            <!-- footer -->            <div class="cart-foot-wrap">                <div class="cart-foot-l">                    <div class="item-all-check">                        <a href="javascript:void 0">                            <span class="item-check-btn">                                <svg class="icon icon-ok"><use xlink:href="#icon-ok"></use></svg>                            </span>                            <span>全選</span>                        </a>                    </div>                    <div class="item-all-del">                        <a href="javascript:void 0" class="item-del-btn">                            取消全選                        </a>                    </div>                </div>                <div class="cart-foot-r">                    <div class="item-total">                        總量: <span class="total-price"></span>                    </div>                    <div class="next-btn-wrap">                        <a href="address.html" class="btn btn--red">結賬</a>                    </div>                </div>            </div>        </div>    </div>    <div class="md-modal modal-msg md-modal-transition" id="showModal">    <div class="md-modal-inner">        <div class="md-top">            <button class="md-close">關閉</button>        </div>        <div class="md-content">            <div class="confirm-tips">                <p id="cusLanInfo">你確認刪除此訂單資訊嗎?</p>            </div>            <div class="btn-wrap col-2">                <button class="btn btn--m" id="btnModalConfirm">確定</button>                <button class="btn btn--m btn--red" id="btnModalCancel">取消</button>            </div>        </div>    </div></div><!--<div class="md-overlay" id="showOverLay"></div>-->    </div>        <script type="text/javascript" src="node_modules/vue/dist/vue.js"></script>        <script type="text/javascript" src="node_modules/axios/dist/axios.js"></script>        <script type="text/javascript" src="js/cart.js"></script>    </body></html>

2 cart.js

/** * Created by Administrator on 2017/4/2 0002. */let vmCart = new Vue({    el: ‘#app‘,    data: {      productList: [] //作為商品列表    },    filter: {  //局部過濾器    },    mounted: function(){ //dom載入完後(生命週期)執行個體建立後查詢某個方法--進來之後查詢商品        this.cartView(); //2使用    },    methods: {        cartView: function(){ //1定義            const _this = this; //這裡的this指向vmCart            axios.get(‘data/cartData.json‘) //使用axios            .then(function(res){  //then回呼函數            // 使用箭頭函數的話,this的範圍並不會變,還是指向vmCart            //     (res)=>{            //     if(res.status == 200){            //         this.totalMoney = res.data.result.totalMoney;            //         this.list = res.data.result.list;            //     }            // }            // 不使用箭頭函數的場合            // 如果要使用vmCart執行個體,            // 就必須在mounted鉤子函數裡再調用一次$nextTick方法才能保證el被render在dom中                //res 資料庫返回的對象                //res.data.result.list 返回的資料數組                //console.log(res);                res.status == 200 && (_this.productList = res.data.result.list ); //狀態為200時執行後面語句            })            .catch(function(err){                console.log(‘get data error...‘); //從資料庫擷取資料出現問題            })        },    }});

3 測試是否擷取資料 

擷取成功

3.1 接下來故意將axios.then引入.catch回調中看是否有效 

說明有效

 

vue2購物車ch1-(安裝依賴、簡單配置、 axios擷取api的類比資料)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.