模态框 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 | 提示框宽度,单位px | number | 420 |
cancelText | 取消按钮文字 | string | '取消' |
okText | 确认按钮文字 | string | '确定' |
noticeText | 通知按钮文字 | string | '知道了' |
center | 水平垂直居中:true,固定高度水平居中:false | boolean | true |
top | 固定高度水平居中时,距顶部高度,单位px | number | 100 |
loading | 加载中 | boolean | false |
visible | 是否可见 | boolean | false |
Events
事件名称 | 说明 | 参数 |
---|---|---|
cancel | 点击遮罩层或取消按钮的回调 | () => void |
ok | 点击确定回调 | () => void |
know | 点击知道了的回调 | () => void |