Vue實現(xiàn)PopupWindow組件詳解
這段時間一直在學(xué)習(xí)前端技術(shù)來完成自己的小項目。在js方面就使用了Vue框架。由于在項目里想實現(xiàn)一個新建地址的PopupWindow效果,便想到可以使用Vue的一些特性來實現(xiàn)。
用到的Vue特性:組件(Component),props傳值,slot內(nèi)容插入,transitions過渡動畫,x-templete模板。
直接上代碼(完整代碼可在鏈接中下載popupwindow):
html代碼(無樣式):
<div id="address-choose"> <div> <button @click="showOneBtnWindow()">顯示</button> </div> <new-address-window v-show="isShowEditWindow" @close="removeEditWindow()" :addressregion="addressRegion"> <!--使用插槽顯示不同的title--> <p slot="edit-window-title"> {{editTitle}} </p> <div slot="popup-btn-container"> <button>保存</button> <button>刪除</button> </div> </new-address-window> </div> <!--新建地址popupwindow模板--> <script type="text/x-template" id="popup-window-address-new"> <transition name="popup-window-transition"> <div> <slot name="edit-window-title"> <p>新建收貨地址</p> </slot> </div> <div> <p>收貨人</p> <input type="text" :value="addressregion.name"/> </div> <div> <p>選擇地區(qū)</p> <ul> <li>{{addressregion.province}}</li> <li>{{addressregion.city}}</li> <li>{{addressregion.region}}</li> </ul> </div> <div> <p>聯(lián)系電話</p> <input type="text" placeholder="手機號"/> </div> <div> <p>詳細地址</p> <input type="text" placeholder="如街道、樓層、門牌號等"/> </div> <div> <p>郵政編碼</p> <input type="text" placeholder="郵政編碼(選填)"/> </div> <div> <slot name="popup-btn-container"> <button class="btn btn-success">保存</button> <button class="btn btn-danger">刪除</button> </slot> </div> </div> </transition> </script>
js代碼:
/* * 新建與編輯地址Vue組件popupwindow * */ var newAddressWindow = Vue.component("new-address-window",{ props: ['addressregion'], template: "#popup-window-address-new" }) /* * 地址popupwindow的Vue實例 * */ var chooseAddress = new Vue({ el: "#address-choose", data: { isShowEditWindow: true, isOneButton: false, editTitle: "新建收貨地址", //填入初始地址信息,組件與改數(shù)據(jù)綁定 addressRegion: { } }, methods: { showOneBtnWindow: function(){ //顯示新建收貨地址對話框(有一個按鈕) this.isShowEditWindow = true; this.isOneButton = false; this.editTitle = "新建收貨地址"; }, removeEditWindow: function(){ //關(guān)閉新建與編輯地址選擇對話框 this.isShowEditWindow = false; } } })
至此,一個popupwindow的組件就完成了。在實現(xiàn)一個Vue組件時,可以使用模板來實現(xiàn)組件,我這里采用了x-templete模板實現(xiàn)了組件,同時在組件通也可以使用vue的transition特性加入一些動畫效果。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue-cli自定義創(chuàng)建項目eslint依賴沖突解決方式
vue-cli是vue.js的腳手架,用于自動生成vue.js+webpack的項目模板,在創(chuàng)建項目時,如果遇到npm安裝報錯,通常是由于依賴版本沖突造成的,文中通過圖文以及代碼介紹的非常詳細,需要的朋友可以參考下2024-09-09vue?如何刪除數(shù)組中的某一條數(shù)據(jù)
這篇文章主要介紹了vue?如何刪除數(shù)組中的某一條數(shù)據(jù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08Vue?Echarts實現(xiàn)多功能圖表繪制的示例詳解
作為前端人員,日常圖表、報表、地圖的接觸可謂相當(dāng)頻繁,今天小編隆重退出前端框架之VUE結(jié)合百度echart實現(xiàn)中國地圖+各種圖表的展示與使用;作為“你值得擁有”專欄階段性末篇,值得一看2023-02-02SpringBoot結(jié)合Vue3實現(xiàn)簡單的前后端交互
本文主要介紹了SpringBoot結(jié)合Vue3實現(xiàn)簡單的前后端交互,結(jié)合實際案例,說明了如何實現(xiàn)前后端數(shù)據(jù)的交互,具有一定的?參考價值,感興趣的可以了解一下2023-08-08