vue3自定义组件的封装

我们这里一封装一个抽屉组件为例子。

环境:element-plus,windicss

1.定义一个vue文件用于存放我们的dom。

FormDrawer.vue:

把抽屉组件扔进去,给它设置宽高,并暴露出打开和关闭方法

<!-- eslint-disable vue/multi-word-component-names -->
<template>
    <el-drawer v-model="showDrawer" title="修改密码" size="45%"
        :close-on-click-modal="false">
    </el-drawer>
</template>
<script setup>
import { ref } from 'vue';

const showDrawer = ref(false)
const open = () => { showDrawer.value = true }
const close = () => { showDrawer.value = false }
defineExpose({
    open,
    close,
})
</script>

2.在我们的父组件中引用我们的子组件,并设置它的开启和关闭

<script setup>
import FormDrawer from '../../../../components/FormDrawer.vue'
//设置它的开启和关闭
const formDrawerRef = ref(null)
const button = ()=>{
    formDrawerRef.value.open()
}
</script>
<template>
    <button @click="button">成功</button>
    <form-test ref="formDrawerRef">
    
    </form-test>
</template>

效果

3.再回来设置自定义组件的样式

我们给它点样式:

<!-- eslint-disable vue/multi-word-component-names -->
<template>
    <el-drawer v-model="showDrawer" title="修改密码" size="45%" class="formDrawer"
        :close-on-click-modal="false">
        <div class="DrawerForm">
            <div class="body">
                <!-- 这是插槽,<slot></slot> 是 HTML 的一个重要特性,也是 Web 组件(Web Components)的一部分。
                    它允许开发者在自定义组件中插入占位符,以便在将来使用时,可以在这些占位符中填充内容。 -->
                <slot></slot>
            </div>
            <div class="actions">
                <el-button type="primary" @click="submit">确认</el-button>
                <el-button type="default" @click="close">取消</el-button>
            </div>
        </div>
    </el-drawer>
</template>
<script setup>
import { ref } from 'vue';

const showDrawer = ref(false)
const open = () => { showDrawer.value = true }
const close = () => { showDrawer.value = false }
defineExpose({
    open,
    close,
})
</script>
<style>
.formDrawer {
    @apply ;
}
.DrawerForm {
    width: 100%;
    height: 100%;
    position: relative;
    /* flex-col垂直排布 */
    @apply flex flex-col;
}

.body {
    /* 超出部分是滚动 */
    overflow-y: auto;
    @apply ;
}

/* margin-top:auto就是会自动至于底部,但是很重要的是!父和子组件都要是块级组件 */
.actions {
    height: 50px;
    @apply mt-auto flex items-center;
}</style>

4.在父组件就可以给插槽里面加东西了

我加了一些表单组件

<script setup>
import FormDrawer from '../../../../components/FormDrawer.vue'
import { ref, reactive } from 'vue'
//设置它的开启和关闭
const formDrawerRef = ref(null)
const button = ()=>{
    formDrawerRef.value.open()
}


//修改密码
const ruleForm = reactive({
    oldpassword:"",
    pasword: "",
    repassword: ""
})
const rules = reactive({
    oldpassword: {
        required: true,
        message: '旧密码不能为空',
        trigger: 'blur'
    },
    password: {
        required: true,
        message: '不要为空',
        trigger: 'blur'
    },
    repassword: {
        required: true,
        message: '新密码不能为空',
        trigger: 'blur'
    }
})
//这是表单验证查看是否正确,在validate会返回true,否则返回false
const formRef = ref(null)

</script>
<template>
    <button @click="button">成功</button>
    <form-test ref="formDrawerRef">
        <form-test ref="formDrawerRef">
            <el-form :model="ruleForm" :rules="rules" label-width="120px" ref="formRef">
                <el-form-item label="旧密码" prop="oldpassword">
                    <el-input v-model="ruleForm.oldpassword" show-password placeholder="请输入旧密码" />
                 </el-form-item>
                <el-form-item label="新密码" prop="password">
                    <el-input v-model="ruleForm.password" show-password placeholder="请输入新密码" />
                </el-form-item>
                <el-form-item label="确认密码" prop="repassword">
                    <el-input v-model="ruleForm.repassword" show-password placeholder="请输入确认密码" />
                </el-form-item>
            </el-form>
        </form-test>
    </form-test>
</template>

5.把子组件的属性暴露出去

现在就基本样式出来了,但是吧,我们不止需要这些呀。我们还要动态设置他的标题(“修改密码”),还有按钮样式,按钮的方法。

所以这时候我们要把他当方法暴露出去。

在这里要使用:size="size"等响应式的写法

完整代码了:

<!-- eslint-disable vue/multi-word-component-names -->
<template>
    <el-drawer v-model="showDrawer" :title="title" :size="size" class="formDrawer"
        :close-on-click-modal="false">
        <div class="DrawerForm">
            <div class="body">
                <!-- 这是插槽,<slot></slot> 是 HTML 的一个重要特性,也是 Web 组件(Web Components)的一部分。
                    它允许开发者在自定义组件中插入占位符,以便在将来使用时,可以在这些占位符中填充内容。 -->
                <slot></slot>
            </div>
            <div class="actions">
                <el-button type="primary" @click="submit">{{ comfirmText }}</el-button>
                <el-button type="default" @click="close">取消</el-button>
            </div>
        </div>
    </el-drawer>
