生成一个vue对话框
时间: 2023-07-04 13:06:59 浏览: 45
好的,以下是一个简单的 Vue 对话框组件示例:
```
<template>
<div class="dialog">
<div class="header">{{ title }}</div>
<div class="content">{{ message }}</div>
<div class="footer">
<button @click="confirm">确认</button>
<button @click="cancel">取消</button>
</div>
</div>
</template>
<script>
export default {
name: 'Dialog',
props: {
title: String,
message: String
},
methods: {
confirm() {
// 确认操作
this.$emit('confirm');
},
cancel() {
// 取消操作
this.$emit('cancel');
}
}
}
</script>
<style scoped>
.dialog {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 300px;
background-color: #fff;
border-radius: 5px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
padding: 20px;
}
.header {
font-size: 20px;
font-weight: bold;
margin-bottom: 10px;
}
.footer {
margin-top: 20px;
text-align: right;
}
button {
margin-left: 10px;
padding: 5px 10px;
border-radius: 3px;
border: none;
background-color: #2196f3;
color: #fff;
cursor: pointer;
}
button:hover {
background-color: #1976d2;
}
</style>
```
你可以在父组件中使用该对话框组件,并通过 props 传入标题和内容,同时监听 confirm 和 cancel 事件来处理用户的操作。
```
<template>
<div>
<button @click="showDialog">打开对话框</button>
<Dialog v-if="dialogVisible" title="提示" message="确定要执行该操作吗?" @confirm="handleConfirm" @cancel="handleCancel" />
</div>
</template>
<script>
import Dialog from './Dialog.vue';
export default {
components: {
Dialog
},
data() {
return {
dialogVisible: false
}
},
methods: {
showDialog() {
this.dialogVisible = true;
},
handleConfirm() {
// 确认操作
this.dialogVisible = false;
},
handleCancel() {
// 取消操作
this.dialogVisible = false;
}
}
}
</script>
```