For AI agents: the complete documentation index is available at https://docs.halo.run/llms.txt, the full documentation bundle is available at https://docs.halo.run/llms-full.txt, and this page is available as Markdown at https://docs.halo.run/developer-guide/plugin/api-reference/ui/base-components/button.md.

按钮

按钮用于触发一个操作,例如提交表单、打开弹窗或跳转页面。

引入

import { VButton } from "@halo-dev/components";

示例

基础用法

<script lang="ts" setup>
import { VButton } from "@halo-dev/components";
</script>

<template>
  <VButton>按钮</VButton>
</template>

不同类型

<script lang="ts" setup>
import { VButton, VSpace } from "@halo-dev/components";
</script>

<template>
  <VSpace>
    <VButton type="default">默认按钮</VButton>
    <VButton type="primary">主要按钮</VButton>
    <VButton type="secondary">次要按钮</VButton>
    <VButton type="danger">危险按钮</VButton>
  </VSpace>
</template>

不同尺寸

<script lang="ts" setup>
import { VButton, VSpace } from "@halo-dev/components";
</script>

<template>
  <VSpace>
    <VButton type="primary" size="lg">大号按钮</VButton>
    <VButton type="primary" size="md">中号按钮</VButton>
    <VButton type="primary" size="sm">小号按钮</VButton>
    <VButton type="primary" size="xs">超小按钮</VButton>
  </VSpace>
</template>

加载状态

<script lang="ts" setup>
import { ref } from "vue";
import { VButton } from "@halo-dev/components";

const loading = ref(false);

function handleClick() {
  loading.value = true;
  window.setTimeout(() => {
    loading.value = false;
  }, 1500);
}
</script>

<template>
  <VButton type="primary" :loading="loading" @click="handleClick">
    {{ loading ? "处理中" : "点击加载" }}
  </VButton>
</template>

禁用状态

<script lang="ts" setup>
import { VButton, VSpace } from "@halo-dev/components";
</script>

<template>
  <VSpace>
    <VButton disabled>禁用按钮</VButton>
    <VButton type="primary" disabled>禁用主要按钮</VButton>
    <VButton type="danger" disabled>禁用危险按钮</VButton>
  </VSpace>
</template>

图标按钮

<script lang="ts" setup>
import {
  IconAddCircle,
  IconDeleteBin,
  IconSearch,
  VButton,
  VSpace,
} from "@halo-dev/components";
</script>

<template>
  <VSpace>
    <VButton type="primary">
      <template #icon>
        <IconAddCircle />
      </template>
      新建
    </VButton>
    <VButton circle aria-label="搜索">
      <template #icon>
        <IconSearch />
      </template>
    </VButton>
    <VButton type="danger" circle aria-label="删除">
      <template #icon>
        <IconDeleteBin />
      </template>
    </VButton>
  </VSpace>
</template>

块级按钮

<script lang="ts" setup>
import { VButton } from "@halo-dev/components";
</script>

<template>
  <VButton type="primary" block>块级按钮</VButton>
</template>

Props

属性类型默认值说明
type"default" | "primary" | "secondary" | "danger""default"按钮类型
size"lg" | "md" | "sm" | "xs""md"按钮尺寸
circlebooleanfalse是否为圆形图标按钮
blockbooleanfalse是否占满父容器宽度
disabledbooleanfalse是否禁用
loadingbooleanfalse是否显示加载状态,加载时不可点击
routeRouteLocationRawundefined点击后跳转的路由,使用 Halo Console 的 vue-router 实例
ghostbooleanfalse是否为幽灵按钮

Emits

事件参数说明
click-点击按钮时触发,disabled 或 loading 时不会触发

Slots

插槽说明
default按钮内容
icon按钮图标,显示在内容左侧