用vue的动态组件怎样做tab切换?有什么好处?
Admin 2021-10-22 群英技术资讯 1611 次浏览
对于实现tab切换效果的方法我们之前也了解了很多,本文给大家分享用用vue的动态组件实现tab切换效果的方法,小编觉得使用vue的动态组件去实现tab的切换效果,会比较方便。
tab切换的场景在开发中会经常用到。当需要实现这种效果的时候,我们常常会想到下面的方式去实现这个效果。
vue的动态组件,本质上还是一个组件,组件通俗来说就是一块具有js逻辑的UI视图层。所谓动态组件就是我们可以根据一些条件去动态控制页面的某个地方具体显示那个组件。这样说就有点tab切换的味道了。
需求效果图

其实很简单,就是一个tab切换的效果,当然实际开发中,tab的样式效果可能会稍微复杂点。
首先在components文件夹下定义四个.vue文件,作为tab切换呈现的内容部分,引入既可使用。
新建

引入并注册
import one from "./components/one";
import two from "./components/two";
import three from "./components/three";
import four from "./components/four";
components: {
one,
two,
three,
four,
},
<template>
<div id="app">
<div class="top">
<!-- 放置tab点击标签 -->
</div>
<div class="bottom">
<!-- 放置动态组件呈现对应内容 -->
</div>
</div>
</template>
// 首先我们在data中定义数组cardArr存放点击tab的数据
data() {
return {
whichIndex: 0,
cardArr: [
{
componentName: "动态组件一",
},
{
componentName: "动态组件二",
},
{
componentName: "动态组件三",
},
{
componentName: "动态组件四",
},
],
};
},
// 然后使用v-for循环出来呈现
<template>
<div id="app">
<div class="top">
<div
class="crad"
:class="{ highLight: whichIndex == index }"
v-for="(item, index) in cardArr"
:key="index"
@click="whichIndex = index"
>
{{ item.componentName }}
</div>
</div>
<div class="bottom">
<!-- 放置动态组件... -->
</div>
</div>
</template>
// 又因为需要有高亮状态,所以初始我们就默认让索引为0的也就是第一个高亮,使用data中定义的whichIndex和:class实现
// 高亮样式
.highLight {
box-shadow: 0 15px 30px rgba(0, 0, 0, 0.2);
transform: translate3d(0, -1px, 0);
}
// 动态组件标签<component/>有一个is属性,is的值为谁,就可以渲染谁,
// 这里我们先用一个变量componentId存起来,componentId为谁,就呈现谁
<div class="bottom">
<component :is="componentId"></component>
</div>
// 我们默认就让第一个第一个呈现吧,同时需要让cardList中的组件名和组件id对应上,
// 所以data中应该修改成这样
data() {
return {
whichIndex: 0,
componentId: "one", // 值就是我们在components对象中注册的引入的组件的名字
cardArr: [
{
componentName: "动态组件一",
componentId: "one", // 要与之对应
},
{
componentName: "动态组件二",
componentId: "two", // 要与之对应
},
{
componentName: "动态组件三",
componentId: "three", // 要与之对应
},
{
componentName: "动态组件四",
componentId: "four", // 要与之对应
},
],
};
},
<template>
<div id="app">
<div class="top">
<div
class="crad"
:class="{ highLight: whichIndex == index }"
v-for="(item, index) in cardArr"
:key="index"
@click="
whichIndex = index;
componentId = item.componentId;
"
>
<!-- @click在标签中可以写多个操作代码,以分号隔开即可 -->
{{ item.componentName }}
</div>
</div>
<div class="bottom">
<!-- keep-alive缓存组件,这样的话,组件就不会被销毁,DOM就不会被重新渲染,
浏览器也就不会回流和重绘,就可以优化性能。不使用的话页面加载就会慢一点 -->
<keep-alive>
<component :is="componentId"></component>
</keep-alive>
</div>
</div>
</template>
<template>
<div id="app">
<div class="top">
<div
class="crad"
:class="{ highLight: whichIndex == index }"
v-for="(item, index) in cardArr"
:key="index"
@click="
whichIndex = index;
componentId = item.componentId;
"
>
{{ item.componentName }}
</div>
</div>
<div class="bottom">
<keep-alive>
<component :is="componentId"></component>
</keep-alive>
</div>
</div>
</template>
<script>
import one from "./components/one";
import two from "./components/two";
import three from "./components/three";
import four from "./components/four";
export default {
components: {
one,
two,
three,
four,
},
data() {
return {
whichIndex: 0,
componentId: "one",
cardArr: [
{
componentName: "动态组件一",
componentId: "one",
},
{
componentName: "动态组件二",
componentId: "two",
},
{
componentName: "动态组件三",
componentId: "three",
},
{
componentName: "动态组件四",
componentId: "four",
},
],
};
},
};
</script>
<style lang="less" scoped>
#app {
width: 100%;
height: 100vh;
box-sizing: border-box;
padding: 50px;
.top {
width: 100%;
height: 80px;
display: flex;
justify-content: space-around;
.crad {
width: 20%;
height: 80px;
line-height: 80px;
text-align: center;
background-color: #fff;
border: 1px solid #e9e9e9;
}
.highLight {
box-shadow: 0 15px 30px rgba(0, 0, 0, 0.2);
transform: translate3d(0, -1px, 0);
}
}
.bottom {
margin-top: 20px;
width: 100%;
height: calc(100% - 100px);
border: 3px solid pink;
display: flex;
justify-content: center;
align-items: center;
}
}
</style>
关于vue的动态组件实现tab切换效果的内容就介绍到这,上述实例具有一定的借鉴价值,感兴趣的朋友可以参考,希望能对大家有帮助,想要了解更多vue的动态组件的应用,大家可以关注其它的相关文章。
文本转载自脚本之家
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
因为现在vue的流行,vue的各种插件都出来了,我们公司也是使用vue做项目,那么应该如何封装,本文就介绍一下如何封装,感兴趣的可以了解一下
这篇文章主要为大家介绍了requestAnimationFrame定时动画屏幕刷新率节流示例浅析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
Node.js中怎么使用Redis?下面本篇文章给大家介绍一下Node.js中使用Redis的方法,你会发现原来这么简单,希望对大家有所帮助!
今天给大家分享的是关于vue轮询的内容,本文会给大家介绍对vue轮询的理解以及实现vue轮询请求的实例,感兴趣的朋友不妨继续往下了解看看。
本文主要介绍了TypeScript数据类型,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008