自定义全局UI组件,满足项目中UI个性化需求: 目录结构:src - plugins - button.vue / text.vue / index.js
plugins/button.vue
<template>
<span class="btn" :style="{ backgroundColor: color, borderRadius: radius + 'px', color: textColor }">
<slot></slot>
</span>
</template>
<script>
export default {
name: "Button",
props: {
color: {
type: String,
default: "aquamarine"
},
radius: {
type: Number,
default: 8
},
textColor: {
type: String,
default: "#333"
}
}
}
</script>
<style scoped>
.btn {
display: inline-block;
padding: 10px 15px;
color: #333;
background-color: aquamarine;
border-radius: 8px;
cursor: pointer;
user-select: none;
}
</style>
plugins/text.vue
<template>
<span class="text" :style="{ color: color }">
<slot></slot>
</span>
</template>
<script>
export default {
name: "AText",
props: {
color: {
type: String,
default: "#333"
},
}
}
</script>
plugins/index.js
const plugins = {
Button: () => import("./button.vue"),
AText: () => import("./text.vue")
}
const usePlugins = Vue => {
for(let k in plugins) {
const item = plugins[k]
item.install = vue => {
vue.component(item.name, item)
}
Vue.use(item)
}
}
export default usePlugins
main.js
import usePlugins from "./plugins"
usePlugins(Vue)
app.vue中使用
<Button color="green" :radius="0" textColor="#fff">AAA</Button>
<Button>BBB</Button>
<AText color="pink">123456</AText>