vue3 自定义插件[plugins]
插件基本语法
createApp(component, props)
- createApp功能是创建一个实例
- createApp有两个参数,第一个参数是根组件,第二个参数是要传递给根组件的props
plugin
- 插件 (Plugins) 是一种能为 Vue 添加全局功能的工具代码。
- 一个插件可以是一个拥有 install() 方法的对象,也可以直接是一个安装函数本身。
简单事例
const myPlugin = {
install(app, options) {
}
}
const myPlugin = (app, options) => {
}
import { createApp } from 'vue'
const app = createApp({})
app.use(myPlugin, {
})
指令可以做哪些事情
- 自定义MessageBox(alert)模态框
- 自定义Toast
- 图片点击放大
- 优惠券选择弹层
- 商品sku选择弹层
- ......
plugin实战
自定义messageBox
<template>
<div class="confirm-box" v-if="showMessage">
<div class="confirm">
<div class="content">
{{ confirmObj.content }}
</div>
<div v-if="confirmObj.description" class="description">
{{ confirmObj.description }}
</div>
<div class="confirm-btn">
<div class="btn border" @click="cancel" v-if="confirmObj.cancelText">{{ confirmObj.cancelText }}</div>
<div
class="btn primary"
:class="{ 'border-l': confirmObj.cancelText, 'block-btn': !confirmObj.cancelText }"
@click="sure"
>
{{ confirmObj.confirmText || '确定' }}
</div>
</div>
</div>
</div>
</template>
<script setup>
import { ref, onMounted } from 'vue'
const props = defineProps({
myProvide: {
type: Function,
required: true
},
})
const showMessage = ref(false)
const confirmObj = ref({})
let callback = () => {}
const showConfirm = (obj) => {
confirmObj.value = obj
showMessage.value = true
return new Promise((resolve) => {
callback = resolve
})
}
const cancel = () => {
callback('cancel')
showMessage.value = false
}
const sure = () => {
callback('confirm')
showMessage.value = false
}
onMounted(() => {
props.myProvide('$confirm', showConfirm)
})
</script>
<style lang="less">
.pop-mask-overflow {
overflow: hidden;
}
.confirm-box {
position: fixed;
top: 0;
left: 0;
z-index: 10000000;
width: 100vw;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
background: rgba(0, 0, 0, 0.5);
.confirm {
width: 600px;
padding: 80px 60px 72px;
background: #fff;
border-radius: 2px;
}
.content {
font-size: 40px;
line-height: 56px;
color: #000;
}
.description {
margin-top: 32px;
font-size: 28px;
font-weight: 300;
color: #555555;
font-family: PingFangSC-Light, PingFang SC;
}
.confirm-btn {
display: flex;
align-items: center;
justify-content: space-between;
height: 50px;
margin-top: 80px;
.btn {
display: flex;
align-items: center;
justify-content: center;
height: 80px;
width: 224px;
font-size: 28px;
line-height: 40px;
color: #000;
&.border {
border: 1px solid #333;
}
&.primary {
background: #000000;
color: #fff;
}
&.block-btn {
width: 100%;
}
}
}
}
</style>
import {createApp} from 'vue'
import Confirm from './index.vue'
export default {
install: (app) => {
const myProvide = app.provide
createApp(Confirm, {
myProvide
}).mount('#messageBox')
}
}
app.use(MessageBox)
const $confirm = inject('$confirm')
const click = () => {
$confirm({
content: '测试title',
desc: '测试content',
confirmText: '确定',
cancelText: '取消'
}).then(r => console.log(r))
}
玫瑰花宝典