今天分享一个小技巧, 教你如何一分钟内学会用 vue 实现分享功能.
H5 无法实现直接调用手机 App 并实现分享功能, 如果你想自定义分享的内容, 就必须在微信内嵌的浏览器里面调用微信的分享接口 (QQ 浏览器就要调用它的相关 API, 其他浏览器也是), 毕竟 H5 不是 App, 有一些东西还是在 App 上调用比较方便. H5 方便的也就是直接使用浏览器自带的分享功能 (把当前页面的 URL 分享出去, 分享的内容根据浏览器自身而定).
不过有个别分享是可以直接通过 URL, 自定义分享内容的
效果图:
(比较简陋, 但是能用)
image.PNG
代码如下:
分享到 QQ 分享到 QQ 空间分享到微博 exportdefault{ data() {return{}; },methods: {// 分享到 QQ 好友 (PC 端可用) shareToQQ() {// 此处分享链接内无法携带图片 constshare = {title:"东金秀财",desc:"描述",share_url:"https://xiucai.neafex.com/#/" }; location.replace("https://connect.qq.com/widget/shareqq/index.html?url="+encodeURIComponent(share.share_url) +"&title="+ share.title +"&desc="+ share.desc ); },// 分享到 QQ 空间 (可用) shareToRoom() {// 自定义内容 constshare = {title:"东金秀财",desc:"描述",image_url: ["https://xxx.jpeg"],share_url:"https:// 地址" };letimage_urls = share.image_url.map(function(image){returnencodeURIComponent(image); });// 跳转地址 location.replace("https://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url="+encodeURIComponent(share.share_url) +"&title="+ share.title +"&pics="+image_urls.join("|") +"&summary="+ share.desc ); },// 分享到微博 (可用) shareToMicroblog() {// 自定义内容 constshare = {title:"东金秀财",image_url: ["https://xxx.jpeg"],share_url:"https:// 地址" };// 跳转地址 location.replace("https://service.weibo.com/share/share.php?url="+encodeURIComponent(share.share_url) +"&title="+ share.title +"&pic="+ share.image_url +"&searchPic=true" ); } }}; 分享到 QQ 分享到 QQ 空间分享到微博 exportdefault{ data() {return{}; },methods: {// 分享到 QQ 好友 (PC 端可用) shareToQQ() {// 此处分享链接内无法携带图片 constshare = {title:"东金秀财",desc:"描述",share_url:"https://xiucai.neafex.com/#/" }; location.replace("https://connect.qq.com/widget/shareqq/index.html?url="+encodeURIComponent(share.share_url) +"&title="+ share.title +"&desc="+ share.desc ); },// 分享到 QQ 空间 (可用) shareToRoom() {// 自定义内容 constshare = {title:"东金秀财",desc:"描述",image_url: ["https://xxx.jpeg"],share_url:"https:// 地址" };letimage_urls = share.image_url.map(function(image){returnencodeURIComponent(image); });// 跳转地址 location.replace("https://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url="+encodeURIComponent(share.share_url) +"&title="+ share.title +"&pics="+image_urls.join("|") +"&summary="+ share.desc ); },// 分享到微博 (可用) shareToMicroblog() {// 自定义内容 constshare = {title:"东金秀财",image_url: ["https://xxx.jpeg"],share_url:"https:// 地址" };// 跳转地址 location.replace("https://service.weibo.com/share/share.php?url="+encodeURIComponent(share.share_url) +"&title="+ share.title +"&pic="+ share.image_url +"&searchPic=true" ); } }}; 分享到 QQ 分享到 QQ 空间分享到微博 exportdefault{ data() {return{}; },methods: {// 分享到 QQ 好友 (PC 端可用) shareToQQ() {// 此处分享链接内无法携带图片 constshare = {title:"东金秀财",desc:"描述",share_url:"https://xiucai.neafex.com/#/" }; location.replace("https://connect.qq.com/widget/shareqq/index.html?url="+encodeURIComponent(share.share_url) +"&title="+ share.title +"&desc="+ share.desc ); },// 分享到 QQ 空间 (可用) shareToRoom() {// 自定义内容 constshare = {title:"东金秀财",desc:"描述",image_url: ["https://xxx.jpeg"],share_url:"https:// 地址" };letimage_urls = share.image_url.map(function(image){returnencodeURIComponent(image); });// 跳转地址 location.replace("https://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url="+encodeURIComponent(share.share_url) +"&title="+ share.title +"&pics="+image_urls.join("|") +"&summary="+ share.desc ); },// 分享到微博 (可用) shareToMicroblog() {// 自定义内容 constshare = {title:"东金秀财",image_url: ["https://xxx.jpeg"],share_url:"https:// 地址" };// 跳转地址 location.replace("https://service.weibo.com/share/share.php?url="+encodeURIComponent(share.share_url) +"&title="+ share.title +"&pic="+ share.image_url +"&searchPic=true" ); } }}; 分享到 QQ 分享到 QQ 空间分享到微博 exportdefault{ data() {return{}; },methods: {// 分享到 QQ 好友 (PC 端可用) shareToQQ() {// 此处分享链接内无法携带图片 constshare = {title:"东金秀财",desc:"描述",share_url:"https://xiucai.neafex.com/#/" }; location.replace("https://connect.qq.com/widget/shareqq/index.html?url="+encodeURIComponent(share.share_url) +"&title="+ share.title +"&desc="+ share.desc ); },// 分享到 QQ 空间 (可用) shareToRoom() {// 自定义内容 constshare = {title:"东金秀财",desc:"描述",image_url: ["https://xxx.jpeg"],share_url:"https:// 地址" };letimage_urls = share.image_url.map(function(image){returnencodeURIComponent(image); });// 跳转地址 location.replace("https://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url="+encodeURIComponent(share.share_url) +"&title="+ share.title +"&pics="+image_urls.join("|") +"&summary="+ share.desc ); },// 分享到微博 (可用) shareToMicroblog() {// 自定义内容 constshare = {title:"东金秀财",image_url: ["https://xxx.jpeg"],share_url:"https:// 地址" };// 跳转地址 location.replace("https://service.weibo.com/share/share.php?url="+encodeURIComponent(share.share_url) +"&title="+ share.title +"&pic="+ share.image_url +"&searchPic=true" ); } }}; 欢迎 关注微信公众号: 我的 web 前端自救之路
欢迎 进微信群 和大佬们交流
分享到 QQ 分享到 QQ 空间分享到微博 exportdefault{ data() {return{}; },methods: {// 分享到 QQ 好友 (PC 端可用) shareToQQ() {// 此处分享链接内无法携带图片 constshare = {title:"东金秀财",desc:"描述",share_url:"https://xiucai.neafex.com/#/" }; location.replace("https://connect.qq.com/widget/shareqq/index.html?url="+encodeURIComponent(share.share_url) +"&title="+ share.title +"&desc="+ share.desc ); },// 分享到 QQ 空间 (可用) shareToRoom() {// 自定义内容 constshare = {title:"东金秀财",desc:"描述",image_url: ["https://xxx.jpeg"],share_url:"https:// 地址" };letimage_urls = share.image_url.map(function(image){returnencodeURIComponent(image); });// 跳转地址 location.replace("https://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url="+encodeURIComponent(share.share_url) +"&title="+ share.title +"&pics="+image_urls.join("|") +"&summary="+ share.desc ); },// 分享到微博 (可用) shareToMicroblog() {// 自定义内容 constshare = {title:"东金秀财",image_url: ["https://xxx.jpeg"],share_url:"https:// 地址" };// 跳转地址 location.replace("https://service.weibo.com/share/share.php?url="+encodeURIComponent(share.share_url) +"&title="+ share.title +"&pic="+ share.image_url +"&searchPic=true" ); } }};
来源: http://www.jianshu.com/p/b34654a34b2c