uni-app 小程序全局挂载分享功能,并动态配置页面是否可以分享

  1. 基于uni-app 小程序全局挂载 onShareTimeline 和 onShareAppMessage 方法
  2. 动态设置 onShareTimeline 和 onShareAppMessage 方法

1. 创建一个share.js 全局混入模块

js 复制代码
// utils/share.js
export default {
  data() {
    return {
      // 计数器 奇数不能分享
      count: 0
    }
  },
  onShareTimeline(res) {
    this.count++
    return {
      title: '转发朋友圈',
      query: 'id=123',
      imageUrl: '你的图片',
      promise: new Promise((resolve, reject) => {
        // 判断是否进行分享 偶数可以分享
        if (this.count % 2 === 0) {
          resolve({
            title: `转发朋友圈${this.count}`,
            query: 'id=123',
            imageUrl: '你的图片',
          })
        } else {
          reject()
          uni.showToast({
            title: `奇数不允许分享${this.count}`,
            icon: 'none'
          })
        }
      })
    }
  },
  onShareAppMessage(e) {
    this.count++
    return {
      title: '转发给好友',
      path: '/pages/main/main',
      imageUrl: '你的图片',
      promise: new Promise((resolve, reject) => {
        // 判断是否进行分享
        if (this.count % 2 === 0) {
          resolve({
            title: `转发给好友${this.count}`,
            path: '/pages/main/main',
            imageUrl: '你的图片',
          })
        } else {
          reject()
          uni.showToast({
            title: `奇数不允许分享${this.count}`,
            icon: 'none'
          })
        }
      })
    }
  }
}

2. 在全局入口文件main.js全局混入

js 复制代码
import App from './App'
import { useSentry } from './utils/sentry'
import share from './utils/share'
// #ifndef VUE3
import Vue from 'vue'
Vue.config.productionTip = false
App.mpType = 'app'
const app = new Vue({
    ...App
})
app.mixin(share)// 非 vue3 项目
app.$mount()
// #endif

// #ifdef VUE3
import { createSSRApp } from 'vue'
export function createApp() {
  const app = createSSRApp(App)
  useSentry(app)
  app.mixin(share)// vue3 项目
  return {
    app
  }
}
// #endif
  • 扩展
  1. 案例中的 count 判断,可以换成当前页面的 path,再维护一个可分享页面池子,判断 path 是否在池中,决定是否能分享
  2. button 类型的分享,可以在页面中向 share.js 传递参数,实现特殊的分享内容。也可以使用 uni.$emit 等方法实现
html 复制代码
<template>
	<button open-type="share" @click="setShareInfoTomixin"></button>
</template>
<script setup lang="ts">
	import { getCurrentInstance } from 'vue'
	const { ctx } = getCurrentInstance() as any
	// setShareInfoTomixin方法必须先触发,不能写在 <button open-type="share"></button> 里面
	const setShareInfoTomixin = (index: number)=>{
    //  setShareInfo 可以是share.js文件 methods 中的任意方法
 	ctx.setShareInfo({
		title: `转发朋友圈${this.count}`,
		query: 'id=123',
		imageUrl: '你的图片',
     })
}
</script>
相关推荐
@yanyu66613 小时前
登录注册功能-明文
vue.js·springboot
滕青山17 小时前
在线PDF拆分工具核心JS实现
前端·javascript·vue.js
专科3年的修炼1 天前
uni-app移动应用开发第四章
开发语言·javascript·uni-app
光影少年1 天前
前端在页面渲染优化和组件优化经验?
前端·vue.js·react.js·前端框架
cosinmz1 天前
图片太多太乱怎么整理?分享一个我最近常用的图片转 PDF方法
经验分享·小程序·pdf
q5507071771 天前
uniapp/uniappx实现原生图片编辑涂鸦、贴图、滤镜、旋转、裁剪等
uni-app
科技互联.1 天前
2026年小程序定制市场:个性化需求激增,技术深度成竞争关键
人工智能·小程序
李白的天不白1 天前
VUE依赖配置问题
前端·javascript·vue.js
小智社群1 天前
获取贝壳新房列表
前端·javascript·vue.js
一 乐1 天前
茶叶商城|基于springboot + vue茶叶商城系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·茶叶商城系统