最近在使用vue3 + element-plus封装自定义弹窗遇到了一系列问题,在此记录
vue2实现
<template>
<el-dialog
width="800px"
title="弹窗示例"
destroy-on-close
append-to-body
:visible.sync="show"
:before-close="cancel">
<div slot="footer" class="dialog-footer">
<el-button @click="cancel">{{ $t('关闭') }}</el-button>
</div>
</el-dialog>
</template>
<script>
export default {
props: {
/**
* 控制显示隐藏
*/
show: {
type: Boolean,
required: true,
default: false,
},
},
data() {
return {
}
},
computed: {},
watch: {
show: function (newValue) {
if (newValue) {
}
},
},
methods: {
cancel() {
this.$emit('update:show', false)
},
},
}
</script>
页面使用
<template>
<div>
<MyDialog :show.sync="myDialog.show" />
<button @click="handleMyDialog">打开弹窗</button>
</div
</template>
<script>
import MyDialog from '@/components/MyDialog/index.vue'
export default {
components: { MyDialog },
data() {
return {
myDialog: {
show: false,
},
}
},
methods: {
handleMyDialog() {
this.myDialog.show = true
}
}
}
</script>
这样可以正常使用
vue3 + element-plus
<template>
<el-dialog :modelValue="show" title="Tips" width="900" :before-close="cancel">
<template #footer>
<div class="dialog-footer">
<el-button @click="cancel">关闭</el-button>
</div>
</template>
</el-dialog>
</template>
<script lang="ts" setup>
type IProps = {
show: boolean
code: string
}
defineProps<IProps>()
const emit = defineEmits(['update:show'])
const cancel = () =>emit('update:show', false)
</script>
<style lang="scss" scoped></style>
页面使用(
正确版本)
<template>
<MyDialog v-model:show="sourceCode.show" />
<button @click="myDialog.show=true">打开弹窗</button>
</template>
<script>
import MyDialog from '@/components/MyDialog/index.vue'
</script>
<script lang="ts" setup>
import MyDialog from '@/components/MyDialog/index.vue'
const myDialog = reactive({
show: false,
})
</script>
遇到的问题
-
dialog绑定对应的值不同
- vue2 element-ui ==> value
是否显示 Dialog,支持 .sync 修饰符 - element-plus ==> model-value / v-model
是否显示 Dialog
- vue2 element-ui ==> value
-
.sync修饰符- vue2
支持 - vue3
不支持改成 v-model:prop="xxx"
- vue2
解决方案
vue2
:value.sync="show"
<MyDialog :show.sync="show"/>
vue3
:modelValue="show"="show"
<MyDialog v-model:show="show"/>