创建一个toast.js文件,(图片随便找的,改一下即可)
import { Toast } from 'vant';
Vue.use(Toast);
Toast.setDefaultOptions({ duration: 2000 }); //所有toast设置为2秒
// 封装
const mytoast = (msg,type=1)=>{ //type1,2,3分别是成功,警告,失败的图标, 默认不传则为成功图标
let imgUrl = null
if(type == 1){
imgUrl = 'http://onetribe.top/qt/picture/success.png'
}
if(type == 2){
imgUrl = 'http://onetribe.top/qt/picture/warning.png'
}
if(type == 3){
imgUrl = 'http://onetribe.top/qt/picture/error.png'
}
Toast({
message: msg,
icon: imgUrl,
className: 'myshowToast'
});
}
// 挂载
import Vue from 'vue';
Vue.prototype.$mytoast = new Vue()
Vue.prototype.$mytoast = mytoast
export default mytoast
弹窗样式,放到全局的样式配置内
/* 提示框样式 */
.myshowToast{
display: block;
width: 120px;
height: 120px;
position: fixed;
top: calc(50% - 60px);
left: calc(50% - 60px);
text-align: center;
background-color: #f6f9fe;
border-radius: 20px;
}
.myshowToast img{
width: 80px;
height: 80px;
}
在行内调用
<button @click="()=>{this.$mytoast('成功',1)}">成功</button>
<button @click="()=>{this.$mytoast('警告',2)}">警告</button>
<button @click="()=>{this.$mytoast('失败',3)}">错误或失败</button>
在事件内调用
<script>
import mytoast from "../utils/Toast";
export default {
methods: {
go() {
mytoast('成功');
},
},
};
</script>