我是靠谱客的博主 忧郁小刺猬,这篇文章主要介绍Toast模块的封装1 Html2 js3 CSS4 封装5 在main.js中使用,现在分享给大家,希望可以做个参考。

注:对组件进行全局封装,省去每次都需要import和注册组件的麻烦

1 Html

复制代码
1
2
3
4
5
//用于显示弹出提示信息 <div v-show="isShow" class="Toast"> <div>{{message}}</div> </div>

2 js

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
export default{ name:'Toast', data(){ return{ message:'', isShow:false } }, methods:{ show(message,duration){ this.isShow=true this.message=message setTimeout(()=>{ this.isShow=false this.message='' },duration) } } }

3 CSS

复制代码
1
2
3
4
5
6
7
8
9
10
11
//水平垂直居中,面试必考,不会的赶紧复习 .toast{ position:fixed; left:50%; top:50%; transform:translate(-50%,-50%); z-index:999; //防止在最下面 background-color:rgba(0,0,0,0.6); color:#fff; }

4 封装

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import Toast from './Toast.vue' const obj={} obj.install=funciton(Vue){ //创建组件构造器 const ToastConstructor=Vue.extend(Toast) //实例化组件构造器对象 const toast=new ToastConstructor() //将组件对象手动挂载 toast.$mount(document.creatElement('div')) //添加到body中 document.body.appendChild(toast.$el) //添加到Vue原型上 Vue.prototype.$toast=toast } export default obj

5 在main.js中使用

复制代码
1
2
3
4
import toast from './index.js' Vue.use(toast)

最后

以上就是忧郁小刺猬最近收集整理的关于Toast模块的封装1 Html2 js3 CSS4 封装5 在main.js中使用的全部内容,更多相关Toast模块的封装1内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(77)

评论列表共有 0 条评论

立即
投稿
返回
顶部