vue3【组件封装】消息提示 Toast

src/components/Toast.vue

html 复制代码
<template>
  <transition name="fade">
    <div class="fixed w-full h-full left-0 top-0 flex z-100 transition-all" v-if="show">
      <div
        :style="{ background: bgColor, color: textColor }"
        :class="['m-auto   px-4 py-2 rounded-2']"
      >
        {{ text }}
      </div>
    </div>
  </transition>
</template>

<script setup lang="ts">
interface ToastType {
  type: string
  text: string
  time?: number
}

const props = defineProps<ToastType>()

const type = props.type
const bgColor = ref('#edf2fc')
const textColor = ref('#aca9b7')

if (type === 'success') {
  bgColor.value = '#f0f9eb'
  textColor.value = '#9bcb76'
} else if (type === 'error') {
  bgColor.value = '#fef0f0'
  textColor.value = '#f56c93'
} else if (type === 'warning') {
  bgColor.value = '#fdf6ec'
  textColor.value = '#e6a23c'
}

const show = defineModel({ default: false })

watch(show, () => {
  if (show.value) {
    setTimeout(() => {
      show.value = false
    }, props.time || 2000)
  }
})
</script>

使用

html 复制代码
  <button @click="show_msg = true">保存</button>

  <teleport to="body">
    <Toast v-model="show_msg" text="操作成功!" type="success"> </Toast>
  </teleport>
ts 复制代码
const show_msg = ref(false)
相关推荐
冥界摄政王3 天前
Cesium学习第二章 camera 相机
node.js·html·vue3·js·cesium
Irene19914 天前
在 Vue3 中使用 Element Plus
vue3·element plus
冥界摄政王4 天前
Cesium学习第一章 安装下载 基于vue3引入Cesium项目开发
vue·vue3·html5·webgl·cesium
小安同学iter4 天前
Vue3 进阶核心:高级响应式工具 + 特殊内置组件核心解析
前端·javascript·vue.js·vue3·api
启扶农4 天前
lecen:一个更好的开源可视化系统搭建项目--页面设计器(表单设计器)--全低代码|所见即所得|利用可视化设计器构建你的应用系统-做一个懂你的人
低代码·vue3·拖拽·表单设计器·所见即所得·页面可视化·页面设计器
C_心欲无痕5 天前
vue3 - 类与样式的绑定
javascript·vue.js·vue3
C_心欲无痕6 天前
vue3 - defineExpose暴露给父组件属性和方法
前端·javascript·vue.js·vue3
Sheldon一蓑烟雨任平生7 天前
Vue3 低代码平台项目实战(上)
低代码·typescript·vue3·低代码平台·问卷调查·json schema
Sheldon一蓑烟雨任平生7 天前
Vue3 低代码平台项目实战(下)
低代码·typescript·vue3·低代码平台·json schema·vue3项目
C_心欲无痕9 天前
vue3 - useId生成唯一标识符
前端·javascript·vue.js·vue3