Skip to content

ProgressNotification

悬浮出现在页面角落,可以用于显示某些场景的进度通知,如下载、上传、导入和导出等,并且支持重试或取消操作。

基础用法

ProgressNotification 提供了三种状态:progresssuccesserror。可以通过 status 字段来控制状态。当 statuserror 时,会显示底部操作按钮,可以重试或取消操作。当 statussuccess 时,经过 duration 字段设置的时间后会自动关闭,默认为 3000 毫秒。

查看源代码
vue
<template>
    <boat-button @click="openSuccess">Success</boat-button>
    <boat-button @click="openError">Error</boat-button>
    <boat-progress-notification
        v-model="visible"
        :status="status"
        title="上传"
        message="test.json"
        :errorTip="errorTip"
    ></boat-progress-notification>
</template>

<script setup lang="ts">
import { ref } from 'vue';

const status = ref<'error' | 'success' | ''>('');
const visible = ref(false);
const errorTip = ref('');

const openSuccess = () => {
    visible.value = true;
    // 使用 setTimeout 模仿接口请求
    setTimeout(() => {
        status.value = 'success';
    }, 4000);
};

const openError = () => {
    visible.value = true;
    // 使用 setTimeout 模仿接口请求
    setTimeout(() => {
        status.value = 'error';
        errorTip.value = '接口错误'
    }, 4000);
};
</script>

进阶用法(推荐)

ProgressNotification 提供一个 action 属性,它接收一个 Promise 函数。当使用 action 时,不需要传递 statuserrorTip 属性,此时 statusaction 函数是否执行成功决定,errorTipaction 函数抛出的错误决定。

Action Success

action 执行成功后,经过 duration 设置的时间后会自动关闭。

查看源代码
vue
<template>
    <boat-button @click="actionSuccessVisible = true">Success</boat-button>

    <boat-progress-notification
        v-model="actionSuccessVisible"
        title="上传"
        message="test.json"
        :action="actionSuccess"
    ></boat-progress-notification>
</template>

<script setup lang="ts">
import { ref } from 'vue';

const actionSuccessVisible = ref(false);

const actionSuccess = async () => {
    try {
        const response = await fetch('https://jsonplaceholder.typicode.com/posts/1', {
            method: 'GET',
            headers: {
                'Content-Type': 'application/json',
            },
        });
        if (!response.ok) {
            // 确保错误被抛出
            throw new Error('Network response was not ok');
        }
        const data = await response.json();
    } catch (error) {
        throw error; // 确保错误被抛出
    }
};
</script>

Action Error

action 执行失败时,会显示底部操作按钮。

查看源代码
vue
<template>
    <boat-button @click="actionErrorVisible = true">Error</boat-button>

    <boat-progress-notification
        v-model="actionErrorVisible"
        title="上传"
        message="test.json"
        :action="actionFailure"
    ></boat-progress-notification>
</template>

<script setup lang="ts">
import { ref } from 'vue';

const actionErrorVisible = ref(false);

const actionFailure = async () => {
    try {
        const response = await fetch('https://jsonplaceholder.typicode.com/invalid-url', {
            method: 'GET',
            headers: {
                'Content-Type': 'application/json',
            },
        });
        if (!response.ok) {
            // 确保错误被抛出
            throw new Error('Network response was not ok');
        }
        const data = await response.json();
    } catch (error) {
        throw error; // 确保错误被抛出
    }
};
</script>

错误时重试

statuserror,或者 action 函数抛出错误时,都会显示底部操作按钮。操作按钮的文本和状态可以通过相关属性控制。

使用 status 时的重试操作

如果使用 status,需要处理 retry 事件,并且需要控制 statuserrorTip 的值。

查看源代码
vue
<template>
    <boat-button @click="openRetryError">Retry Status</boat-button>
    <boat-progress-notification
        v-model="retryVisible1"
        :status="status"
        title="上传"
        message="test.json"
        :errorTip="errorTip"
        @retry="handleRetry"
    />
</template>

<script setup lang="ts">
import { ref } from 'vue';

const retryVisible1 = ref(false);
const status = ref<'error' | 'success' | ''>('');
const errorTip = ref('');

const openRetryError = () => {
    retryVisible1.value = true;
    // 使用 setTimeout 模仿接口请求
    setTimeout(() => {
        status.value = 'error';
        errorTip.value = '接口错误'
    }, 4000);
};
const handleRetry = () => {
    setTimeout(() => {
        status.value = 'success';
    }, 5000);
};
</script>

使用 action 时的重试操作

如果使用 action,则不需要处理 retry 事件,也不需要控制 statuserrorTip 的值。只需要在 action 函数中抛出错误即可。点击重试按钮时,内部会再次执行 action 函数。

查看源代码
vue
<template>
    <boat-button @click="retryVisible2 = true">Retry Action</boat-button>
    <boat-progress-notification
        v-model="retryVisible2"
        title="上传"
        message="test.json"
        :action="actionFailure"
    />
</template>

<script setup lang="ts">
import { ref } from 'vue';

const retryVisible2 = ref(false);

const actionFailure = async () => {
    try {
        const response = await fetch('https://jsonplaceholder.typicode.com/invalid-url', {
            method: 'GET',
            headers: {
                'Content-Type': 'application/json',
            },
        });
        if (!response.ok) {
            // 确保错误被抛出
            throw new Error('Network response was not ok');
        }
        const data = await response.json();
    } catch (error) {
        throw error; // 确保错误被抛出
    }
};
</script>

API

Props

属性说明类型可选值默认值
v-model是否显示boolean-false
title标题string-''
message消息内容string-''
customClass自定义类名string-''
status状态stringsuccess / error''
errorTipstatuserror 时显示错误信息string-''
durationstatussuccess 时自动关闭时间, 单位为毫秒。值为 0 则不会自动关闭number-3000
showClose是否显示关闭按钮boolean-true
zIndex设置通知的 z-indexnumber-9999
footerLeftText底部左侧按钮文本string-'Cancel'
footerRightText底部右侧按钮文本string-'Retry'
footerLeftDisabled底部左侧按钮是否禁用boolean-false
footerRightDisabled底部右侧按钮是否禁用boolean-false
action执行函数Function(() => Promise<any>)-null
appendTo挂载到哪个 DOM 元素,默认为 body 元素。在挂载前请确保该元素已经存在string / HTMLElement-'body'
position自定义弹出位置stringtop-right / top-left / bottom-right / bottom-lefttop-right
offset相对屏幕顶部/底部的距离number-16

Events

事件名说明类型参数
close关闭 / 取消事件Function() => void
retry重试事件Function() => void