警告 Alert
用于页面中展示重要的提示信息,支持多种类型、可关闭、描述、图标。
ℹ️
信息提示
这是一条普通的信息提示。
✅
成功提示
操作已成功完成。
⚠️
警告提示
请注意相关事项。
❌
错误提示
操作失败,请重试。
无图标
组件代码
<template>
<div class="wrjnb-alert" :class="[`wrjnb-alert--${type}`, { 'is-closable': closable }]">
<span v-if="showIcon" class="wrjnb-alert__icon">{{ iconMap[type] }}</span>
<div class="wrjnb-alert__content">
<div v-if="title" class="wrjnb-alert__title">{{ title }}</div>
<div v-if="description || $slots.default" class="wrjnb-alert__desc">
<slot>{{ description }}</slot>
</div>
</div>
<button v-if="closable" class="wrjnb-alert__close" @click="close">×</button>
</div>
</template>
<script setup lang="ts">
import { defineProps, defineEmits, ref } from "vue"
const props = defineProps({
type: { type: String as () => "info" | "success" | "warning" | "error", default: "info" },
title: { type: String, default: "" },
description: { type: String, default: "" },
closable: { type: Boolean, default: false },
showIcon: { type: Boolean, default: true },
})
const emit = defineEmits(["close"])
const visible = ref(true)
const iconMap = {
info: "ℹ️",
success: "✅",
warning: "⚠️",
error: "❌",
}
function close() {
visible.value = false
emit("close")
}
</script>
<style lang="scss" scoped>
.wrjnb-alert {
display: flex;
align-items: flex-start;
padding: 12px 20px;
border-radius: 6px;
font-size: 1em;
background: #f4f4f5;
color: #333;
border: 1px solid #ebeef5;
position: relative;
margin: 8px 0;
transition: all 0.2s;
}
.wrjnb-alert__icon {
font-size: 1.5em;
margin-right: 12px;
line-height: 1.2;
}
.wrjnb-alert__content {
flex: 1;
}
.wrjnb-alert__title {
font-weight: 600;
margin-bottom: 2px;
}
.wrjnb-alert__desc {
font-size: 0.98em;
color: #666;
}
.wrjnb-alert__close {
background: none;
border: none;
font-size: 1.2em;
color: #bbb;
cursor: pointer;
position: absolute;
top: 10px;
right: 14px;
transition: color 0.2s;
}
.wrjnb-alert__close:hover {
color: #888;
}
.wrjnb-alert--success {
background: #f0f9eb;
border-color: #e1f3d8;
color: #52c41a;
}
.wrjnb-alert--info {
background: #f4f4f5;
border-color: #ebeef5;
color: #409eff;
}
.wrjnb-alert--warning {
background: #fffbe6;
border-color: #faecd8;
color: #faad14;
}
.wrjnb-alert--error {
background: #fef0f0;
border-color: #fde2e2;
color: #f56c6c;
}
</style>
属性说明
属性名 | 说明 | 类型 | 默认值 |
---|---|---|---|
title | 标题内容 | String | '' |
type | 警告类型 | 'info' | 'success' | 'warning' | 'error' | 'info' |
description | 辅助描述内容 | String | '' |
closable | 是否可关闭 | Boolean | false |
showIcon | 是否显示图标 | Boolean | true |
事件说明
事件名 | 说明 | 回调参数 |
---|---|---|
close | 点击关闭按钮时触发 | event: MouseEvent |
插槽说明
插槽名 | 说明 |
---|---|
icon | 自定义图标插槽。 |
default | 警告内容插槽。 |