1、首先建一个HelloWorld.vue其中的内容如下:
<template>
<div class="xtx-skeleton" :style="{width,height}" :class="{shan:animated}">
<div class="block" :style="{backgroundColor:bg}">jjw</div>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
props: {
bg: {
type: String,
default: '#efefef'
},
width: {
type: String,
default: '100px'
},
height: {
type: String,
default: '100px'
},
animated: {
type: Boolean,
default: false
}
}
}
</script>
<style scoped lang="less">
.xtx-skeleton {
display: inline-block;
position: relative;
overflow: hidden;
vertical-align: middle;
.block {
width: 100%;
height: 100%;
border-radius: 2px;
}
}
.shan {
&::after {
content: "";
position: absolute;
animation: shan 1.5s ease 0s infinite;
top: 0;
width: 50%;
height: 100%;
background: linear-gradient(
to left,
rgba(255, 255, 255, 0) 0,
rgba(255, 255, 255, 0.3) 50%,
rgba(255, 255, 255, 0) 100%
);
transform: skewX(-45deg);
}
}
@keyframes shan {
0% {
left: -100%;
}
100% {
left: 120%;
}
}
</style>
2、再新建一个js文件,这里我起名为helloworld.js内容为:
import HelloWorld from "@/components/HelloWorld.vue";
export default {
install (app) {
app.component(HelloWorld.name, HelloWorld)
}
}
3、再main.js中使用插件,内容如下:
import { createApp } from 'vue'
import App from './App.vue'
import './assets/reset.css'
import ui from './helloworld'
createApp(App).use(ui).mount('#app')
4、再App.vue中使用组件
<template>
<HelloWorld width="60px" height="18px" style="margin-right:5px" bg="rgba(0,0,255,0.2)" animated />
<HelloWorld width="60px" height="18px" style="margin-right:5px" bg="deeppink" animated />
</template>
<script setup>
</script>
<style lang="less" scoped>
// 这个类选择器是组件内部的类
.xtx-skeleton {
margin-left: 50px;
}
</style>
5、效果如下: