uniapp编译微信小程序(关于分享)

50 阅读1分钟

项目配置

vue3 vite

首先创建一个share.js文件

import {onShareAppMessage,onShareTimeline}from '@dcloudio/uni-app'

const share = {

	// 分享给微信好友
	onShareAppMessage(res) {
		if (res.from === 'button') { // 来自页面内分享按钮
			console.log(res.target)
			return {
				title: '欢迎您使用本小程序',
				path: '',
				imageUrl: ""
			}
		} else {
			return {
				title: '欢迎您使用本小程序',
				path: '',
				imageUrl: ""
			}
		}

	},
	// 分享到朋友圈
	onShareTimeline() {

		return {
			title: '欢迎您使用本小程序',
			path: '',
			imageUrl: ""
		}
	}
};
export default share

然后在main.js中引入share.js文件

import share from '@/utils/share.js'

export function createApp() {
	const app = createSSRApp(App)
	app.mixin(share)
	app.use(pinia)
	return {
		app
	}
}
// #endif

最后在manifest.json 中配置 App模块配置 找到share(分享)勾选微信分享并配置appid 然后启动项目就可以了

图片.png

图片.png

button按钮分享

<button open-type="share">分享</button>

放在所需要使用的位置,button分享可以设置成当前页 将path设置为分享的当前页 例如:pages/index/index,不设置的换就默认
注意:在main.js中引入的时候 有些是使用的vue.minxin(share) 可能出现按钮是灰色的情况,将其挂载到app上就可以