给你写个示例作为参考思路
// toast.js
export const Toast = {
name: 'toast',
props: {
title: {
type: String,
required: true
}
},
render (h) {
return h('div', {
style: {
backgroundColor: 'rgba(0,0,0,0.3)',
color: '#fff',
width: 'fit-content',
padding: '.5rem',
borderRadius: '6px',
marginBottom: '1rem'
}
}, this.title)
}
}
export default {
install (vue, config) {
const ToastComponent = vue.extend(Toast)
vue.prototype.$toast = function (title) {
const instance = new ToastComponent({ propsData: { title } })
const anchor = document.createElement('div')
window.document.body.appendChild(anchor)
instance.$mount(anchor)
setTimeout(() => {
window.document.body.removeChild(instance.$el)
instance.$destroy()
}, 1500)
}
}
}
使用
// main.js
import Toast from './toast'
Vue.use(Toast)
// 代码中
this.$toast('hay man!')
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…