| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- /**
- * Toast 全局 Mixin
- * 在 main.js 中通过 Vue.mixin 注入到所有页面
- *
- * 使用方式:
- * - 在页面 template 中添加 <ay-toast ref="ayToast" />
- * - 在页面 methods 中使用 this.$toast.success('提示')
- */
- export default {
- methods: {
- /**
- * 获取 $toast 实例代理
- * 通过 this.$refs.ayToast 获取组件实例
- */
- $toast(options) {
- const ref = this.$refs.ayToast
- if (ref) {
- ref.show(options)
- } else {
- // 降级:通过全局事件通知
- uni.$emit('ayToast:show', typeof options === 'string' ? { type: 'text', title: options } : options)
- }
- }
- },
- computed: {
- /**
- * 提供 this.$toastRef 快捷访问
- */
- $toastRef() {
- return {
- success: (title, options = {}) => {
- const ref = this.$refs.ayToast
- if (ref) {
- ref.success(title, options)
- } else {
- uni.$emit('ayToast:show', { type: 'success', title, duration: 2000, ...options })
- }
- },
- error: (title, options = {}) => {
- const ref = this.$refs.ayToast
- if (ref) {
- ref.error(title, options)
- } else {
- uni.$emit('ayToast:show', { type: 'error', title, duration: 2500, ...options })
- }
- },
- loading: (title = '加载中...', options = {}) => {
- const ref = this.$refs.ayToast
- if (ref) {
- ref.loading(title, options)
- } else {
- uni.$emit('ayToast:show', { type: 'loading', title, duration: 0, mask: true, ...options })
- }
- },
- text: (title, options = {}) => {
- const ref = this.$refs.ayToast
- if (ref) {
- ref.show({ type: 'text', title, duration: 2000, ...options })
- } else {
- uni.$emit('ayToast:show', { type: 'text', title, duration: 2000, ...options })
- }
- },
- hide: () => {
- const ref = this.$refs.ayToast
- if (ref) {
- ref.hide()
- } else {
- uni.$emit('ayToast:hide')
- }
- }
- }
- }
- }
- }
|