文章摘要
这篇文章主要介绍了如何使用JavaScript和CSS构建一个动态组件,并展示了组件初始化和样式应用的实现方法。文章通过代码示例展示了如何使用V-FOR循环遍历组件数组,动态绑定组件名称和ID,并通过组件生命周期事件(如`componentIs`和`componentOf`)管理组件关系。此外,文章还详细说明了如何通过CSS样式进一步美化组件界面,包括组件布局、样式继承和动态效果的实现。代码示例中还定义了四个动态组件,并展示了它们在不同场景下的应用。
<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 “https://www.jb51.net/article/components/one”;
import two from “https://www.jb51.net/article/components/two”;
import three from “https://www.jb51.net/article/components/three”;
import four from “https://www.jb51.net/article/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>
<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 “https://www.jb51.net/article/components/one”;
import two from “https://www.jb51.net/article/components/two”;
import three from “https://www.jb51.net/article/components/three”;
import four from “https://www.jb51.net/article/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>
© 版权声明
文章版权归作者所有,未经允许请勿转载。