1、效果

  1. ![image.png](https://cdn.nlark.com/yuque/0/2020/png/1225858/1599572178870-3fd1242f-bac3-44e4-8cac-b49b40148218.png#align=left&display=inline&height=374&margin=%5Bobject%20Object%5D&name=image.png&originHeight=748&originWidth=416&size=134700&status=done&style=none&width=208)

2、功能实现

Toast/index.js

  1. import ToastVue from './toast.vue';
  2. var Toast = {};
  3. let ToastTpl = null;
  4. Toast.install = function (Vue) {
  5. Vue.prototype.$mytoast = (opts = {}) => {
  6. let defaultOpts = {
  7. text: '展示Toast', // 默认显示位置
  8. duration: 2500, // 持续时间
  9. toastSty: '',
  10. };
  11. for (let property in opts) {
  12. if (opts.hasOwnProperty(property) === true) {
  13. defaultOpts[property] = opts[property]; // 使用 options 的配置
  14. }
  15. }
  16. var options = JSON.parse(JSON.stringify(defaultOpts));
  17. if (!ToastTpl) {
  18. // 1、 创建构造器,定义好提示信息的模版
  19. // 2、创建实例,挂在到文档以后的地方
  20. ToastTpl = new ToastVue({
  21. el: document.createElement('div'),
  22. });
  23. }
  24. ToastTpl.show = true;
  25. //ToastTpl.text = options.text;
  26. //ToastTpl.type = options.type;
  27. //ToastTpl.toastSty = options.toastSty;
  28. for (var key in options) {
  29. if (options.hasOwnProperty(key) === true) {
  30. ToastTpl[key] = options[key];
  31. }
  32. }
  33. clearTimeout(this.time);
  34. document.body.appendChild(ToastTpl.$el); // 3、 把创建的实例添加到body中
  35. // 永不关闭
  36. if (options.duration === 'forever') {
  37. return;
  38. }
  39. this.time = setTimeout(() => {
  40. ToastTpl.show = false;
  41. if (options.callback) {
  42. options.callback(options);
  43. }
  44. }, options.duration);
  45. };
  46. };
  47. export default Toast;

Toast/toast.vue

  1. <template>
  2. <div class="vue-toast" v-show="show" :style="toastSty">
  3. <span v-html="text"></span>
  4. </div>
  5. </template>
  6. <script>
  7. import Vue from 'vue';
  8. export default Vue.extend({
  9. name: 'TnToast',
  10. componentName: 'TnToast',
  11. data() {
  12. return {
  13. show: false,
  14. };
  15. },
  16. props: {
  17. text: {
  18. type: String,
  19. default: '这是toast',
  20. },
  21. type: {
  22. type: String,
  23. default: '',
  24. },
  25. toastSty: {
  26. type: String,
  27. default: '',
  28. },
  29. },
  30. });
  31. </script>
  32. <style lang="scss" scoped>
  33. .vue-toast {
  34. position: fixed;
  35. box-sizing: border-box;
  36. left: 50%;
  37. top: 50%;
  38. transform: translate(-50%, -50%);
  39. width: 100px;
  40. height: 100px;
  41. padding: 15px;
  42. text-align: center;
  43. background: rgba(0, 0, 0, 0.8);
  44. border-radius: 10px;
  45. display: flex;
  46. justify-content: center;
  47. align-items: center;
  48. span {
  49. color: #fff;
  50. font-size: 16px;
  51. line-height: 25px;
  52. }
  53. }
  54. </style>

3、使用

main.js 中

  1. import Toast from './components/MyToast/index.js';
  2. Vue.use(Toast);
  1. this.$mytoast({
  2. text: '请填写完整信息并上传设备图片',
  3. duration: 1000,
  4. toastSty: 'width:157px;height:141px;'
  5. })

4、参考

Vue插件开发