<script setup> const props = defineProps({ tips: String }) </script> <template> <div class="tips" v-if="flag">hi, there.</div> </template> <style scoped> .tips { position: fixed; top: 20px; right: 20px; color: #fff; padding: 4px 20px; font-size: 12px; color: #fff; border-radius: 8px; background-color: rgba(0, 0, 0, 0.8); z-index: 99; } </style>
import Tips from './components/Tips.vue';
. 使用响应式数据 isOpenModal 控制其显示和隐藏:真,则显示,否则关闭
let isOpenModal = ref(false)
. 使用侦听
watch(isModalOpen, (newval, oldval) => { if (newval) { setTimeout(() => { isModalOpen.value = false }, 3000); } })
. 使用延时
const OpenModal = () => { isOpenModal.value = true setTimeout(() => { isOpenModal.value = false; }, 3000); }
. 使用鼠标单击事件 - 略
. 结构使用
<template> <Tips :flag="isOpenModal"></Tips> <button @click="OpenModal">click to open modal</button> </template>
const props = defineProps({ tips: String, flag: Boolean })
<div class="tips" v-if="flag">{{ tips }}</div>
let tips = ref('') const OpenModal = () => { showTips('msg here') } const showTips = (msg) => { isOpenModal.value = true; tips.value = msg; setTimeout(() => { isOpenModal.value = false; }, 3000); }
<Tips :flag="isOpenModal" :tips="tips"></Tips>
过渡、延时等既可以在父组件中使用过,也可以在模态框组件使用;在哪里使用要结合每个组件的具体情况;如果其中一个组件也用到了类似的设计,那么就应该在该组件中使用,避免重复设计
<transition name="modal-fade"> // </transition>
.modal-fade-enter-active, .modal-fade-leave-active { transition: opacity 0.3s ease; } .modal-fade-enter-from, .modal-fade-leave-to { opacity: 0; }