uni-app/vue项目如何封装全局消息提示组件

效果图:

第一步:封装组件和方法,采用插件式注册!

在项目目录下新建components文件夹,里面放两个文件,分别是index.vue和index.js.

index.vue:

<template>
  <div class="toast" v-if="isShow">
    {{ message }}
  </div>
</template>

<script>
export default {
  name: 'AllToast',
  props: {
    isShow: {
      type: Boolean,
      required: true,
      default: false
    },
    message: {
      type: String,
      required: true,
      default: ''
    }
  },
  data() {
    return {};
  }
}
</script>

<style scoped>
.toast {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 9999;
  width: 300rpx;
  height: 100rpx;
  background-color: red;
  display: flex;
  justify-content: center;
  align-items: center;
  color: #fff;
  border-radius: 10rpx;
}
</style>

index.js:

import Vue from 'vue'
import AllToast from './index.vue'

const ToastConstructor = Vue.extend(AllToast)

function showToast(message) {
  const instance = new ToastConstructor({
    el: document.createElement('view'),
    propsData: {
      isShow: true,
      message: message
    }
  })
  document.body.appendChild(instance.$el)

  setTimeout(() => {
    instance.isShow = false
    document.body.removeChild(instance.$el)
  }, 3000) // 3秒后自动隐藏
}

export default {
  install: function (vue) {
    vue.component('toast', AllToast)
    vue.prototype.$showToast = showToast
  }
}

第二步:全局挂载

在main.js中引入和使用

import App from './App'
import uView from '@/uni_modules/uview-ui'
// 引入全局组件
import Mycomponent from '@/components/index.js'

// #ifndef VUE3
import Vue from 'vue'
Vue.use(uView)
// 挂载组件
Vue.use(Mycomponent)
import './uni.promisify.adaptor'
Vue.config.productionTip = false
App.mpType = 'app'
const app = new Vue({
  ...App
})
app.$mount()
// 测试使用
Vue.prototype.$showToast('请求失败');
// #endif

// #ifdef VUE3
import { createSSRApp } from 'vue'
export function createApp() {
  const app = createSSRApp(App)
  return {
    app
  }
}
// #endif

第三步:使用方法

vue页面使用

this.$showToast('我是全局组件菜鸡')

其他页面使用

//对于this指向不是vue的需要先引入vue
import Vue from 'vue'

//然后调用原型方法
Vue.prototype.$showToast('请求失败');

同理 这个方法也适用于Vue项目不止是uni

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-11 09:26:03       7 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-11 09:26:03       8 阅读
  3. 在Django里面运行非项目文件

    2024-07-11 09:26:03       6 阅读
  4. Python语言-面向对象

    2024-07-11 09:26:03       10 阅读

热门阅读

  1. .NET 9 预览版 5 发布

    2024-07-11 09:26:03       11 阅读
  2. 【Android12】第三方APP开机自启

    2024-07-11 09:26:03       12 阅读
  3. 深入理解CSS中的透明效果实现

    2024-07-11 09:26:03       8 阅读
  4. mac查看31295端口被占

    2024-07-11 09:26:03       7 阅读
  5. 简述框架和函数库的区别

    2024-07-11 09:26:03       10 阅读
  6. WPF自定义模板--ToggleButton

    2024-07-11 09:26:03       9 阅读
  7. pc安装python opencv

    2024-07-11 09:26:03       8 阅读
  8. Linux关于数据库,群集,缓存加速等精捡面试题

    2024-07-11 09:26:03       6 阅读
  9. 【AI应用探讨】—多智能体系统(MAS)应用场景

    2024-07-11 09:26:03       12 阅读
  10. 探索GraphQL的迷宫:Postman中测试指南

    2024-07-11 09:26:03       13 阅读