Skip to content
On this page

模态框 Modal

全局信息提示,一般用于展示一些重要信息或者重要操作。

基本使用

Show Code
vue
<template>
  <n-button type="primary" @click="showEraseModal('Some descriptions ...')">基本使用</n-button>
  <n-modal ref="modal" :loading="loading" :visible="visible" @cancel="onCancel" @ok="onConfirm" />
</template>
<script setup lang="ts">
import { ref } from 'vue'

const modal = ref()
const loading = ref(false)
const visible = ref(false)
const center = ref(true)

function showEraseModal(content: string) {
  modal.value.erase({
    title: 'Do you Want to delete these items ?',
    content: content,
  })
  center.value = true
  visible.value = true
}
function onCancel() {
  // “取消”按钮回调
  visible.value = false
}
function onConfirm() {
  // “确定”按钮回调
  loading.value = true // 开启加载状态
  setTimeout(() => {
    visible.value = false
    loading.value = false
  }, 500)
}
</script>

信息展示

Show Code
vue
<template>
  <n-button type="primary" @click="showInfoModal('Some descriptions ...')">信息展示</n-button>
  <n-modal ref="modal" :visible="visible" @know="onKnow" />
</template>

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

const modal = ref()
const visible = ref(false)
const center = ref(true)
function onKnow() {
  // “我知道了”按钮回调
  visible.value = false
}

function showInfoModal(content: string) {
  modal.value.info({
    title: 'Do you See these items ?',
    content: content,
  })
  center.value = true
  visible.value = true
}
</script>

模态框显示位置

Show Code
vue
<template>
  <n-button type="primary" @click="showFixModal('Some descriptions ...')">position</n-button>
  <n-modal ref="modal" :center="false" :top="120" :visible="visible" @know="onKnow" />
</template>
<script setup lang="ts">
import { ref } from 'vue'

const modal = ref()
const center = ref(true)
const visible = ref(false)
function onKnow() {
  // “我知道了”按钮回调
  visible.value = false
}

function showFixModal(content: string) {
  modal.value.info({
    title: 'Do you See these items ?',
    content: content,
  })
  center.value = false
  visible.value = true
}
</script>

API

参数说明类型默认值
width提示框宽度,单位pxnumber420
cancelText取消按钮文字string'取消'
okText确认按钮文字string'确定'
noticeText通知按钮文字string'知道了'
center水平垂直居中:true,固定高度水平居中:falsebooleantrue
top固定高度水平居中时,距顶部高度,单位pxnumber100
loading加载中booleanfalse
visible是否可见booleanfalse

Events

事件名称说明参数
cancel点击遮罩层或取消按钮的回调() => void
ok点击确定回调() => void
know点击知道了的回调() => void

Released under the MIT License.