Skip to content

Alert 警告提示

介绍

警告提示,展现需要关注的信息。

导入

js
import Alert from '@/components/feedback/Alert.vue'

用法

基本用法

展示不同类型的警告提示。

vue
<template>
  <FlexCol :gap="20">
    <Alert type="primary" description="主要警告信息" />
    <Alert type="success" description="成功提示信息" />
    <Alert type="warning" description="警告提示信息" />
    <Alert type="danger" description="危险提示信息" />
    <Alert type="error" description="错误提示信息" />
    <Alert type="info" description="提示信息" />
    <Alert type="default" description="默认提示信息" />
  </FlexCol>
</template>

带标题的警告提示

同时显示标题和描述信息。

vue
<template>
  <FlexCol :gap="20">
    <Alert type="primary" message="主要提示" description="这是一条带有标题的主要警告信息" />
    <Alert type="success" message="成功提示" description="这是一条带有标题的成功提示信息" />
    <Alert type="warning" message="警告提示" description="这是一条带有标题的警告提示信息" />
    <Alert type="danger" message="危险提示" description="这是一条带有标题的危险提示信息" />
  </FlexCol>
</template>

可关闭的警告提示

通过closeable属性设置警告提示可以关闭。

vue
<template>
  <Alert v-if="show" type="warning" message="警告提示" description="这是一条可关闭的警告信息" closeable @close="show = false" />
</template>

自定义图标

通过icon属性设置自定义图标。

vue
<template>
  <FlexCol :gap="20">
    <Alert type="primary" description="自定义图标的警告信息" icon="discount" />
    <Alert type="info" message="自定义图标" description="这是一条带有自定义图标的警告信息" icon="favorite" />
  </FlexCol>
</template>

自定义颜色

通过backgroundColor、borderColor和color属性自定义警告提示的颜色。

vue
<template>
  <FlexCol :gap="20">
    <Alert 
      description="自定义背景颜色和文字颜色"
      backgroundColor="#ff9900"
      borderColor="#ff0000"
      color="#f0f9ff"
    />
    <Alert 
      type="success"
      message="自定义主题" 
      description="自定义背景颜色和文字颜色"
      color="#006600"
      borderColor="#00cc00"
    />
  </FlexCol>
</template>

使用自定义内容插槽

通过插槽自定义警告提示的内容。

vue
<template>
  <Alert type="warning">
    <template #icon>
      <Icon icon="lock" color="#ff6600" fontSize="24" />
    </template>
    <view style="display: flex; flex-direction: column; gap: 8rpx;">
      <text style="font-size: 36rpx; font-weight: bold; color: #ff6600;">自定义标题</text>
      <text style="font-size: 28rpx; color: #ff9900;">这是自定义的内容文本,可以包含更多样式</text>
      <view style="margin-top: 12rpx; padding: 12rpx; background: #fff3e0; border-radius: 8rpx;">
        <text style="font-size: 26rpx; color: #e65100;">额外信息提示区域</text>
      </view>
    </view>
  </Alert>
</template>

限制内容显示行数

通过lines属性限制内容显示的行数,超出部分会自动截断并显示省略号。

vue
<template>
  <Alert 
    type="info" 
    message="多行文本限制"
    description="这是一段比较长的文本内容,通过设置lines属性可以限制显示的行数,超出部分会自动截断并显示省略号。这是一段比较长的文本内容,通过设置lines属性可以限制显示的行数。"
    :lines="2"
  />
</template>

自定义关闭按钮

通过close插槽自定义关闭按钮的样式。

vue
<template>
  <Alert v-if="show2" type="primary" message="自定义关闭按钮" description="点击自定义关闭按钮可以关闭提示" closeable>
    <template #close>
      <Icon icon="delete-filling" color="#999" :fontSize="24" @click="show2 = false" />
    </template>
  </Alert>
</template>

API 参考

Alert

警告提示组件。

Props

名称说明类型必填默认值
type提示类型,可选值为 primary, success, warning, danger, error, info, defaultAlertType-'default'
message提示标题string--
description提示内容string--
icon自定义图标string--
color文字和图标的颜色string--
backgroundColor背景颜色string--
borderColor边框颜色string--
closeable是否显示关闭按钮boolean-false
closeIcon关闭图标string-'close'
closeIconColor关闭图标颜色string--
lines内容最多显示的行数,0表示不限制number-0

Slots

名称说明
default自定义内容区域
icon自定义图标
message自定义提示标题
description自定义提示内容
close自定义关闭按钮

Events

名称说明参数
close关闭事件$event
click点击事件$event

主题变量

名称类型默认值说明
AlertBorderRadiusnumber16警告框圆角
AlertBorderWidthstring'1px'警告框边框宽度
AlertPaddingnumber16警告框内边距
AlertIconSizenumber20图标大小
AlertIconMarginRightnumber12图标右侧边距
AlertIconMarginTopnumber2图标上边距
AlertTitleFontSizestring'fontSize.medium'标题字体大小
AlertTitleFontWeightstring'bold'标题字重
AlertMessageFontSizestring'fontSize.small'内容字体大小
AlertMessageMarginTopnumber8内容与标题间距
AlertCloseButtonSizenumber42关闭按钮大小
AlertCloseButtonMarginLeftnumber12关闭按钮左侧边距