- 设置标题
props
- 动态插入内容
默认插槽
- 动态插入底部操作按钮
具名插槽
- 打开关闭功能额
组件通信
基础页面布局
<template>
<div class="xtx-dialog" :class="{fade}">
<div class="wrapper" :class="{fade}">
<div class="header">
<h3>切换收货地址</h3>
<a href="JavaScript:;" class="iconfont icon-close-new"></a>
</div>
<div class="body">
对话框内容
</div>
<div class="footer">
<XtxButton type="gray" style="margin-right:20px">取消</XtxButton>
<XtxButton type="primary">确认</XtxButton>
</div>
</div>
</div>
</template>
<script>
import { ref, onMounted } from 'vue'
export default {
name: 'XtxDialog',
setup () {
const fade = ref(false)
onMounted(() => {
// 结构和样式同时加上无过度效果,需要些延时。
setTimeout(() => {
fade.value = true
}, 0)
})
return { fade }
}
}
</script>
<style scoped lang="less">
.xtx-dialog {
position: fixed;
left: 0;
top: 0;
width: 100%;
height: 100%;
z-index: 8887;
background: rgba(0,0,0,0);
&.fade {
transition: all 0.4s;
background: rgba(0,0,0,.5);
}
.wrapper {
width: 600px;
background: #fff;
border-radius: 4px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-60%);
opacity: 0;
&.fade {
transition: all 0.4s;
transform: translate(-50%,-50%);
opacity: 1;
}
.body {
padding: 20px 40px;
font-size: 16px;
.icon-warning {
color: @priceColor;
margin-right: 3px;
font-size: 16px;
}
}
.footer {
text-align: center;
padding: 10px 0 30px 0;
}
.header {
position: relative;
height: 70px;
line-height: 70px;
padding: 0 20px;
border-bottom: 1px solid #f5f5f5;
h3 {
font-weight: normal;
font-size: 18px;
}
a {
position: absolute;
right: 25px;
top: 25px;
font-size: 24px;
width: 20px;
height: 20px;
line-height: 20px;
text-align: center;
color: #999;
&:hover {
color: #666;
}
}
}
}
}
</style>
显示隐藏
props抛出一个visible用来控制弹层组件的渲染
<div class="xtx-dialog" :class="{fade}" v-if="visible">
props: {
visible: {
type: Boolean,
default: false
}
},
标题
层级问题解决
css的层级在创建下就定义好了,组件内部无论在设置多少都无法突破层级限制 传送门组件
<Teleprot to="#id名"></Teleprot>
在public下index.html加对应id的盒子用来存放就能突破层级限制