Skip to content
On this page

Dialog 对话框

TIP

点击按钮,打开对应对话框组件。

● 类型

通过type属性来设置对话框的类型

展示代码
vue
<template>
    <div>
        <k-space>
            <k-button :onclick = "() => {clickToShow(0)}" type="primary">Open a dialog of type info</k-button>
            <k-button :onclick = "() => {clickToShow(1)}" type="success">Open a dialog of type confirm</k-button>
            <k-button :onclick = "() => {clickToShow(2)}" type="warming">Open a dialog of type warning</k-button>
            <k-button :onclick = "() => {clickToShow(3)}" type="danger">Open a dialog of type error</k-button>
        </k-space>
        
        <k-dialog v-if = "isShow[0]"
        titleText = "Title"
        contentText = "Some contents... Some contents... Some contents... Some contents... Some contents... Some contents... Some contents... Some contents... Some contents..."
        ></k-dialog>
       
        <k-dialog v-if = "isShow[1]"
        titleText = "Title"
        contentText = "Some contents... Some contents... Some contents... Some contents... Some contents... Some contents... Some contents... Some contents... Some contents..."
        type="confirm"
        ></k-dialog>
       
        <k-dialog v-if = "isShow[2]"
        titleText = "Are you sure delete this task?"
        contentText = "Some contents... Some contents... Some contents... Some contents... Some contents... Some contents... Some contents... Some contents... Some contents..."
        type="warning"
        ></k-dialog>
       
        <k-dialog v-if = "isShow[3]"
        titleText = "A serious error has occurred here !"
        contentText = "It needs to be updated online. Please confirm that your network connection is normal and try again."
        type="error"
        ></k-dialog>
    </div>
</template>
<script lang="ts" setup>
    import { ref } from 'vue'
    let isShow: any = ref([false, false, false, false]);
    let clickToShow = (i:number) => {
        isShow.value.forEach((item:boolean, index:number) => isShow.value[index] = false);
        isShow.value[i] = true;
    }
</script>

注意

请勿将<k-dialog>组件放入行内元素中。

● 自定义大小

通过widthheight属性来设置对话框的整体宽度和高度

展示代码
vue
<template>
    <div>
        <k-space>
            <k-button :onclick = "() => {clickToShow(0)}" type="primary">600×200</k-button>
            <k-button :onclick = "() => {clickToShow(1)}" type="primary">400×170</k-button>
        </k-space>
        
        <k-dialog v-if = "isShow[0]"
        titleText = "对话框标题"
        contentText = "提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容"
        width="600px"
        height="200px"
        ></k-dialog>

        <k-dialog v-if = "isShow[1]"
        titleText = "对话框标题"
        contentText = "提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容"
        width="400px"
        height="170px"
        ></k-dialog>
    </div>
</template>
<script lang="ts" setup>
    import { ref } from 'vue'
    let isShow: any = ref([false, false]);
    let clickToShow = (i:number) => {
        isShow.value.forEach((item:boolean, index:number) => isShow.value[index] = false);
        isShow.value[i] = true;
    }
</script>

● 自定义主题颜色

通过themeColor属性来设置对话框的主题颜色

展示代码
vue
<template>
    <div>
        <k-space>
            <k-button :onclick = "() => {clickToShow(6)}" type="success">themeColor = "#1ABC9C"</k-button>
        </k-space>
    
        <k-dialog v-if = "isShow[0]"
        titleText = "对话框标题"
        contentText = "提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容"
        themeColor="#1ABC9C"
        ></k-dialog>
    </div>
</template>
<script lang="ts" setup>
    import { ref } from 'vue'
    let isShow: any = ref([false]);
    let clickToShow = (i:number) => {
        isShow.value.forEach((item:boolean, index:number) => isShow.value[index] = false);
        isShow.value[i] = true;
    }
</script>

● 位置

通过location属性来设置对话框的位置

展示代码
vue
<template>
    <div>
        <k-space>
            <k-button :onclick = "() => {clickToShow(0)}" type="primary">top</k-button>
            <k-button :onclick = "() => {clickToShow(1)}" type="primary">center</k-button>
        </k-space>

        <k-dialog v-if = "isShow[0]"
            titleText = "对话框标题"
            contentText = "提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容"
            location="top"
        ></k-dialog>

        <k-dialog v-if = "isShow[1]"
            titleText = "对话框标题"
            contentText = "提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容"
            location="center"
        ></k-dialog>
    </div>
</template>
<script lang="ts" setup>
    import { ref } from 'vue'
    let isShow: any = ref([false, false]);
    let clickToShow = (i:number) => {
        isShow.value.forEach((item:boolean, index:number) => isShow.value[index] = false);
        isShow.value[i] = true;
    }
</script>

● 按钮

通过closable属性来设置是否展示对话框的关闭按钮

