vue render封装switch提示

引用子组件

 <switchComp
   checkedTxt="上架"
   unCheckTxt="下架"
   v-model="formRole.status"
   :switchStatus="formRole.status"
   ></switchComp>

定义子组件

<script>
export default {
    data () {
        return {
            isBooleanVal: true, // 传的value值是否是布尔值
            popconfirmVisible: false, // 隐藏冒泡弹窗
        }
    },
    props: {
        checkedTxt: {
            type: String,
            default: "上架"
        },
        unCheckTxt: {
            type: String,
            default: "下架"
        },
        okEvent: {
            type: Function,
            default: function () {}
        },
        cancelEvent: {
            type: Function,
            default: function () {}
        },
        switchStatus: {
            type: [Boolean, Number],
            default: true
        }
    },
    mounted () {
        if (this.getValType(this.switchStatus) == "[object Boolean]") {
            this.isBooleanVal = true
        } else {
            this.isBooleanVal = false
        }
    },
    render(createElement) {
        return createElement(
            'div',
            {},
            [
                createElement(
                    'a-popconfirm',
                    {
                        attrs: {
                            title: "确定"+this.unCheckTxt+"吗?",
                            okText: "确定",
                            cancelText: "取消",
                            disabled: this.popconfirmVisible
                        },
                        on: {
                            confirm: () => {
                                // if (this.okEvent) {
                                    let resu = null
                                    if (this.isBooleanVal) {
                                        resu = (!this.switchStatus) ? true : false
                                    } else {
                                        resu = (!this.switchStatus) ? 1 : 0
                                    }
                                    this.$emit("input", resu)
                                    //this.okEvent()
                                // }
                            },
                            cancel: () => {
                                if (this.cancelEvent) {
                                    //this.cancelEvent()
                                    // this.$emit("input", this.switchStatus)
                                }
                            }
                        }
                    },
                    [
                        createElement(
                            'a-switch',
                            {
                                attrs: {
                                    checked: this.switchStatus ? true : false,
                                    checkedChildren: this.checkedTxt,
                                    unCheckedChildren: this.unCheckTxt
                                },
                                on: {
                                    change: (v) => {
                                        if (v) {
                                            this.popconfirmVisible = true
                                            // this.switchStatus = this.isBooleanVal ? true : 1
                                            this.$emit("input", this.isBooleanVal ? true : 1)
                                        }
                                        setTimeout(() => {
                                            this.popconfirmVisible = false
                                        })
                                    }
                                }
                            }
                        )
                    ]
                )
            ]
        )
    },
    methods: {
        getValType (v) {
            return Object.prototype.toString.call(v)
        }
    }
}
</script>

<style lang="scss" scoped>

</style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,针对您的问题,我可以为您提供一个简单的 Vue3 消息提示组件的封装示例。 首先,我们可以创建一个单独的 Message 组件用于显示消息提示,如下所示: ```vue <template> <div class="message" :class="type"> <div class="message-content">{{ message }}</div> <div class="close" @click="close">x</div> </div> </template> <script> import { ref } from 'vue'; export default { props: { type: { type: String, default: 'info' }, message: { type: String, required: true } }, setup(props) { const visible = ref(true); const close = () => { visible.value = false; }; return { visible, close }; } }; </script> <style scoped> .message { position: fixed; top: 20px; left: 50%; transform: translateX(-50%); padding: 10px 20px; border-radius: 4px; color: #fff; font-size: 14px; box-shadow: 0px 2px 5px rgba(0, 0, 0, 0.2); transition: opacity 0.3s, height 0.3s; opacity: 1; } .message.info { background-color: #1890ff; } .message.success { background-color: #52c41a; } .message.warning { background-color: #faad14; } .message.error { background-color: #f5222d; } .message-content { display: inline-block; margin-right: 10px; } .close { position: absolute; top: 5px; right: 5px; cursor: pointer; } </style> ``` 接着,我们可以在我们的 Vue3 项目中创建一个全局的消息提示插件,用于方便地调用 Message 组件。在该插件中,我们可以定义不同类型的消息提示方法,如 success、info、warning 和 error,分别对应不同的消息提示样式和内容。 ```javascript import { createApp } from 'vue'; import Message from './Message.vue'; const messagePlugin = { install(app) { app.config.globalProperties.$message = { success(message) { showMessage('success', message); }, info(message) { showMessage('info', message); }, warning(message) { showMessage('warning', message); }, error(message) { showMessage('error', message); } }; } }; function showMessage(type, message) { const messageInstance = createApp(Message, { type, message }); const mountNode = document.createElement('div'); document.body.appendChild(mountNode); messageInstance.mount(mountNode); setTimeout(() => { messageInstance.unmount(); document.body.removeChild(mountNode); }, 3000); } export default messagePlugin; ``` 最后,在我们的 main.js 文件中使用该插件即可: ```javascript import { createApp } from 'vue'; import App from './App.vue'; import messagePlugin from './messagePlugin'; const app = createApp(App); app.use(messagePlugin); app.mount('#app'); ``` 这样,我们就完成了一个简单的 Vue3 消息提示组件的封装

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值