Vue實現(xiàn)PopupWindow組件詳解
這段時間一直在學習前端技術來完成自己的小項目。在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(){ //關閉新建與編輯地址選擇對話框
this.isShowEditWindow = false;
}
}
})
至此,一個popupwindow的組件就完成了。在實現(xiàn)一個Vue組件時,可以使用模板來實現(xiàn)組件,我這里采用了x-templete模板實現(xiàn)了組件,同時在組件通也可以使用vue的transition特性加入一些動畫效果。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
vue項目中請求數(shù)據(jù)特別多導致頁面卡死的解決
這篇文章主要介紹了vue項目中請求數(shù)據(jù)特別多導致頁面卡死的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09
vue項目使用jszip和file-saver批量打包壓縮圖片或附件方式
這篇文章主要介紹了vue項目使用jszip和file-saver批量打包壓縮圖片或附件方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03
解決VUE打包后與nginx代理出現(xiàn)加載速度超級慢的問題
這篇文章主要介紹了解決VUE打包后與nginx代理出現(xiàn)加載速度超級慢的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09
VUE動態(tài)綁定class類的三種常用方式及適用場景詳解
文章介紹了在實際開發(fā)中動態(tài)綁定class的三種常見情況及其解決方案,包括根據(jù)不同的返回值渲染不同的class樣式、給模塊添加基礎樣式以及根據(jù)設計確定是否使用多個樣式2025-01-01

