Skip to content

按钮 Button

按钮用于开始一个即时操作。

何时使用

标记了一个(或封装一组)操作命令,响应用户点击行为,触发相应的业务逻辑。

在 Fused Design Vue 中我们提供了五种按钮。

  • 主按钮:用于主行动点,一个操作区域只能有一个主按钮。
  • 默认按钮:用于没有主次之分的一组行动点。
  • 虚线按钮:常用于添加操作。
  • 文本按钮:用于最次级的行动点。
  • 链接按钮:一般用于链接,即导航至某位置。

以及四种状态属性与上面配合使用。

  • 危险:删除/移动/修改权限等危险操作,一般需要二次确认。
  • 幽灵:用于背景色比较复杂的地方,常用在首页/产品页等展示场景。
  • 禁用:行动点不可用的时候,一般需要文案解释。
  • 加载中:用于异步操作等待反馈的时候,也可以避免多次提交。

代码演示

按钮类型

按钮有五种类型:主按钮、次按钮、虚线按钮、文本按钮和链接按钮。主按钮在同一个操作区域最多出现一次。

vue
<template>
  <a-space wrap>
    <a-button type="primary">Primary Button111</a-button>
    <a-button>Default Button</a-button>
    <a-button type="dashed">Dashed Button</a-button>
    <a-button type="text">Text Button</a-button>
    <a-button type="link">Link Button</a-button>
  </a-space>
</template>
<script setup>
</script>

图标按钮

当需要在 Button 内嵌入 Icon 时,可以设置 icon 属性,或者直接在 Button 内使用 Icon 组件。

如果想控制 Icon 具体的位置,只能直接使用 Icon 组件,而非 icon 属性。

vue
<template>
  <a-space direction="vertical">
    <a-space warp>
      <a-tooltip title="search">
        <a-button type="primary" shape="circle" :icon="h(SearchOutlined)" />
      </a-tooltip>
      <a-button type="primary" shape="circle">A</a-button>
      <a-button type="primary" :icon="h(SearchOutlined)">Search</a-button>
      <a-tooltip title="search">
        <a-button shape="circle" :icon="h(SearchOutlined)" />
      </a-tooltip>
      <a-button :icon="h(SearchOutlined)">Search</a-button>
    </a-space>
    <a-space warp>
      <a-tooltip title="search">
        <a-button shape="circle" :icon="h(SearchOutlined)" />
      </a-tooltip>
      <a-button :icon="h(SearchOutlined)">Search</a-button>
      <a-tooltip title="search">
        <a-button type="dashed" shape="circle" :icon="h(SearchOutlined)" />
      </a-tooltip>
      <a-button type="dashed" :icon="h(SearchOutlined)">Search</a-button>
      <a-button :icon="h(SearchOutlined)" href="https://www.google.com" />
    </a-space>
  </a-space>
</template>
<script lang="ts" setup>
import { h } from 'vue'
import { SearchOutlined } from '@ant-design/icons-vue'
</script>

按钮组合

按钮组合使用时,推荐使用 1 个主操作 + n 个次操作,3 个以上操作时把更多操作放到 Dropdown.Button 中组合使用。

vue
<template>
  <a-space>
    <a-button type="primary">Primary</a-button>
    <a-button>secondary</a-button>
    <a-dropdown>
      <template #overlay>
        <a-menu @click="handleMenuClick">
          <a-menu-item key="1">1st item</a-menu-item>
          <a-menu-item key="2">2nd item</a-menu-item>
          <a-menu-item key="3">3rd item</a-menu-item>
        </a-menu>
      </template>
      <a-button>
        Actions
        <DownOutlined />
      </a-button>
    </a-dropdown>
  </a-space>
</template>
<script lang="ts" setup>
import { DownOutlined } from '@ant-design/icons-vue'
import type { MenuProps } from '@design-middle-center/fuse-design-vue'
const handleMenuClick: MenuProps['onClick'] = (e) => {
  console.log('click', e)
}
</script>

块级按钮

block 属性将使按钮适合其父宽度。

vue
<template>
  <a-space wrap>
    <a-button type="primary" block>Primary</a-button>
    <a-button block>Default</a-button>
    <a-button type="dashed" block>Dashed</a-button>
    <a-button danger block>Danger</a-button>
    <a-button type="link" block>Link</a-button>
  </a-space>
</template>

API

通过设置 Button 的属性来产生不同的按钮样式,推荐顺序为: type -> shape -> size -> loading -> disabled

按钮的属性说明如下:

属性说明类型默认值版本
block将按钮宽度调整为其父宽度的选项booleanfalse
danger设置危险按钮booleanfalse2.2.0
disabled按钮失效状态booleanfalse
ghost幽灵属性,使按钮背景透明booleanfalse
href点击跳转的地址,指定此属性 button 的行为和 a 链接一致string-
htmlType设置 button 原生的 type 值,可选值请参考 HTML 标准stringbutton
icon设置按钮的图标类型v-slot-
loading设置按钮载入状态boolean |false
shape设置按钮形状default | circle | rounddefault
size设置按钮大小large | middle | smallmiddle
target相当于 a 链接的 target 属性,href 存在时生效string-
type设置按钮类型primary | ghost | dashed | link | text | defaultdefault

事件

事件名称说明回调参数版本
click点击按钮时的回调(event) => void

支持原生 button 的其他所有属性。

方法

Button

名称描述版本
blur()移除焦点
focus()获取焦点

FAQ

如何移除 2 个汉字之间的空格

根据 Ant Design 设计规范要求,我们会在按钮内(文本按钮和链接按钮除外)只有两个汉字时自动添加空格,如果你不需要这个特性,可以设置 ConfigProvider 的 autoInsertSpaceInButtonfalse