1. 程式人生 > 實用技巧 >元件化開發案例

元件化開發案例

程式碼在最下面

購物車案例:

總覽:my-cart裡有三個子元件以及資料。

中間元件的邏輯: 1頁面上的內容是通過父元件傳遞值來顯示,而不是通過雙向資料繫結。

2對資料進行修改也全都是通過子傳父的方式改變父元件中的資料(資料一直在父元件中) 當多個按鈕要對data中的同一個資料進行修改時可定義同一個事件,給同一個事件新增不同型別

3父元件接收(type別忘記加引號)

形式就是:

子元件模板: @click='add(item.id)' 子元件方法: add: function(id) { this.$emit('事件名'), {id: id , type: '....'}} 父元件接收: <cart-list @事件名='函式名($event)'></cart-list> 父元件方法: 函式名: function(val) { val.id , val.type }
<!
DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"> .container { } .container .cart { width: 300px; margin: auto; } .container .title { background-color: lightblue;
height: 40px; line-height: 40px; text-align: center; /*color: #fff;*/ } .container .total { background-color: #FFCE46; height: 50px; line-height: 50px; text-align: right; } .container .total button { margin: 0 10px; background-color
: #DC4C40; height: 35px; width: 80px; border: 0; } .container .total span { color: red; font-weight: bold; } .container .item { height: 55px; line-height: 55px; position: relative; border-top: 1px solid #ADD8E6; } .container .item img { width: 45px; height: 45px; margin: 5px; } .container .item .name { position: absolute; width: 90px; top: 0;left: 55px; font-size: 16px; } .container .item .change { width: 100px; position: absolute; top: 0; right: 50px; } .container .item .change a { font-size: 20px; width: 30px; text-decoration:none; background-color: lightgray; vertical-align: middle; } .container .item .change .num { width: 40px; height: 25px; } .container .item .del { position: absolute; top: 0; right: 0px; width: 40px; text-align: center; font-size: 40px; cursor: pointer; color: red; } .container .item .del:hover { background-color: orange; } </style> </head> <body> <div id="app"> <div class="container"> <my-cart></my-cart> </div> </div> <script src="vue.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript"> var CartTitle = { props: ['name'], template: ` <div class="title">{{name}}的商品</div> ` } var CartList = { props: ['list'], template: ` <div> <div class="item" v-for='(item,index) in list' :key='item.id'> <div class="name">{{item.name}}</div> <div class="change"> <a href="" @click.prevent='jian(item.id)'></a> <input type="text" class="num" :value='item.num' @blur='changeNum(item.id,$event)'/> <a href="" @click.prevent='add(item.id)'></a> </div> <div class="del" @click='del(item.id)'>×</div> </div> </div> `, methods: { del: function(id){ // 吧id傳給父元件 this.$emit('cart-del',id) }, changeNum(id,event){ this.$emit('change-num',{ id: id, num: event.target.value, type: 'change' }) }, jian: function(id) { this.$emit('change-num',{ id: id, type: 'jian' }) }, add: function(id) { this.$emit('change-num',{ id: id, type: 'add' }) } } } var CartTotal = { props: ['list'], template: ` <div class="total"> <span>總價:{{total}}</span> <button>結算</button> </div> `, computed: { total: function() { var t = 0; this.list.forEach(item => t += item.price*item.num); return t } } } // 購物車整個屬於全域性元件,但是裡面三個元件要定義為購物車的區域性元件 Vue.component('my-cart',{ data: function() { return { uname: '戰鬥', list: [{ id: 1, name: 'TCL彩電', price: 1000, num: 1, img: 'img/a.jpg' },{ id: 2, name: '機頂盒', price: 2000, num: 1, img: 'img/b.jpg' },{ id: 3, name: '海爾冰箱', price: 2000, num: 1, img: 'img/c.jpg' },{ id: 4, name: '小米手機', price: 5000, num: 1, img: 'img/d.jpg' },{ id: 5, name: 'PPTV電視', price: 1000, num: 2, img: 'img/e.jpg' }] } }, template: ` <div class='cart'> <cart-title :name='uname'></cart-title> <cart-list :list='list' @cart-del='delCart($event)' @change-num='changeNum($event)'></cart-list> <cart-total :list='list'></cart-total> </div> `, // 區域性元件 components: { 'cart-title': CartTitle, 'cart-list': CartList, 'cart-total': CartTotal }, methods: { delCart: function(id) { var index = this.list.findIndex(item => item.id === id) this.list.splice(index,1) }, changeNum(val) { if(val.type === 'change'){ this.list.some(item => { if(item.id === val.id){ item.num = val.num } }) } else if(val.type === 'jian'){ this.list.some(item => { if(item.id === val.id){ item.num -=1 } }) } else if(val.type === 'add'){ this.list.some(item => { if(item.id === val.id){ item.num += 1 } }) } } } }); var vm = new Vue({ el: '#app', data: { } }); </script> </body> </html>