uniapp微信分享詳解

一、uniapp微信分享音樂

1、在manifest.json文件中聲明微信appid存在;

{
    "mp-weixin": {
        "appid": "your appid"
    }
} 

2、在需要分享音樂的頁面引用uni-share模塊;

<template>
  <view>
    <button @tap="onShareTap">分享音樂</button>
  </view>
</template>

<script>
  import uniShare from '@/uni_modules/uni-share/js_sdk/uni-share.js'
  
  export default {
    methods: {
      onShareTap() {
        uniShare.shareMusic({
          title: '音樂名稱',
          desc: '音樂描述',
          musicUrl: 'http://www.example.com/music.mp3',
          imageUrl: 'http://www.example.com/cover.jpg',
          success() {
            console.log('分享成功')
          },
          fail() {
            console.log('分享失敗')
          }
        })
      }
    }
  }
</script> 

3、調用uniShare.shareMusic方法即可完成分享音樂操作。

二、uniapp微信分享h5鏈接帶描述

1、在manifest.json文件中聲明微信appid存在;

{
    "mp-weixin": {
        "appid": "your appid"
    }
} 

2、在需要分享h5鏈接的頁面引入uni-share模塊;

<template>
  <view>
    <button @tap="onShareTap">分享鏈接</button>
  </view>
</template>

<script>
  import uniShare from '@/uni_modules/uni-share/js_sdk/uni-share.js'
  
  export default {
    methods: {
      onShareTap() {
        uniShare.shareUrl({
          title: '分享標題',
          desc: '分享描述',
          href: 'http://www.example.com/page.html',
          imageUrl: 'http://www.example.com/image.jpg',
          success() {
            console.log('分享成功')
          },
          fail() {
            console.log('分享失敗')
          }
        })
      }
    }
  }
</script> 

3、調用uniShare.shareUrl方法即可完成分享h5鏈接帶描述操作。

三、uniapp微信分享鏈接

1、在manifest.json文件中聲明微信appid存在;

{
    "mp-weixin": {
        "appid": "your appid"
    }
} 

2、在需要分享鏈接的頁面引入uni-share模塊;

<template>
  <view>
    <button @tap="onShareTap">分享鏈接</button>
  </view>
</template>

<script>
  import uniShare from '@/uni_modules/uni-share/js_sdk/uni-share.js'
  
  export default {
    methods: {
      onShareTap() {
        uniShare.shareLink({
          title: '分享標題',
          url: 'http://www.example.com/page.html',
          imageUrl: 'http://www.example.com/image.jpg',
          success() {
            console.log('分享成功')
          },
          fail() {
            console.log('分享失敗')
          }
        })
      }
    }
  }
</script> 

3、調用uniShare.shareLink方法即可完成分享鏈接操作。

四、uniapp微信分享朋友圈

1、在manifest.json文件中聲明微信appid存在;

{
    "mp-weixin": {
        "appid": "your appid"
    }
} 

2、在需要分享朋友圈的頁面引入uni-share模塊;

<template>
  <view>
    <button @tap="onShareTap">分享朋友圈</button>
  </view>
</template>

<script>
  import uniShare from '@/uni_modules/uni-share/js_sdk/uni-share.js'
  
  export default {
    methods: {
      onShareTap() {
        uniShare.shareTimeline({
          title: '分享標題',
          imageUrl: 'http://www.example.com/image.jpg',
          success() {
            console.log('分享成功')
          },
          fail() {
            console.log('分享失敗')
          }
        })
      }
    }
  }
</script> 

3、調用uniShare.shareTimeline方法即可完成分享朋友圈操作。

五、uniapp微信分享不申請

uniapp微信分享不申請可通過微信開放平台網站申請分享許可權,具體操作如下:

1、登錄微信開放平台網站進入組件預授權頁面;

2、輸入要預授權的APP ID,包括Native App和H5 App,點擊「下一步」按鈕;

