经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » Vue.js » 查看文章
Vue实现全局的toast组件方式
来源:jb51  时间:2023/3/6 9:13:04  对本文有异议

Vue全局的toast组件

1.创建toast组件

  1. <template>
  2. <div class="toast" v-if="show">
  3. {{ msg }}
  4. </div>
  5. </template>
  6.  
  7. <style scoped>
  8. .toast{
  9. position: fixed;
  10. left: 50%;
  11. top: 50%;
  12. transform: translate(-50%,-50%);
  13. border-radius: 3px;
  14. max-width: 200px;
  15. padding: 10px;
  16. background: #333;
  17. color: #fff;
  18. font-size: 14px;
  19. opacity: .9;
  20. text-align: justify;
  21. word-break: break-all;
  22. word-wrap: break-word;
  23. }
  24. </style>

2.创建toast.js文件

  1. import toast from "@/components/toast"
  2. export default (Vue) => {
  3. ? ? let toastComp = Vue.extend(toast);
  4. ? ? function showToast(msg , duration = 3e3){
  5. ? ? ? ? let toastDom = new toastComp({
  6. ? ? ? ? ? ? data(){
  7. ? ? ? ? ? ? ? ? return{
  8. ? ? ? ? ? ? ? ? ? ? show:true,
  9. ? ? ? ? ? ? ? ? ? ? msg
  10. ? ? ? ? ? ? ? ? }
  11. ? ? ? ? ? ? }
  12. ? ? ? ? }).$mount()
  13. ? ? ? ? document.body.appendChild(toastDom.$el);
  14. ? ? ? ? setTimeout(() => {
  15. ? ? ? ? ? ? toastDom.show = false
  16. ? ? ? ? }, duration)
  17. ? ? }
  18. ? ? Vue.prototype.$toast = showToast;
  19. }

3.安装并使用 

  1. import toast from "@/plugins/toast";
  2. Vue.use(toast);
  3.  
  4. // 组件里面使用
  5. this.$toast("message");

vue自定义toast组件

//toast.js

  1. const ?TOAST_CLASS = 'toast'
  2. const ?TOAST_OUT_CLASS = 'toast out'
  3. let innerHtml=""
  4. function ?toast(msg,time=1000) {
  5. ? ? let body=document.querySelector('#app');
  6. ? ? if(body.querySelector('.toast')){
  7. ? ? ? ? body.removeChild(body.querySelector('.toast'))
  8. ? ? }
  9. ? ? let toastElem = document.createElement('div')
  10. ? ? toastElem.setAttribute('class',TOAST_CLASS)
  11. ? ? innerHtml = `<sapn>${msg}</sapn>`
  12. ? ? toastElem.innerHTML = innerHtml;
  13. ? ? body.appendChild(toastElem);
  14. ? ? setTimeout(function () {
  15. ? ? ? ? toastElem.setAttribute('class',TOAST_OUT_CLASS)
  16. ? ? },time)
  17. ? ? setTimeout(function () {
  18. ? ? ? ? let elm = body.querySelector('.toast');
  19. ? ? ? ? if(elm){
  20. ? ? ? ? ? ? body.removeChild(elm)
  21. ? ? ? ? }
  22. ? ? },time+1000)
  23.  
  24. }
  25. export ?default toast

//toast.less

  1. @-webkit-keyframes toastIn {
  2. ? 0%{
  3. ? ? opacity: 1;
  4. ? }
  5. ? 50%{
  6. ? ? opacity: 1;
  7. ? }
  8. ? 100%{
  9. ? ? opacity: 1;
  10. ? }
  11. }
  12. @-webkit-keyframes toastOut {
  13. ? 0%{
  14. ? ? opacity:1;
  15. ? }
  16. ? 50%{
  17. ? ? opacity:0.7;
  18. ? }
  19. ? 100%{
  20. ? ? opacity:0;
  21. ? }
  22. }
  23. //animation: name duration timing-function delay iteration-count direction;
  24. .toast{
  25. ? position: fixed;
  26. ? z-index:99;
  27. ? background-color: rgba(0,0,0,0.6);
  28. ? color:#fff;
  29. ? padding:15px 25px;
  30. ? border-radius:5px;
  31. ? top: 50%;
  32. ? left:50%;
  33. ? font-size:18px;
  34. ? transform: translate(-50% , -50%);
  35. ? animation-name: toastIn;
  36. ? animation-duration: 1s;
  37. ? animation-iteration-count: 1;
  38. ? animation-delay: 0s;
  39. }
  40.  
  41. .toast.out {
  42. ? animation-name: toastOut;
  43. ? animation-duration: 1s;
  44. ? animation-iteration-count: 1;
  45. ? animation-delay: 0s;
  46. ? animation-fill-mode: forwards;
  47. }

使用

全局注入(main.js),this._toast(‘XXXX’)调用

  1. import toast from "./utils/toast";
  2. window._toast = toast

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号