14赞
731
当前位置:  开发笔记 > 编程语言 > 正文

Vue封装自己的插件

效果图:image.png1:先创建组件,并创建index.js文件image.pngtosi.vue文件 你好啊 export default { name: "Tosi"

效果图:




Vue 封装自己的插件


image.png

1:先创建组件,并创建index.js文件




Vue 封装自己的插件


image.png

tosi.vue文件


你好啊

export default {
name: "Tosi",
data(){
return {
isShow: false
}
},
methods: {
show(message, duration = 1500){
this.isShow = true
setTimeout(() => {
this.isShow = false
}, duration)
}
}
}
.toast{
background: rgba(0, 0, 0, .7);
padding: 5px 8px;
color: #ffffff;
border-radius: 5px;
position: fixed;
left: 50%;
top: 50%;
transform: translate(-50% ,-50%);
z-index: 10;
}

index.js文件

import Tosi from "./Tosi";
const obj = {}
obj.install = function (Vue) {
//1、创建组件构造器
const toastCOntrustor= Vue.extend(Tosi)
//2、new的方式,根据组件构造器,可以创建来一个组件对象
const tosi = new toastContrustor()
//3、将组件对象,手动挂载到某一个元素上
tosi.$mount(document.createElement('div'))
//4、tois.$el对应的就是div
document.body.appendChild(tosi.$el)
Vue.prototype.$tosi = tosi
}
export default obj

2:在main.js里面进行挂载注册

import Vue from 'vue'
import App from './App.vue'
import tosi from "./components/common/tosi"
Vue.config.productiOnTip= false
Vue.use(tosi)
new Vue({
render: h => h(App)
}).$mount('#app')

3:进行调用

this.$tosi.show("你好", 2000)



推荐阅读
author-avatar
魂牵夢绕的思念丶
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有