Application scenario explanation of Vue dynamic component

application scenario explanation vue dynamic


Problem description

tab Switching scenarios are often used in development . When it comes to achieving this effect , We often think of the following ways to achieve this effect .

  • Mode one Use display:none; To control the dom Show and hide elements . So as to achieve , Two tab Show and hide . But if there are three or four tab To switch , This way is not desirable .
  • Mode two Use vue The instructions in v-if perhaps v-show Realization . This way you can achieve , But the code is not elegant . Imagine a .vue There's a bunch of them in the file v-if What kind of effect ? And use v-if Many variables have to be declared for identification . So it's not a very good solution
  • Mode three Use elementui perhaps iview Medium tab Switch components This way is OK , But sometimes you need to /deep/ Change the style , There's a little bit of trouble .

The author thinks , Use vue Dynamic components to achieve tab The switching effect of , It will be more convenient .

What is? vue Dynamic components of

vue Dynamic components of , It's essentially a component , In general, a component is a piece with js Logical UI View layer . The so-called dynamic component is that we can dynamically control the specific display of that component in a certain place of the page according to some conditions . It's kind of tab The taste of switching .

Application scenario description

Demand rendering

1.gif It's very simple , It's just one. tab The effect of switching , Of course, in actual development ,tab The style effect may be a little more complicated .

Implementation steps

First step ( Create a new component and introduce registration )

First, in the components Four... Are defined under the folder .vue file , As tab Switch the content part of the rendering , The introduction of both can be used .

newly build snipaste_20210502_213624.png Introduce and register

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,
},
 Copy code 

The second step ( Layout , Put... On it tab Click on the label , The next component presents the corresponding content )

<template>
<div id="app">
<div class="top">
<!-- place tab Click on the label -->
</div>
<div class="bottom">
<!-- Place dynamic components to render corresponding content -->
</div>
</div>
</template>
 Copy code 

The third step ( Write the above tab Click on the label )

// First of all we have data In the definition of array cardArr Save click tab The data of 
data() {
return {
whichIndex: 0,
cardArr: [
{
componentName: " Dynamic component one ",
},
{
componentName: " Dynamic component two ",
},
{
componentName: " Dynamic component three ",
},
{
componentName: " Dynamic component four ",
},
],
};
},
// And then use v-for Loop out and present 
<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">
<!-- Place dynamic components ... -->
</div>
</div>
</template>
// And because it needs to be highlighted , So initially, we default the index to 0 That's the first highlight , Use data As defined in whichIndex and :class Realization 
// Highlight style 
.highLight {
box-shadow: 0 15px 30px rgba(0, 0, 0, 0.2);
transform: translate3d(0, -1px, 0);
}
 Copy code 

