Skip to content

Toast 吐司提示

方法

事件名称说明参数
open打开function(option: Options)

Options 有效值

参数说明类型可选值默认值
icon图标类名string
image图片objectnull
image.url图片地址string
image.mode图片裁剪模式stringaspectFit
image.style图片样式object
message提示消息string
duration保留时长number2000
type类型stringprimary / success / error / warning / info
position悬浮位置stringtop / middle / bottombottom
single是否单个显示booleanfalse

示例

cl-page 中使用

html
<template>
  <cl-page>
    <cl-button @tap="open"></cl-button>
  </cl-page>
</template>

<script setup lang="ts">
  import { useUi } from "/$/cool-ui";
  const ui = useUi();

  function open() {
    ui.showToast("Hello");
  }
</script>

使用 ref 获取组件实例并添加 ClToast.Ref 类型提示

html
<template>
  <view class="demo">
    <cl-toast ref="Toast"></cl-toast>
    <cl-button @tap="open">提示</cl-button>
  </view>
</template>

<script lang="ts" setup>
  import { ref } from "vue";
  const Toast = ref<ClToast.Ref>();

  function open() {
    Toast.value?.open({
      message: "Toast 提示",
    });
  }
</script>

不同类型

配置 type 参数:

html
<template>
  <view class="demo">
    <cl-toast ref="Toast"></cl-toast>
    <cl-button @tap="open">提示</cl-button>
  </view>
</template>

<script lang="ts" setup>
  import { ref } from "vue";
  const Toast = ref<ClToast.Ref>();

  function open() {
    Toast.value?.open({
      message: "Toast 提示",
      type: "primary", // warning error info success
    });
  }
</script>

不同位置

配置 position 参数:

html
<template>
  <view class="demo">
    <cl-toast ref="Toast"></cl-toast>
    <cl-button @tap="open">提示</cl-button>
  </view>
</template>

<script lang="ts" setup>
  import { ref } from "vue";
  const Toast = ref<ClToast.Ref>();

  function open() {
    Toast.value?.open({
      message: "Toast 提示",
      position: "top", // top middle bottom
    });
  }
</script>

添加图片、图标

html
<template>
  <view class="demo">
    <cl-toast ref="Toast"></cl-toast>
    <cl-button @tap="open">图标</cl-button>
    <cl-button @tap="open2">图片</cl-button>
  </view>
</template>

<script lang="ts" setup>
  import { ref } from "vue";
  const Toast = ref<ClToast.Ref>();

  function open() {
    Toast.value?.open({
      message: "Toast 提示",
      icon: "cl-icon-good-fill",
    });
  }

  function open2() {
    Toast.value?.open({
      message: "Toast 提示",
      image: {
        url: "../static/avatar.png",
        style: {
          height: "120rpx",
          width: "120rpx",
        },
      },
    });
  }
</script>