3、填寫申請內容,如下圖:

4、完成身份驗證後,會跳轉到預授權信息的確認頁面,確認完成後,可以在「預授權記錄」中查看已申請的預授權列表。

六、uniapp微信分享小程序頁面

1、在manifest.json文件中聲明微信appid存在;

{
    "mp-weixin": {
        "appid": "your appid"
    }
} 

2、在需要分享小程序頁面的頁面引入uni-share模塊;

<template>
  <view>
    <button @tap="onShareTap">分享小程序頁面</button>
  </view>
</template>

<script>
  import uniShare from '@/uni_modules/uni-share/js_sdk/uni-share.js'
  
  export default {
    methods: {
      onShareTap() {
        uniShare.shareMiniProgram({
          title: '分享標題',
          imageUrl: 'http://www.example.com/image.jpg',
          pagePath: 'pages/index/index',
          success() {
            console.log('分享成功')
          },
          fail() {
            console.log('分享失敗')
          }
        })
      }
    }
  }
</script> 

3、調用uniShare.shareMiniProgram方法即可完成分享小程序頁面操作。

七、uniapp微信分享調起app

1、在manifest.json文件中聲明微信appid存在;

{
    "mp-weixin": {
        "appid": "your appid"
    }
} 

2、在需要調起app的頁面引入uni-share模塊;

<template>
  <view>
    <button @tap="onShareTap">調起app</button>
  </view>
</template>

<script>
  import uniShare from '@/uni_modules/uni-share/js_sdk/uni-share.js'
  
  export default {
    methods: {
      onShareTap() {
        uniShare.shareAppMessage({
          title: '分享標題',
          imageUrl: 'http://www.example.com/image.jpg',
          path: '/pages/index/index',
          launchApp: true,
          success() {
            console.log('分享成功')
          },
          fail() {
            console.log('分享失敗')
          }
        })
      }
    }
  }
</script> 

3、調用uniShare.shareAppMessage方法即可完成調起app操作。

八、uniapp微信分享 不用申請appid

如果不想申請微信appid,也可以通過調用微信官方分享介面實現分享,具體操作如下:

1、在需要分享的頁面引入weixin-js-sdk庫;

<template>
  <view>
    <button @tap="onShareTap">分享</button>
  </view>
</template>

<script>
  import wx from 'weixin-js-sdk'
  
  export default {
    methods: {
      onShareTap() {
        wx.ready(() => {
          wx.updateAppMessageShareData({
            title: '分享標題',
            desc: '分享描述',
            link: 'http://www.example.com/page',
            imgUrl: 'http://www.example.com/image.jpg',
            success() {
              console.log('分享成功')
            },
            fail() {
              console.log('分享失敗')
            }
          })

          wx.updateTimelineShareData({
            title: '分享標題',
            link: 'http://www.example.com/page',
            imgUrl: 'http://www.example.com/image.jpg',
            success() {
              console.log('分享成功')
            },
            fail() {
              console.log('分享失敗')
            }
          })
        })
      }
    }
  }
</script> 

2、調用wx.ready方法準備就緒後,調用wx.updateAppMessageShareData和wx.updateTimelineShareData方法即可完成分享操作。

九、uniapp微信分享頁面里標題和描述

如果需要在頁面里設置分享標題和描述,可以通過在html文件里設置meta標籤實現,具體操作如下:

<template>
  <head>
    <meta name="title" content="分享標題">
    <meta name="description" content="分享描述">
  </head>
  <body>
    <button @tap="onShareTap">分享</button>
  </body>
</template>