Step four ( Use dynamic component tags <component/>

 // Dynamic component tags <component/> There is one is attribute ,is Who is the value of , You can play up who ,
// Here we start with a variable componentId Save up ,componentId For whom , Show who 
<div class="bottom">
<component :is="componentId"></component>
</div>
// Let's make the first presentation by default , At the same time, we need to make cardList Component name and component id Corresponding upper ,
// therefore data It should be changed to this 
data() {
return {
whichIndex: 0,
componentId: "one", // The value is that we are components The name of the imported component registered in the 
cardArr: [
{
componentName: " Dynamic component one ",
componentId: "one", // To match it 
},
{
componentName: " Dynamic component two ",
componentId: "two", // To match it 
},
{
componentName: " Dynamic component three ",
componentId: "three", // To match it 
},
{
componentName: " Dynamic component four ",
componentId: "four", // To match it 
},
],
};
},
 Copy code 

Step five ( Click on a tab Components , Change the correspondence dynamically componentId value )

<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 Multiple operation codes can be written in the tag , Separate them with semicolons -->
{{ item.componentName }}
</div>
</div>
<div class="bottom">
<!-- keep-alive Cache components , In this case , Components will not be destroyed ,DOM Will not be re rendered ,
The browser will not reflow and redraw , You can optimize performance . If you don't use it, the page will load a little slower -->
<keep-alive>
<component :is="componentId"></component>
</keep-alive>
</div>
</div>
</template>
 Copy code 

Complete code with

<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: " Dynamic component one ",
componentId: "one",
},
{
componentName: " Dynamic component two ",
componentId: "two",
},
{
componentName: " Dynamic component three ",
componentId: "three",
},
{
componentName: " Dynamic component four ",
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>
 Copy code 

In real business , Flexible use of dynamic components , The style of the code written in this way is quite elegant . Life is not easy , We work together

版权声明
本文为[Water is redundant and water is abundant]所创,转载请带上原文链接,感谢
https://qdmana.com/2021/05/20210503112203909c.html

  1. HTML + CSS + JavaScript to achieve cool Fireworks (cloud like particle text 3D opening)
  2. HTML + CSS + JavaScript realizes 520 advertising love tree (including music), which is necessary for programmers to express themselves
  3. Solve the problem of Web front-end deployment server (it can be deployed online without a server)
  4. HTML + CSS + JS make wedding countdown web page template (520 / Tanabata Valentine's Day / programmer advertisement)
  5. What else can driverless minibus do besides "Park connection"?
  6. Cloud native leads the era of all cloud development
  7. NRM mirror source management tool
  8. Bring it to you, flex Jiugong
  9. Lolstyle UI component development practice (II) -- button group component
  10. Deconstruction assignment in ES6
  11. Luo 2 peerless Tang clan was officially launched. The official gave a key point, and the broadcast time was implied
  12. 20初识前端HTML(1)
  13. 当新零售遇上 Serverless
  14. 20 initial knowledge of front-end HTML (1)
  15. When new retail meets serverless
  16. [golang] - go into go language lesson 5 type conversion
  17. [golang] - go into go language lesson 6 conditional expression
  18. HTML5(八)——SVG 之 path 详解
  19. HTML5 (8) -- detailed explanation of SVG path
  20. 需要开通VIP以后页面内容才能复制怎么办?控制台禁用javascript即可
  21. Web前端|CSS入门教程(超详细的CSS使用讲解,适合前端初学者)
  22. 实践积累 —— 用Vue3简单写一个单行横向滚动组件
  23. Serverless 全能选手,再下一城
  24. What if you need to open a VIP to copy the page content? Just disable JavaScript on the console
  25. Web front end | CSS introductory tutorial (super detailed CSS explanation, suitable for front-end beginners)
  26. Practice accumulation - write a single line horizontal scroll component simply with vue3
  27. Dili Reba is thin again. She looks elegant and high in a strapless hollow skirt, and her "palm waist" is beautiful to a new height
  28. Serverless all-round player, next city
  29. The difference between MySQL semi synchronous replication and lossless semi synchronous replication
  30. Vue表单设计器的终极解决方案
  31. The ultimate solution for Vue form designer
  32. Nginx从理论到实践超详细笔记
  33. Yu Shuxin's red backless swimsuit is split to the waist and tail, with a concave convex figure and excessive color matching, and his face is white to dazzling
  34. Nginx ultra detailed notes from theory to practice
  35. 【动画消消乐|CSS】086.炫酷水波浪Loading过渡动画
  36. typecho全站启用https
  37. CCTV has another popular employee. The off-site interpretation is very professional, and the appearance ability is no less than that of Wang Bingbing
  38. [animation Xiaole | CSS] 086. Cool water wave loading transition animation
  39. Enable HTTPS in Typecho
  40. 50天用JavaScript完成50个web项目,我学到了什么?
  41. 根据JavaScript中原生的XMLHttpRequest实现jQuery的Ajax
  42. What have I learned from completing 50 web projects with JavaScript in 50 days?
  43. "My neighbor doesn't grow up" has hit the whole network. There are countless horse music circles, and actor Zhou Xiaochuan has successfully made a circle
  44. 根据JavaScript中原生的XMLHttpRequest实现jQuery的Ajax
  45. Implement the Ajax of jQuery according to the native XMLHttpRequest in JavaScript
  46. Implement the Ajax of jQuery according to the native XMLHttpRequest in JavaScript
  47. 30 + women still wear less T-shirts and jeans. If they wear them like stars, they will lose weight
  48. 数栈技术分享前端篇:TS,看你哪里逃~
  49. Several stack technology sharing front end: TS, see where you escape~
  50. 舍弃Kong和Nginx,Apache APISIX 在趣链科技 BaaS 平台的落地实践
  51. Abandon the landing practice of Kong and nginx, Apache apisik on the baas platform of fun chain technology
  52. 浪迹天涯king教你用elementui做复杂的表格,去处理报表数据(合并表头,合并表体行和列)
  53. 前端HTML两万字图文大总结,快来看看你会多少!【️熬夜整理&建议收藏️】
  54. Wandering around the world king teaches you to use elementui to make complex tables and process report data (merge header, merge table body rows and columns)
  55. 路由刷新数据丢失 - vuex数据读取的问题
  56. Front end HTML 20000 word graphic summary, come and see how much you can【 Stay up late to sort out & suggestions]
  57. Route refresh data loss - vuex data reading problem
  58. Systemctl系统启动Nginx服务脚本
  59. Systemctl system startup nginx service script
  60. sleepless