Vue實(shí)現(xiàn)PopupWindow組件詳解
這段時(shí)間一直在學(xué)習(xí)前端技術(shù)來(lái)完成自己的小項(xiàng)目。在js方面就使用了Vue框架。由于在項(xiàng)目里想實(shí)現(xiàn)一個(gè)新建地址的PopupWindow效果,便想到可以使用Vue的一些特性來(lái)實(shí)現(xiàn)。
用到的Vue特性:組件(Component),props傳值,slot內(nèi)容插入,transitions過(guò)渡動(dòng)畫(huà),x-templete模板。
直接上代碼(完整代碼可在鏈接中下載popupwindow):
html代碼(無(wú)樣式):
<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)系電話(huà)</p> <input type="text" placeholder="手機(jī)號(hào)"/> </div> <div> <p>詳細(xì)地址</p> <input type="text" placeholder="如街道、樓層、門(mén)牌號(hào)等"/> </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實(shí)例 * */ var chooseAddress = new Vue({ el: "#address-choose", data: { isShowEditWindow: true, isOneButton: false, editTitle: "新建收貨地址", //填入初始地址信息,組件與改數(shù)據(jù)綁定 addressRegion: { } }, methods: { showOneBtnWindow: function(){ //顯示新建收貨地址對(duì)話(huà)框(有一個(gè)按鈕) this.isShowEditWindow = true; this.isOneButton = false; this.editTitle = "新建收貨地址"; }, removeEditWindow: function(){ //關(guān)閉新建與編輯地址選擇對(duì)話(huà)框 this.isShowEditWindow = false; } } })
至此,一個(gè)popupwindow的組件就完成了。在實(shí)現(xiàn)一個(gè)Vue組件時(shí),可以使用模板來(lái)實(shí)現(xiàn)組件,我這里采用了x-templete模板實(shí)現(xiàn)了組件,同時(shí)在組件通也可以使用vue的transition特性加入一些動(dòng)畫(huà)效果。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue-cli自定義創(chuàng)建項(xiàng)目eslint依賴(lài)沖突解決方式
vue-cli是vue.js的腳手架,用于自動(dòng)生成vue.js+webpack的項(xiàng)目模板,在創(chuàng)建項(xiàng)目時(shí),如果遇到npm安裝報(bào)錯(cuò),通常是由于依賴(lài)版本沖突造成的,文中通過(guò)圖文以及代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-09-09vue?如何刪除數(shù)組中的某一條數(shù)據(jù)
這篇文章主要介紹了vue?如何刪除數(shù)組中的某一條數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08Vue?Echarts實(shí)現(xiàn)多功能圖表繪制的示例詳解
作為前端人員,日常圖表、報(bào)表、地圖的接觸可謂相當(dāng)頻繁,今天小編隆重退出前端框架之VUE結(jié)合百度echart實(shí)現(xiàn)中國(guó)地圖+各種圖表的展示與使用;作為“你值得擁有”專(zhuān)欄階段性末篇,值得一看2023-02-02element表格行列的動(dòng)態(tài)合并示例詳解
這篇文章主要為大家介紹了element表格行列的動(dòng)態(tài)合并示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07vue項(xiàng)目中使用websocket的實(shí)現(xiàn)
本文主要介紹了vue項(xiàng)目中使用websocket的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01SpringBoot結(jié)合Vue3實(shí)現(xiàn)簡(jiǎn)單的前后端交互
本文主要介紹了SpringBoot結(jié)合Vue3實(shí)現(xiàn)簡(jiǎn)單的前后端交互,結(jié)合實(shí)際案例,說(shuō)明了如何實(shí)現(xiàn)前后端數(shù)據(jù)的交互,具有一定的?參考價(jià)值,感興趣的可以了解一下2023-08-08Vue3項(xiàng)目中使用自適應(yīng)Rem示例
這篇文章主要為大家介紹了Vue3項(xiàng)目中使用自適應(yīng)Rem示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08