当前位置:网站首页>教你如何封装功能组件和页面组件
教你如何封装功能组件和页面组件
2022-08-05 05:16:00 【多次拒绝王姨】
如果你是学的vue框架 ,那么你是必须要进行组件的封装。不然当一个页面的代码过多的时候就令人看起来冗余,也容易产生一些变量的污染,让别人看起来显得不太专业
话不多唠,开始上干货
一.components
找到创建vue文件的components文件,这个文件将会是我们用来封装一些公共的组件,在里面创建一个index.ts的文件 ,正如图上所示,这个文件 正是components文件的下一级

二.进行一些简单功能组件的封装
当我们写一些页面功能的时候,一些消息的提示框或者确认框看起来简单的实用,但其中也分为自己封装或者一些插件,
下载一些插件,只用了其中的一个功能,会是很浪费内存的,这个时候 我们可以自己进封装,然后调用就可以了
首先。在components文件另开拓一个文件夹,然后在components下一级的index文件中导入并注册全局

因为 我使用的是vue3 +ts 则需要src 文件下进行类型声明 (进行类型的好处就是 当有错误或者渲染的时候有提示)

这里就是components下文件的内容 ,到这里一个全局组件和内容 就已经完成的差不多了
<script lang="ts" setup>
import { onMounted, ref } from 'vue';
// 导入页面初始化状态和响应式ref
import { MessageType } from './type';
// 导入在type定义的数据状态
defineProps<{
type: MessageType,
text: string
}>()
// 定义一个对象,包含三种情况的样式,对象key就是类型字符串
const style = {
warning: {
icon: 'icon-warning',
color: '#E6A23C',
backgroundColor: 'rgb(253, 246, 236)',
borderColor: 'rgb(250, 236, 216)',
},
error: {
icon: 'icon-shanchu',
color: '#F56C6C',
backgroundColor: 'rgb(254, 240, 240)',
borderColor: 'rgb(253, 226, 226)',
},
success: {
icon: 'icon-queren2',
color: '#67C23A',
backgroundColor: 'rgb(240, 249, 235)',
borderColor: 'rgb(225, 243, 216)',
},
}
// 初始隐藏
const isShow = ref(false)
// dom渲染完成,显示
onMounted(() => {
isShow.value = true
})
</script>
// 这里是内容
<template>
<Transition name="down">
<div class="xtx-message" :style="style[type]" v-show="isShow">
<i class="iconfont" :class="style[type].icon"></i>
<span class="text">{
{text}}</span>
</div>
</Transition>
</template>
// 这里是一些消息提示的状态和样式
<style scoped lang="less">
.down-enter-from {
transform: translateY(-70px);
}
.down-enter-active {
transition: transform 0.5s;
}
.xtx-message {
width: 300px;
height: 50px;
position: fixed;
z-index: 9999;
left: 50%;
margin-left: -150px;
top: 25px;
line-height: 50px;
padding: 0 25px;
border: 1px solid #e4e4e4;
background: #f5f5f5;
color: #999;
border-radius: 4px;
i {
margin-right: 4px;
vertical-align: middle;
}
.text {
vertical-align: middle;
}
}
</style>但还需要进行下一步
因为使用的是v3需在message文件下面在声明两个类型

声明的三个状态 使用type进行声明类型 加联合类型

messzge下面的index文件: 运用到vue自带的两个函数, {h} 和{render}函数
h函数:创建一个虚拟的dom
render函数:将一个虚拟dom挂载到真实的dom上
// 导入XtxMessage.vue组件
// 通过代码的方式去渲染它
import { h, render } from 'vue'
import { Message } from './type'
import XtxMessage from './XtxMessage.vue'
// 创建一个dom容器
// 把这个容器添加在body上
const div = document.createElement('div')
div.className = "xtx-message-container"
document.body.appendChild(div)
// 调用这个函数
let timer = -1
export default function Message(obj: Message) {
const vNode = h(XtxMessage, {type: obj.type, text: obj.text})
// 把虚拟dom放入上面定义的容器中
render(vNode, div)
// 清除定时器
clearTimeout(timer)
// 开启定时器
timer = window.setTimeout(() => {
// 从dom上删除
render(null,div)
}, obj.duration || 2000)
}
Message.success = (value: string) => {
Message({ type: "success", text: value, duration: 1500 })
}
Message.error = (value: string) => {
Message({ type: "error", text: value, duration: 1500 })
}
Message.warning = (value: string) => {
Message({ type: "warning", text: value, duration: 1500 })
}然后这个时候我们所做的就已经进本完成了
我们需要在哪个页面使用的时候 直接用这个全局组件即可
创建一个新的页面可以测试使用


三.页面组件
页面组件解决代码冗余和变量污染
写一个主页面的时候,我们可以拆分为两个部分或者更多,可以在其他页面写代码然后导入到主页面,在vue2中 需要用到components注册组件才能使用,
而在vue3 就不大同,vue3 可以直接进行组件的使用,把组件放在你想要放得地方

示例上面代码: 上面代码是我封装的页面一些局部的组件。使用起来相对比v2比较方便一点
边栏推荐
猜你喜欢

【数据库和SQL学习笔记】5.SELECT查询3:多表查询、连接查询

Jupyter notebook选择不同的Anaconda环境作为内核运行

【Pytorch学习笔记】9.分类器的分类结果如何评估——使用混淆矩阵、F1-score、ROC曲线、PR曲线等(以Softmax二分类为例)
![[Pytorch study notes] 10. How to quickly create your own Dataset dataset object (inherit the Dataset class and override the corresponding method)](/img/71/f82e76085f9d8e6610f6f817e2148f.png)
[Pytorch study notes] 10. How to quickly create your own Dataset dataset object (inherit the Dataset class and override the corresponding method)

Tensorflow踩坑笔记,记录各种报错和解决方法

LeetCode刷题之第701题

Redis设计与实现(第二部分):单机数据库的实现

Flutter 3.0升级内容,该如何与小程序结合

CVPR2021 - Inception Convolution with Efficient Dilation Search

11%的参数就能优于Swin,微软提出快速预训练蒸馏方法TinyViT
随机推荐
[Pytorch study notes] 10. How to quickly create your own Dataset dataset object (inherit the Dataset class and override the corresponding method)
IT系统运行维护方法及策略
11%的参数就能优于Swin,微软提出快速预训练蒸馏方法TinyViT
MySQL
发顶会顶刊论文,你应该这样写作
读论文 - Unpaired Portrait Drawing Generation via Asymmetric Cycle Mapping
Service
CVPR 2020 - 频谱正则化
Facial Motion Capture 调研
Thread handler句柄 IntentServvice handlerThread
记我的第一篇CCF-A会议论文|在经历六次被拒之后,我的论文终于中啦,耶!
GIS面试问题
【Kaggle项目实战记录】一个图片分类项目的步骤和思路分享——以树叶分类为例(用Pytorch)
MySQL主从复制—有手就能学会的MySQL集群搭建教程
SharedPreferences和SQlite数据库
【shell编程】第三章:函数
1008 数组元素循环右移问题 (20 分)
九、响应处理——内容协商底层原理
【论文阅读-表情捕捉】High-quality Real Time Facial Capture Based on Single Camera
CVPR 2022 |节省70%的显存,训练速度提高2倍