Skip to content

Commit b889153

Browse files
Auto-update blog content
1 parent 3a2bb2d commit b889153

File tree

69 files changed

+20806
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+20806
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
---
2+
layout: post
3+
title: "微信小程序全局配置分享功能"
4+
date: 2025-11-04T11:00:02+0800
5+
description: "全局配置分享功能"
6+
keywords: "微信小程序设置所有的页面都能分享"
7+
categories: ['未分类']
8+
tags: ['微信小程序', '小程序']
9+
artid: "154385338"
10+
arturl: "https://blog.csdn.net/Avengerrr/article/details/154385338"
11+
image:
12+
path: https://api.vvhan.com/api/bing?rand=sj&artid=154385338
13+
alt: "微信小程序全局配置分享功能"
14+
render_with_liquid: false
15+
featuredImage: https://bing.ee123.net/img/rand?artid=154385338
16+
featuredImagePreview: https://bing.ee123.net/img/rand?artid=154385338
17+
cover: https://bing.ee123.net/img/rand?artid=154385338
18+
img: https://bing.ee123.net/img/rand?artid=154385338
19+
---
20+
21+
22+
23+
# 微信小程序全局配置分享功能
24+
25+
26+
27+
### 技术栈:Taro+vue3
28+
29+
### 需求:所有页面均可实现分享转发功能,且指定跳转到统一页面
30+
31+
### 方法一:
32+
33+
每个页面单独配置分享转发功能
34+
35+
```javascript
36+
<script setup lang="ts">
37+
import { useShareAppMessage } from '@tarojs/taro'
38+
39+
// 每个vue文件页面配置 enableShareAppMessage
40+
definePageConfig({
41+
navigationBarTitleText: '首页',
42+
enableShareAppMessage: true
43+
})
44+
45+
// 使用 useShareAppMessage 钩子来自定义分享内容,并指定跳转路径为首页。
46+
useShareAppMessage((res) => {
47+
console.log('share', res)
48+
return {
49+
title: '欢迎访问我们的小程序', // 自定义分享标题
50+
path: '/pages/index/index' // 指定跳转到首页
51+
}
52+
})
53+
</script>
54+
55+
```
56+
57+
### 方法二:
58+
59+
进行全局配置
60+
61+
**注意: Taro 没有直接提供全局设置分享内容的方法**
62+
可以通过覆盖 Taro 的 Page 函数,为每个页面添加全局默认的分享配置。这样,所有页面都会自动具备相同的分享功能,而无需在每个页面单独配置。
63+
64+
```javascript
65+
// app.ts
66+
const app = createApp({
67+
//@ts-ignore
68+
// 先保存原生Page函数
69+
const originPage = Page
70+
// @ts-ignore
71+
// 通过直接赋值方式,覆盖Taro的Page函数
72+
Page = (pageConfig)=>{
73+
const globalShareConfig = {
74+
// 设置全局默认分享
75+
onShareAppMessage:(res:any) => {
76+
if(res.from === 'button') {
77+
console.log(res.target)
78+
}
79+
return {
80+
path: 'pages/index/index'
81+
}
82+
}
83+
}
84+
// 全局默认的分享配置 globalShareConfig 和页面的原始配置 pageConfig 合并到一个新的对象 newPageConfig
85+
const newPageConfig = Object.assign({},globalShareConfig,pageConfig)
86+
// 使用原始Page函数创建页面实例
87+
return originPage(newPageConfig);
88+
}
89+
})
90+
91+
```
92+
93+
94+

0 commit comments

Comments
 (0)