通过footerBtn属性来设置对话框的底部按钮的展示情况

展示代码
vue
<template>
    <div>
        <k-space>
            <k-button :onclick = "() => {clickToShow(0)}" type="primary">无关闭按钮</k-button>
            <k-button :onclick = "() => {clickToShow(1)}" type="primary">仅含确认按钮</k-button>
            <k-button :onclick = "() => {clickToShow(2)}" type="primary">仅含取消按钮</k-button>
            <k-button :onclick = "() => {clickToShow(3)}" type="primary">不展示确认和取消按钮</k-button>
        </k-space>
        <k-dialog v-if = "isShow[0]"
            titleText = "对话框标题"
            contentText = "提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容"
            :closable = false
        ></k-dialog>
        <k-dialog v-if = "isShow[1]"
            titleText = "对话框标题"
            contentText = "提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容"
            footerBtn="confirm"
        ></k-dialog>
        <k-dialog v-if = "isShow[2]"
            titleText = "对话框标题"
            contentText = "提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容"
            footerBtn="cancel"
        ></k-dialog>
        <k-dialog v-if = "isShow[3]"
            titleText = "对话框标题"
            contentText = "提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容"
            footerBtn="null"
        ></k-dialog> 
    </div>
</template>
<script lang="ts" setup>
    import { ref } from 'vue'
    let isShow: any = ref([false, false, false, false]);
    let clickToShow = (i:number) => {
        isShow.value.forEach((item:boolean, index:number) => isShow.value[index] = false);
        isShow.value[i] = true;
    }
</script>

● 点击事件

通过confirmClickcancelClick属性来注册对话框的确认按钮和取消按钮的点击事件

展示代码
vue
<template>
    <div>
        <k-space>
            <k-button :onclick = "() => {clickToShow(0)}" type="primary">确认按钮点击事件</k-button>
            <k-button :onclick = "() => {clickToShow(1)}" type="primary">取消按钮点击事件</k-button>       
        </k-space>

        <k-dialog v-if = "isShow[0]"
            titleText = "对话框标题"
            contentText = "提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容"
            :confirmClick="confirmClick"
        ></k-dialog>

        <k-dialog v-if = "isShow[1]"
            titleText = "对话框标题"
            contentText = "提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容"
            :cancelClick="cancelClick"
        ></k-dialog>
    </div>
</template>
<script lang="ts" setup>
    import { ref } from 'vue'
    // 是否展示对话框组件
    let isShow: any = ref([false, false]);
    // 点击按钮显示对应对话框
    let clickToShow = (i:number) => {
        isShow.value.forEach((item:boolean, index:number) => isShow.value[index] = false);
        isShow.value[i] = true;
    }

    // 自定义确认按钮点击事件
    let confirmClick = () => {
        window.alert("点击了确认按钮");
    }
    // 自定义取消按钮点击事件
    let cancelClick = () => {
        window.alert("点击了取消按钮");
    }
</script>

● 异步确认

通过confirmLoading属性来设置确认按钮是否为异步按钮

展示代码
vue
<template>
    <div>
        <k-space>
            <k-button :onclick = "() => {clickToShow()}" type="primary">异步确认</k-button>            
        </k-space>
        <k-dialog v-if = "isShow"
            titleText = "对话框标题"
            contentText = "提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容提示内容"
            :confirmLoading = true
            :confirmClick="confirmClick"
        ></k-dialog>
    </div>
</template>
<script lang="ts" setup>
    import { ref } from 'vue'
    // 是否展示对话框组件
    let isShow: any = ref(false);
    // 点击按钮显示对应对话框
    let clickToShow = () => {
        isShow.value = true;
    }
    // 自定义异步确认按钮点击事件
    let confirmClick = () => {
        let p = new Promise<void>((resolve, reject) => {
            setTimeout(()=> {
                resolve();
            }, 1000)
        })
        p.then(()=> {
            isShow.value = false;
        })
    }
</script>

● Attributes 参数

参数说明类型默认值
type提示框类型:info、confirm、warning、errorstring"info"
themeColor主题颜色,包括头部背景颜色、图标颜色、确认按钮颜色string-
titleColor标题文字颜色string"white"
contentColor提示框内容文字颜色string"black"
titleText标题文本string-
contentText提示框内容文本string-
confirmBtnText确认按钮文本string"确认"
cancelBtnText取消按钮文本string"取消"
closable是否显示右上角的关闭按钮booleantrue
footerBtn底部按钮类型:both、confirm、cancel、nullstring"both"
width对话框整体宽度string"500px"
height对话框整体高度string"200px"
location提示框位置:top、centerstring"top"
confirmClick确定按钮点击事件function-
cancelClick取消按钮点击事件function-
confirmLoading异步确认按钮booleanfalse

Released under the MIT License.