<script>
  import wx from 'weixin-js-sdk'
  
  export default {
    methods: {
      onShareTap() {
        wx.ready(() => {
          wx.updateAppMessageShareData({
            title: document.title,
            desc: document.querySelector('meta[name="description"]').getAttribute('content'),
            link: 'http://www.example.com/page',
            imgUrl: 'http://www.example.com/image.jpg',
            success() {
              console.log('分享成功')
            },
            fail() {
              console.log('分享失敗')
            }
          })

          wx.updateTimelineShareData({
            title: document.title,
            link: 'http://www.example.com/page',
            imgUrl: 'http://www.example.com/image.jpg',
            success() {
              console.log('分享成功')
            },
            fail() {
              console.log('分享失敗')
            }
          })
        })
      }
    }
  }
</script> 

通過在head標籤里設置meta標籤,然後在分享方法里調用document.title和document.querySelector獲取相關數據即可。

以上就是關於uniapp微信分享的詳細闡述,我們通過對分享音樂、h5鏈接帶描述、鏈接、朋友圈、不申請、小程序頁面、調起app、不用申請appid、頁面里標題和描述等多個方面進行了探討,並給出了完整的代碼示例。希望對大家有所幫助。

原創文章,作者:VRTL,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/148207.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
VRTL的頭像VRTL
上一篇 2024-11-02 13:15
下一篇 2024-11-02 13:15

相關推薦

  • uniapp分頁第二次請求用法介紹

    本文將從多個方面對uniapp分頁第二次請求進行詳細闡述,並給出對應的代碼示例。 一、請求參數的構造 在進行分頁請求時,需要傳遞的參數體包含當前頁碼以及每頁顯示的數據量。對於第二次…

    編程 2025-04-27
  • Linux sync詳解

    一、sync概述 sync是Linux中一個非常重要的命令,它可以將文件系統緩存中的內容,強制寫入磁碟中。在執行sync之前,所有的文件系統更新將不會立即寫入磁碟,而是先緩存在內存…

    編程 2025-04-25
  • 神經網路代碼詳解

    神經網路作為一種人工智慧技術,被廣泛應用於語音識別、圖像識別、自然語言處理等領域。而神經網路的模型編寫,離不開代碼。本文將從多個方面詳細闡述神經網路模型編寫的代碼技術。 一、神經網…

    編程 2025-04-25
  • Python安裝OS庫詳解

    一、OS簡介 OS庫是Python標準庫的一部分,它提供了跨平台的操作系統功能,使得Python可以進行文件操作、進程管理、環境變數讀取等系統級操作。 OS庫中包含了大量的文件和目…

    編程 2025-04-25
  • Python輸入輸出詳解

    一、文件讀寫 Python中文件的讀寫操作是必不可少的基本技能之一。讀寫文件分別使用open()函數中的’r’和’w’參數,讀取文件…

    編程 2025-04-25
  • Java BigDecimal 精度詳解

    一、基礎概念 Java BigDecimal 是一個用於高精度計算的類。普通的 double 或 float 類型只能精確表示有限的數字,而對於需要高精度計算的場景,BigDeci…

    編程 2025-04-25
  • git config user.name的詳解

    一、為什麼要使用git config user.name? git是一個非常流行的分散式版本控制系統,很多程序員都會用到它。在使用git commit提交代碼時,需要記錄commi…

    編程 2025-04-25
  • MPU6050工作原理詳解

    一、什麼是MPU6050 MPU6050是一種六軸慣性感測器,能夠同時測量加速度和角速度。它由三個感測器組成:一個三軸加速度計和一個三軸陀螺儀。這個組合提供了非常精細的姿態解算,其…

    編程 2025-04-25
  • 詳解eclipse設置

    一、安裝與基礎設置 1、下載eclipse並進行安裝。 2、打開eclipse,選擇對應的工作空間路徑。 File -> Switch Workspace -> [選擇…

    編程 2025-04-25
  • Linux修改文件名命令詳解

    在Linux系統中,修改文件名是一個很常見的操作。Linux提供了多種方式來修改文件名,這篇文章將介紹Linux修改文件名的詳細操作。 一、mv命令 mv命令是Linux下的常用命…

    編程 2025-04-25

發表回復

登錄後才能評論