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>
相关推荐
2501_9159214313 小时前
傻瓜式 HTTPS 抓包,简单抓取iOS设备数据
android·网络协议·ios·小程序·https·uni-app·iphone
yangzheui14 小时前
【VUE2转VUE3学习笔记】-Day1:模板语法
vue.js·笔记·学习
A_nanda15 小时前
c# 用VUE+elmentPlus生成简单管理系统
javascript·vue.js·c#
北极糊的狐15 小时前
若依项目vue前端启动键入npm run dev 报错:不是内部或外部命令,也不是可运行的程序或批处理文件。
前端·javascript·vue.js
yqcoder15 小时前
uni-app 之 设置导航
uni-app
2501_9159184116 小时前
把 iOS 性能监控融入日常开发与测试流程的做法
android·ios·小程序·https·uni-app·iphone·webview
jiayong2316 小时前
Vue2 与 Vue3 核心原理对比 - 面试宝典
vue.js·面试·职场和发展
有诺千金16 小时前
VUE3入门很简单(4)---组件通信(props)
前端·javascript·vue.js
2501_9447114316 小时前
Vue-路由懒加载与组件懒加载
前端·javascript·vue.js
2601_9498049216 小时前
开源多商户商城源码最新版_适配微信小程序+H5+APP+PC多端
微信小程序·小程序