Home  >  Article  >  Web Front-end  >  How to use Vue to implement the PopupWindow component

How to use Vue to implement the PopupWindow component

php中世界最好的语言
php中世界最好的语言Original
2018-06-01 14:54:052026browse

This time I will show you how to use Vue to implement the PopupWindow component. What are the precautions for using Vue to implement the PopupWindow component. The following is a practical case, let's take a look.

I have been learning front-end technology during this period to complete my own small projects. For js, the Vue framework is used. Since I wanted to achieve the PopupWindow effect of a new address in the project, I thought that I could use some features of Vue to achieve it.

Vue features used: Component, propsValue passing, slot content insertion, transitions transition animation, x-templete template.

Directly upload the code (the complete code can be downloaded in the link popupwindow):

html code (no style):

<p id="address-choose">
 <p>
 <button @click="showOneBtnWindow()">显示</button>
  </p>
  <new-address-window 
   v-show="isShowEditWindow" 
   @close="removeEditWindow()" 
   :addressregion="addressRegion">
   <!--使用插槽显示不同的title-->
   <p slot="edit-window-title">
   {{editTitle}}
   </p>
   <p slot="popup-btn-container">
   <button>保存</button> 
   <button>删除</button>
  </p>
  </new-address-window>
 </p>
<!--新建地址popupwindow模板-->
<script type="text/x-template" id="popup-window-address-new">
 <transition name="popup-window-transition">
 <p>
  <slot name="edit-window-title">
  <p>新建收货地址</p>
  </slot> 
 </p>
 <p>
 <p>收货人</p>
 <input type="text" :value="addressregion.name"/>
 </p>
 <p>
 <p>选择地区</p>
 <ul>
  <li>{{addressregion.province}}</li>
  <li>{{addressregion.city}}</li>
  <li>{{addressregion.region}}</li>
 </ul>
 </p>
 <p>
 <p>联系电话</p>
 <input type="text" placeholder="手机号"/>
 </p>
 <p>
  <p>详细地址</p>
  <input type="text" placeholder="如街道、楼层、门牌号等"/>
 </p>
 <p>
 <p>邮政编码</p>
 <input type="text" placeholder="邮政编码(选填)"/>
 </p>
 <p>
 <slot name="popup-btn-container">
  <button class="btn btn-success">保存</button>
  <button class="btn btn-danger">删除</button>
 </slot>
 </p>
 </p>
 </transition>
</script>

js code:

/*
 * 新建与编辑地址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: "新建收货地址",
 //填入初始地址信息,组件与改数据绑定
 addressRegion: {
 }
 },
 methods: {
 showOneBtnWindow: function(){ //显示新建收货地址对话框(有一个按钮)
  this.isShowEditWindow = true;
  this.isOneButton = false;
  this.editTitle = "新建收货地址";
 },
 removeEditWindow: function(){ //关闭新建与编辑地址选择对话框
  this.isShowEditWindow = false;
 }
 }
})

At this point, a popupwindow component is completed. When implementing a Vue component, you can use a template to implement the component. I used the x-templete template to implement the component. At the same time, you can also use the transition feature of vue to add some animation effects in the component pass.

# I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to operate jQuery to achieve the electronic clock effect

How to use Vue nextTick

The above is the detailed content of How to use Vue to implement the PopupWindow component. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn