1、新建一个提示框的vue
dialog.vue:
<template>
<div>
<div>{{title}}</div>
<div>{{container}}</div>
</div>
</template>
<script setup>
import { ref } from "vue"
const props = defineProps({
msg: {}
})
const propData = props.msg;
const title= ref(propData.title);
const container = ref(propData.container);
</script>
2、在一个ts文件中引用上一个提示框组件
test.ts:
import { createVNode, render } from "vue";
import dialog './src/dialog.vue'
const alertMsg = (propData: any) => {
let alert = createVNode(dialog, {
//传值
msg: propData.props
});
// 先生成一个vnode的代码块
render(null, document.querySelector('body') as HTMLElement);
// 通过赋值null清空之前生成的vnode
render(alert , document.querySelector('body') as HTMLElement);
}
export default dealAlert;
3、在需要使用的vue中导入alertMsg方法
nedd.vue:
import dealAlert from '@/......'
dealAlert({
props: {
//数据
}
})