</template>
<script setup>
import { ref } from 'vue';
const showDrawer = ref(false)
const open = () => { showDrawer.value = true }
const close = () => { showDrawer.value = false }
defineExpose({
    open,
    close,
})
// 动态暴露我们的组件属性
const prop = defineProps({
    title: String,
    size: {
        type: String,
        default: "45%",
    },
    destroyOnClose: {
        type: Boolean,
        default: false
    },
    comfirmText: {
        type: String,
        default: "提交"
    }
})
//获取到父组件的方法
const emit = defineEmits(["submit"])
//将父组件方法添加到本组件
const submit = ()=>emit("submit")
</script>
<style>
.formDrawer {
    @apply ;
}
.DrawerForm {
    width: 100%;
    height: 100%;
    position: relative;
    /* flex-col垂直排布 */
    @apply flex flex-col;
}

.body {
    /* 超出部分是滚动 */
    overflow-y: auto;
    @apply ;
}

/* margin-top:auto就是会自动至于底部,但是很重要的是!父和子组件都要是块级组件 */
.actions {
    height: 50px;
    @apply mt-auto flex items-center;
}</style>

 6.在父组件使用

完整代码了:

<script setup>
import FormDrawer from '../../../../components/FormDrawer.vue'
import { ref, reactive } from 'vue'
//设置它的开启和关闭
const formDrawerRef = ref(null)
const button = ()=>{
    formDrawerRef.value.open()
}


//修改密码
const ruleForm = reactive({
    oldpassword:"",
    pasword: "",
    repassword: ""
})
const rules = reactive({
    oldpassword: {
        required: true,
        message: '旧密码不能为空',
        trigger: 'blur'
    },
    password: {
        required: true,
        message: '不要为空',
        trigger: 'blur'
    },
    repassword: {
        required: true,
        message: '新密码不能为空',
        trigger: 'blur'
    }
})
//这是表单验证查看是否正确,在validate会返回true,否则返回false
const formRef = ref(null)

</script>
<template>
    <button @click="button">成功</button>
    <form-test ref="formDrawerRef" title="成功超过超过" size="50%" comfirmText="提交" @submit="submit">
            <el-form :model="ruleForm" :rules="rules" label-width="120px" ref="formRef">
                <el-form-item label="旧密码" prop="oldpassword">
                    <el-input v-model="ruleForm.oldpassword" show-password placeholder="请输入旧密码" />
                 </el-form-item>
                <el-form-item label="新密码" prop="password">
                    <el-input v-model="ruleForm.password" show-password placeholder="请输入新密码" />
                </el-form-item>
                <el-form-item label="确认密码" prop="repassword">
                    <el-input v-model="ruleForm.repassword" show-password placeholder="请输入确认密码" />
                </el-form-item>
            </el-form>
        </form-test>
</template>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 3中的自定义指令封装是一种扩展Vue的能力,允许开发者在DOM元素上添加自定义行为。下面是Vue 3中自定义指令封装的步骤: 1. 创建自定义指令:使用`app.directive`方法来创建自定义指令。该方法接受两个参数,第一个参数是指令名称,第二个参数是一个对象,包含了指令的各种钩子函数和配置选项。 2. 钩子函数:自定义指令可以通过钩子函数来定义其行为。常用的钩子函数有: - `beforeMount`:在指令绑定的元素挂载到DOM之前调用。 - `mounted`:在指令绑定的元素挂载到DOM之后调用。 - `beforeUpdate`:在指令所在组件更新之前调用。 - `updated`:在指令所在组件更新之后调用。 - `beforeUnmount`:在指令所在组件卸载之前调用。 - `unmounted`:在指令所在组件卸载之后调用。 3. 配置选项:除了钩子函数外,还可以通过配置选项来定义自定义指令的行为。常用的配置选项有: - `bind`:在指令绑定到元素时立即调用,只调用一次。 - `update`:在指令所在组件的VNode更新时调用,可能会调用多次。 - `unbind`:在指令从元素上解绑时调用,只调用一次。 下面是一个示例,演示了如何在Vue 3中封装一个自定义指令: ```javascript // 创建Vue实例 const app = Vue.createApp({}); // 创建自定义指令 app.directive('my-directive', { beforeMount(el, binding, vnode) { // 指令绑定到元素之前的操作 }, mounted(el, binding, vnode) { // 指令绑定到元素之后的操作 }, beforeUpdate(el, binding, vnode) { // 指令所在组件更新之前的操作 }, updated(el, binding, vnode) { // 指令所在组件更新之后的操作 }, beforeUnmount(el, binding, vnode) { // 指令所在组件卸载之前的操作 }, unmounted(el, binding, vnode) { // 指令所在组件卸载之后的操作 } }); // 将Vue实例挂载到DOM元素上 app.mount('#app'); ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值