需求:文章添加分享功能(包括微信、微博、QQ空间)

如下图所示:

点击图标分别跳转到如下界面:(实现效果如下

话不多说直接代码(可以封装成组件)

<template>
<div class="shareBox">
分享到:
<span class="hover-pointer" @click="shareToMicroblog()">
<img src="@/assets/img/weibo.png" alt="" />
</span>
<span class="qqIcon hover-pointer" @click="shareToQQRom()">
<img src="@/assets/img/QQKJ.png" alt="" />
</span>
<span class="hover-pointer" @click="shareToWeChat()">
<img src="@/assets/img/weixin.png" alt="" />
</span>
</div>
</template> <script>
export default {
props: ["sysInfo"], //父组件传过来的文章数据
data() {
return {
shareUrl: location.href,
};
},
methods: {
/**
* 分享到微博
*/
shareToMicroblog() {
//跳转地址
window.open(
"https://service.weibo.com/share/share.php?url=" +
encodeURIComponent(this.shareUrl) +
"&title=" +
this.sysInfo
);
},
/**
* 分享到qq空间
*/
shareToQQRom() {
//跳转地址
window.open(
"https://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=" +
encodeURIComponent(this.shareUrl) +
"&title=" +
this.sysInfo +
"&summary=" +
this.sysInfo
);
},
/**
* 分享到微信:需要单独画页面
*/
shareToWeChat() {
localStorage.setItem("shareUrl", this.shareUrl); // 存储当前页面地址,前端页面直接根据URL链接生成二维码
let url = location.href.split("#")[0] + "#/Share/toWechat"; // 注意:#/Share/toWechat: 自己定义的路由
window.open(url);
},
},
};
</script> <style lang="scss" scoped>
.shareBox {
.iconfont {
font-size: 22px !important;
}
.qqIcon {
display: inline-block;
margin: 0 10px;
}
}
</style>

注意:

1.微信分享需要加另外画页面 加前端页面根据URL链接生成二维码功能

参考:Vue中qrcode的使用方法(生成二维码插件) / 前端页面根据URL链接生成二维码 - 微微一笑绝绝子 - 博客园 (cnblogs.com)

2.橙色字体需要换成你自己定义的变量(包括图片 路由  props中的值 )

3.注意微信分享页面的路由设置,最好放在根路由中 (如下图 )并同步到代码中

完结撒花!赶快试试吧

作者:微微一笑绝绝子
出处:https://www.cnblogs.com/wwyxjjz/p/16922560.html
本博客文章均为作者原创,转载请注明作者和原文链接。

最新文章

  1. 【leetcode】First Missing Positive(hard) ☆
  2. 【CRL in c#】常量与字段
  3. 从客户端中检测到有潜在危险的 Request.Form 值。
  4. jquery之 animate()方法详解
  5. C++实现python标准库中的Counter
  6. oracle通过DBlink连接mysql(MariaDB)
  7. 看来ms sql server if 中定义个变量出了if 还是可以用的
  8. 图片放大镜(像淘宝浏览商品一样)JS操作
  9. ROS中Mangle解析
  10. logstash 处理tomcat access报ArgumentError: comparison of String with 5 failed
  11. mongodb进阶一之高级查询
  12. Axure RP 实践.1
  13. kafka Network
  14. 使用命令行编译QT helloworld 项目
  15. MyBatis 别名标签 &amp; sql的复用
  16. text-align: justify;浏览器、安卓手机不兼容问题
  17. Elasticsearch相关概念了解
  18. ls 大全
  19. Python概述
  20. MongoDB基本语句

热门文章

  1. CVE-2020-13933
  2. 基于APIView写接口
  3. LeetCode_单周赛_332
  4. Nginx 安装perl
  5. 【雅礼联考DAY02】Magic
  6. JZOJ 5348. 【NOIP2017提高A组模拟9.5】心灵治愈
  7. JZOJ 5346. 【NOIP2017提高A组模拟9.5】NYG的背包
  8. redis(10)事务和锁机制秒杀
  9. Hbase学习三:Hbase常用命令总结
  10. 重新配置 Idea Webapp 部署