Skip to content

FaButton 按钮

封装 Element Plus Button,并加入团队统一的自动 Loading、页面遮罩和 doLoading 实例方法。

按钮类型、自动 Loading 与 Expose

查看代码
Vue
<template>
	<div class="demo-stack">
		<div class="demo-row">
			<FaButton type="primary" @click="handleAsyncClick">自动 Loading</FaButton>
			<FaButton ref="buttonRef" type="success" disabled-loading @click="handleExposeLoading">Expose Loading</FaButton>
			<FaButton type="warning" plain disabled-loading>禁用自动 Loading</FaButton>
			<FaButton type="danger" disabled>禁用状态</FaButton>
		</div>
		<div class="demo-row">
			<FaButton size="small" round disabled-loading>小型圆角</FaButton>
			<FaButton type="primary" dashed disabled-loading>虚线按钮</FaButton>
			<FaButton type="primary" link disabled-loading>链接按钮</FaButton>
			<FaButton type="primary" circle icon="el-icon-Search" disabled-loading />
		</div>
	</div>
</template>

<script setup lang="ts">
import { useTemplateRef } from "vue";
import { ElMessage } from "element-plus";

const buttonRef = useTemplateRef<{ doLoading: (callback: () => void | Promise<void>) => Promise<void> }>("buttonRef");

const handleAsyncClick = (_event: MouseEvent, done?: () => void): void => {
	window.setTimeout(() => {
		ElMessage.success("异步操作完成");
		done?.();
	}, 700);
};

const handleExposeLoading = (): void => {
	buttonRef.value?.doLoading(async () => {
		await new Promise<void>((resolve) => {
			window.setTimeout(resolve, 800);
		});
		ElMessage.success("Expose Loading 完成");
	});
};
</script>

FaButton 完整 API

Props 属性 (20)

Fast 与 Element Plus 2.14.6 的属性已合并展示;Fast 修改项优先排列,未透传的原生属性保留用于兼容性核对。

属性来源说明类型默认值
loadingFast 修改加载状态。BooleanFastfalseELfalse
loadingIconFast 修改加载状态显示的图标组件。String / Object / FunctionFastEL
disabledLoadingFast 新增点击时不启用 Fast 自动 Loading。Booleanfalse
sizeEL 原生组件尺寸。String
disabledEL 原生是否禁用组件或当前选项。Booleanfalse
typeEL 原生按钮视觉类型。String""
iconEL 原生图标组件或图标名称。String / Object / Function
nativeTypeEL 原生按钮原生 type 属性。Stringbutton
plainEL 原生是否显示为朴素按钮。Booleanfalse
textEL 原生是否显示为文字按钮。Booleanfalse
linkEL 原生将表格单元格显示为链接。Booleanfalse
bgEL 原生文字按钮是否显示背景。Booleanfalse
autofocusEL 原生是否自动获取焦点。Booleanfalse
roundEL 原生是否显示为圆角按钮。Booleanfalse
circleEL 原生是否显示为圆形按钮。Booleanfalse
dashedEL 原生是否显示为虚线按钮。Booleanfalse
colorEL 原生按钮自定义颜色。String
darkEL 原生自定义颜色是否使用暗色模式。Booleanfalse
autoInsertSpaceEL 原生是否在两个中文字符之间自动插入空格。Booleanfalse
tagEL 原生是否将单元格显示为标签。String / Objectbutton
Events 事件(1)
名称来源说明参数 / 类型
clickEL 原生点击时触发。
Slots 插槽(3)
名称来源说明参数 / 类型
defaultEL 原生组件默认内容插槽。
loadingEL 原生自定义加载状态内容。
iconEL 原生自定义按钮或组件图标。
Expose 暴露(7)
名称来源说明参数 / 类型
refEL 原生底层 Element Plus 组件引用。
sizeEL 原生按钮解析后的实际尺寸。
typeEL 原生按钮解析后的实际类型。
disabledEL 原生按钮当前是否处于禁用状态。
shouldAddSpaceEL 原生按钮是否会在两个中文字符之间自动插入空格。
loadingFast 新增Fast 业务加载状态。
doLoading(function)Fast 新增在统一 Loading 和遮罩状态下执行同步或异步函数。(function)

FaButton 实例方法

doLoading 加载

按钮加载

签名

ts
doLoading(loadingFunction: () => void | Promise<void>): Promise<void>;

示例

vue
<template>
	<FaButton ref="componentRef" />
</template>

<script setup lang="ts">
import { ref } from "vue";
import { FaButton } from "fast-element-plus";

const componentRef = ref<InstanceType<typeof FaButton>>();
const saveData = async () => Promise.resolve();
const result = await componentRef.value?.doLoading(async () => saveData());
</script>

输入

输入值输入值类型必填/默认值输入值说明
loadingFunction() => void | Promise<void>需要在加载状态中执行的同步或异步任务。

返回

返回值返回值类型返回值说明
resultPromise<void>异步完成后的结果;Promise 拒绝时由调用方处理。

状态与生命周期

doLoading 的并发调用和 click 的 done 分别持有 Loading;重复或迟到的 done 只能释放自己的任务。外部 loading 和手动写入状态独立,不关闭仍在执行的任务。卸载释放本实例遮罩,之后不启动新任务。