本文為大家分享了vue微信分享插件的使用方法,供大家參考,具體內(nèi)容如下
1.安裝weixin-js-sdk
npm install weixin-js-sdk
2.創(chuàng)建文件并引入
在src下創(chuàng)建common目錄 在common目錄下創(chuàng)建wxshare.js3.在wxshare.js中編寫插件
import wx from 'weixin-js-sdk'import URL from '@/common/urlConfig'export const shareTitle = '測試';export const shareUrl = '測試連接';export const shareImg = '測試圖片';export const shareDesc = '測試詳情';/** *分享 * @param _this * @param shareTitle 標(biāo)題 * @param shareUrl 鏈接 * @param shareImg 圖片 * @param shareDesc 描述 */export const commonShare = (_this, shareTitle, shareUrl, shareImg, shareDesc) => { let url = window.location.href; let data = { url: url }; _this.$axios.post(URL.vip.insertApplyRecord, data).then(res => { if (res.status == 1){ let data = res.data wx.config({ debug: false, // 開啟調(diào)試模式,調(diào)用的所有api的返回值會在客戶端alert出來,若要查看傳入的參數(shù),可以在pc端打開,參數(shù)信息會通過log打出,僅在pc端時才會打印。 appId: data.appId, // 必填,公眾號的唯一標(biāo)識 timestamp: data.timestamp, // 必填,生成簽名的時間戳 nonceStr: data.nonceStr, // 必填,生成簽名的隨機(jī)串 signature: data.signature, // 必填,簽名,見附錄1 jsApiList: ["onMenuShareTimeline", "onMenuShareAppMessage"] // 必填,需要使用的JS接口列表,所有JS接口列表見附錄2 }); wx.ready(function () { wx.onMenuShareTimeline({ title: shareTitle, // 分享標(biāo)題 link: shareUrl, // 分享鏈接,該鏈接域名或路徑必須與當(dāng)前頁面對應(yīng)的公眾號JS安全域名一致 imgUrl: shareImg, // 分享圖標(biāo) success: function () { // 用戶確認(rèn)分享后執(zhí)行的回調(diào)函數(shù) // alert('分享成功!!!'); }, cancel: function () { // 用戶取消分享后執(zhí)行的回調(diào)函數(shù) // alert('取消分享!!!'); } }); wx.onMenuShareAppMessage({ title: shareTitle, // 分享標(biāo)題 desc: shareDesc, // 分享描述 link: shareUrl, // 分享鏈接,該鏈接域名或路徑必須與當(dāng)前頁面對應(yīng)的公眾號JS安全域名一致 imgUrl: shareImg, // 分享圖標(biāo) type: "", // 分享類型,music、video或link,不填默認(rèn)為link dataUrl: "", // 如果type是music或video,則要提供數(shù)據(jù)鏈接,默認(rèn)為空 success: function () { // 用戶確認(rèn)分享后執(zhí)行的回調(diào)函數(shù) // alert('分享成功!!!'); }, cancel: function () { // 用戶取消分享后執(zhí)行的回調(diào)函數(shù) // alert('取消分享!!!'); } }); }); } })};4.在需要分享頁面編寫
import {commonShare, shareTitle, shareUrl, shareImg, shareDesc} from "@/common/wxshare"; commonShare(this, shareTitle, shareUrl, shareImg, shareDesc);以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持錯新站長站。
新聞熱點(diǎn)
疑難解答