uniapp 微信小程序全局分享功能

思路 在全局配置文件 main.js 文件中配置 全局mixin

直接上代码

分享业务逻辑

export default {
    
    
	onShareAppMessage(res) {
    
    
		return {
    
    
			path:'/pages/welcome/welcome',
			success(res) {
    
    
				uni.showToast({
    
    
					title: '分享成功'
				})
			},
			fail(res) {
    
    
				uni.showToast({
    
    
					title: '分享失败',
					icon: 'none'
				})
			}
		}
	}
}

main.js 中使用

import Vue from 'vue'
import App from './App'
import store from './store'

import share from './mixins/share.js'
import './ajax/index.js'

Vue.config.productionTip = false

Vue.prototype.$store = store
Vue.mixin(share)
App.mpType = 'app'

const app = new Vue({
    
    
	store,
	...App
})
app.$mount()

题外话

1 注意自己的文件路径

2 单个页面有定制化分享业务时 在该页面中重写就好了

例如

在home.vue中我有特定的分享业务逻辑

export default {
    
    
...
		onShareAppMessage() {
    
    
			var data = this.info
			var type = this.type
			if(type == 0){
    
    
				var path = '/pages/articleDetail/articleDetail?id=' + data.id
			} else {
    
    
				var path = '/pages/articleDetail/articleDetail?id=' + data.id+'&from=case'
			}
			
			return {
    
    
				title: data.title,
				// path: '/pages/articleDetail/articleDetail?id=' + data.id
				path
			}
		},
	}
...

猜你喜欢

转载自blog.csdn.net/hu1628299958/article/details/114693849