Skip to content
On this page

按钮 Button

常用组件,用于响应某些时间或者处理某些逻辑

基本使用

Show Code
vue
<script setup lang="ts">
function onClick(e: Event) {
  console.log('click')
}
</script>
<template>
  <n-button @click="onClick">Default</n-button>
  <n-button effect="reverse" @click="onClick">Reverse</n-button>
  <n-button type="primary" @click="onClick">Primary</n-button>
  <n-button type="danger" @click="onClick">Danger</n-button>
  <n-button disabled @click="onClick">Disabled</n-button>
</template>

大、中、小三种尺寸

Show Code
vue
<script setup lang="ts">
function onClick(e: Event) {
  console.log('click')
}
</script>
<template>
  <n-button size="small" @click="onClick">Small</n-button>
  <n-button @click="onClick">Default</n-button>
  <n-button size="large" @click="onClick">Large</n-button>
</template>

自定义样式

Show Code
vue
<script setup lang="ts">
function onClick(e: Event) {
  console.log('click')
}
</script>
<template>
  <n-button :width="120" :height="40" :border-radius="8" size="large" @click="onClick">
    <p style="font-size: 18px">自定义样式</p>
  </n-button>
</template>

加载中状态

Loading state:

Show Code
vue
<script setup lang="ts">
import { ref } from 'vue'
const loading = ref(true)
function onClick(e: Event) {
  console.log('click')
}
</script>
<template>
  <n-button :loading="loading" @click="onClick">Default</n-button>
  <n-button :loading="loading" type="primary" @click="onClick">Primary</n-button>
  <n-button :loading="loading" type="danger" @click="onClick">Danger</n-button>
  <h3>
    Loading state:
    <n-switch v-model:checked="loading" />
  </h3>
</template>

API

参数说明类型默认值可选值
name默认文本string | slot'按钮'false
type类型'default' | 'primary' | 'danger''default'false
effect悬浮变化效果,只有 type 为 default 时,effect 才生效'fade' | 'reverse'''
size尺寸'small' | 'middle' | 'large''_self'false
width宽度,优先级高于 size 属性,为0时自适应内容的宽度number0false
height高度,优先级高于 size 属性number0false
borderRadius圆角,单位pxnumber5false
route跳转目标URL地址{path?: string, query?: object}{}false
target如何打开目标URL,设置 route 时生效'_self' | '_blank''_self'false
disabled是否禁用booleanfalsefalse
loading是否加载中booleanfalsefalse
center是否将按钮设置为块级元素并居中展示booleanfalsefalse

Events

事件名称说明参数
click点击按钮时的回调,未设置 route 时生效(e: Event) => void

Released under the MIT License.