vue3+element 封装el-input

search组件

<template>
    <div class="contain">
        <el-input class="input" v-model="value" @change="onInput" :placeholder="placeholder"></el-input>
    </div>
</template>

<script>
import { defineComponent, reactive, ref, watch, toRefs } from "vue";
export default defineComponent({
    name: "CustomInput",
    props: {
        modelValue: {
            type: [String, Number],
            default: "",
        },
        placeholder: { type: String, default: "" },
    },
    emits: ["update:modelValue"],
    setup(props, context) {
        // 绑定控件的值
        const value = ref("");

        // 监听属性,给 value 赋值
        watch(
            () => props.modelValue,
            (v1, v2) => {
                value.value = v1;
            },{
                immediate: true;    // 必须要深度监听,否则初始值赋不进去
            }
        );
        return {
            value,
            props,
        };
    },
    methods: {
        onInput(e) {
            this.$emit("update:modelValue", e);
        },
    },
});
</script>

使用的地方

<template>
    <div>
        <search v-model="msg" placeholder="请输入内容"/>
    </div>
</template>


<script>
import search from '../components/input/search.vue'
export default {
    components:{
        search
    },
    data() {
        return {
            msg:"3",
        }
    },
}
</script>

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面我来尝试给您讲解一下如何基于若依框架使用vue3+element-plus去二次封装一个el-dialog: 1. 首先,在使用vue3+element-plus进行开发前,需要先安装对应的依赖。可以通过以下命令进行安装: ``` npm install vue@next npm install element-plus --save ``` 2. 接下来,我们需要创建一个新的dialog组件。可以在`components`文件夹下新建一个`dialog.vue`文件,然后在该文件中定义我们的dialog组件。 ```vue <template> <el-dialog :visible.sync="visible" :title="title" :width="width" :before-close="beforeClose"> <slot></slot> </el-dialog> </template> <script> import { defineComponent } from 'vue' import { ElDialog } from 'element-plus' export default defineComponent({ name: 'Dialog', components: { ElDialog, }, props: { visible: { type: Boolean, default: false, }, title: { type: String, default: '', }, width: { type: String, default: '50%', }, beforeClose: { type: Function, default: () => {}, }, }, }) </script> ``` 在`dialog`组件中,我们使用了`<el-dialog>`组件,并且对其进行了二次封装。我们将`visible`、`title`、`width`和`beforeClose`四个属性绑定到`<el-dialog>`组件上,同时在`<slot>`中插入了组件传递进来的内容。 3. 接下来,我们需要在使用dialog的页面中引入该组件,并且传递需要的参数。 ```vue <template> <div> <el-button @click="showDialog">打开dialog</el-button> <dialog :visible="dialogVisible" :title="dialogTitle" :beforeClose="beforeClose"> <el-form> <el-form-item label="姓名"> <el-input v-model="name"></el-input> </el-form-item> <el-form-item label="年龄"> <el-input v-model="age"></el-input> </el-form-item> </el-form> </dialog> </div> </template> <script> import { defineComponent, ref } from 'vue' import Dialog from '@/components/dialog.vue' export default defineComponent({ name: 'Page', components: { Dialog, }, setup() { const dialogVisible = ref(false) const dialogTitle = ref('示例dialog') const name = ref('') const age = ref('') const showDialog = () => { dialogVisible.value = true } const beforeClose = (done) => { if (name.value && age.value) { done() } else { this.$message.warning('请输入完整信息') } } return { dialogVisible, dialogTitle, name, age, showDialog, beforeClose, } }, }) </script> ``` 在`Page`页面中,我们引入了刚才定义的`<dialog>`组件,并且传递了需要的参数。在点击打开dialog的按钮时,我们通过`showDialog`方法来显示dialog。在dialog关闭前,我们通过`beforeClose`方法来进行校验,只有当输入完整信息时才能关闭dialog。 至此,我们就完成了基于若依框架使用vue3+element-plus去二次封装一个el-dialog的操作。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值