vue系列_自定义组件Modal(模态窗口)

父组件的代码(打开模态窗口的代码):

<template>
  <div class="hello">
    <button @click="toggleModal">打开Modal对话框</button>
    <Modal v-show="showModal" v-on:closeme="closeme"></Modal>
  </div>
</template>

<script>
import Modal from './Modal';
export default {
  name: 'HelloWorld',
  data () {
    return {
      showModal:false
    }
  },
  components:{
    Modal
  },
  methods:{
    toggleModal:function(){
      this.showModal = !this.showModal;
    },
    closeme:function(){
      this.showModal = !this.showModal;
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1, h2 {
  font-weight: normal;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
</style>

 

模态窗口组件的代码:

<template>
  <div class="modal-backdrop">
     <div class="modal" :style="mainStyles">
        <div class="modal-header">
          <h3>我是一个Modal的标题</h3>
        </div>
        <div class="modal-body">
            <p>我是一个Modal的内容</p>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn-close" @click="closeSelf">关闭</button>
            <button type="button" class="btn-confirm" @click="confirm">确认</button>
        </div>
    </div>

  </div>
</template>

<style>
.modal-backdrop { 
    position: fixed; 
    top: 0; 
    right: 0; 
    bottom: 0; 
    left: 0; 
    background-color: rgba(0,0,0,.3); 
    display: flex; 
    justify-content: center; 
    align-items: center; 
}
.modal { 
    background-color: #fff; 
    box-shadow: 2px 2px 20px 1px; 
    overflow-x:auto; 
    display: flex; 
    flex-direction: column;
    border-radius: 16px;
    width: 700px;
} 
.modal-header { 
    border-bottom: 1px solid #eee; 
    color: #313131; 
    justify-content: space-between;
    padding: 15px; 
    display: flex; 
} 
.modal-footer { 
    border-top: 1px solid #eee; 
    justify-content: flex-end;
    padding: 15px; 
    display: flex; 
} 
.modal-body { 
    position: relative; 
    padding: 20px 10px; 
}
.btn-close, .btn-confirm {    
    border-radius: 8px;
    margin-left:16px;
    width:56px;
    height: 36px;
    border:none;
    cursor: pointer;
}
.btn-close {
    color: #313131;
    background-color:transparent;
}
.btn-confirm {
    color: #fff; 
    background-color: #2d8cf0;
}


</style>

<script>
export default {
  name: 'Modal',
  props: {  

  },
  data() {
    return {       
    }
  }, 
  methods: {    
    closeSelf() {
        this.$emit("closeme");      
    }
  }
}
</script>

 

Vue.js 2中,自定义组件是一种非常重要的概念,它允许开发者将页面拆分为可重用的组件,以提高代码的可维护性和复用性。下面是关于Vue.js 2中自定义组件的介绍: 1. 创建组件: 在Vue.js中,可以使用Vue.component()方法来创建一个全局的自定义组件,或者在组件选项中使用components属性来创建局部的自定义组件。 2. 组件的模板: 组件的模板可以使用HTML标签和Vue.js的模板语法来定义。可以在模板中使用Vue.js的指令、表达式、事件处理等功能。 3. 组件的数据: 每个组件都有自己的数据对象,可以在组件选项中使用data属性来定义组件的数据。组件的数据是独立的,不会相互影响。 4. 组件的通信: 在Vue.js中,组件之间可以通过props属性和自定义事件进行通信。通过props属性,父组件可以向子组件传递数据;通过自定义事件,子组件可以向父组件发送消息。 5. 组件的生命周期: 组件在创建、更新和销毁过程中会触发一系列的生命周期钩子函数,可以在这些钩子函数中执行相应的操作。常用的生命周期钩子函数包括created、mounted、updated和destroyed等。 6. 组件的样式: 组件的样式可以使用普通的CSS样式表,也可以使用Vue.js的scoped样式,以确保组件的样式只作用于当前组件。 7. 组件的插槽: 插槽是一种特殊的组件内容分发机制,允许父组件向子组件传递DOM结构。通过插槽,可以实现更灵活的组件复用。 以上是关于Vue.js2中自定义组件的简要介绍,希望对有所帮助。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值