如果我们的Vue项目中没有用到任何UI框架的话,为了更好的用户体验,肯定会用到loading和toast。那么我们就自定义这两个组件吧。
1、Toast组件
首先,在common下新建global文件夹,存放我们的toast.vue和toast.js两个文件(当然文件的具体位置你可以自行安排)。
(1). toast.vue
<template lang="html">
<div v-if="isShowToast" class="toast-container" @touchmove.prevent>
<!-- 这里content为双花括号 -->
<span class="loading-txt">{content}</span>
</div>
</template>
<script>
export default {
data () {
return {
isShowToast: true,
content: ''
}
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.toast-container {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(255, 255, 255, 0.1);
}
.toast-msg {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 60%;
padding: 35px;
border-radius: 10px;
font-size: 28px;
line-height: 36px;
background: #eee;
color: #666;
}
</style>
(2). toast.js
import Vue from 'Vue'
import ToastComponent from './Toast.vue'
const Toast = {}
let showToast = false // 存储loading显示状态
let toastNode = null // 存储loading节点元素
const ToastConstructor = Vue.extend(ToastComponent)
Toast.install = function (Vue, options) {
// 参数
var opt = {
duration: '1200'
}
for (var property in options) {
opt[property] = options[property]
}
Vue.prototype.$toast = function (tips, type) {
if (type === 'hide') {
toastNode.isShowToast = showToast = false
} else {
if (showToast) {
// 如果toast还在,则不再执行
return
}
toastNode = new ToastConstructor({
data: {
isShowToast: showToast,
content: tips
}
})
toastNode.$mount() // 挂在实例,为了获取下面的toastNode.$el
document.body.appendChild(toastNode.$el)
toastNode.isShowToast = showToast = true
setTimeout(function () {
toastNode.isShowToast = showToast = false
}, opt.duration)
}
};
['show', 'hide'].forEach(function (type) {
Vue.prototype.$toast[type] = function (tips) {
return Vue.prototype.$toast(tips, type)
}
})
}
export default Toast
然后,我们需要把写好的组件在 /src/main.js 中引用一下。
import Toast from './components/common/global/toast' Vue.use(Toast)
最后,怎么使用呢?只需在要用的地方this.$toast.show('hello world')
2、Loading组件
loading组件只需要照着toast组件搬过来,稍微改下就可以了。
首先,在common下新建global文件夹,存放我们的loading.vue和loading.js两个文件。
(1). loading.vue
<template lang="html"> <div v-if="isShowLoading" class="loading-container"> <div class="loading-box"> <img class="loading-img" :src="/UploadFiles/2021-04-02/loading.png')">(2). loading.js
import Vue from 'Vue' import LoadingComponent from './Loading.vue' const Loading = {} let showLoading = false // 存储loading显示状态 let loadingNode = null // 存储loading节点元素 const LoadingConstructor = Vue.extend(LoadingComponent) Loading.install = function (Vue) { Vue.prototype.$loading = function (tips, type) { if (type === 'hide') { loadingNode.isShowLoading = showLoading = false } else { if (showLoading) { // 如果loading还在,则不再执行 return } loadingNode = new LoadingConstructor({ data: { isShowLoading: showLoading, content: tips } }) loadingNode.$mount() // 挂在实例,为了获取下面的loadingNode.$el document.body.appendChild(loadingNode.$el) loadingNode.isShowLoading = showLoading = true } }; ['show', 'hide'].forEach(function (type) { Vue.prototype.$loading[type] = function (tips) { return Vue.prototype.$loading(tips, type) } }) } export default Loading然后,在 /src/main.js 中引用一下loading组件。
import Loading from './components/common/global/loading' Vue.use(Loading)最后,只需在要用的地方
this.$loading.show('hello world')、 this.$loading.hide()总结
以上所述是小编给大家介绍的Vue自定义全局Toast和Loading的实例详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新动态
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]