3
0

u-link.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <template>
  2. <text
  3. class="u-link"
  4. @tap.stop="openLink"
  5. :style="[linkStyle, addStyle(customStyle)]"
  6. >{{text}}</text>
  7. </template>
  8. <script>
  9. import { props } from './props';
  10. import { mpMixin } from '../../libs/mixin/mpMixin';
  11. import { mixin } from '../../libs/mixin/mixin';
  12. import { addStyle, addUnit, getPx, toast } from '../../libs/function/index';
  13. /**
  14. * link 超链接
  15. * @description 该组件为超链接组件,在不同平台有不同表现形式:在APP平台会通过plus环境打开内置浏览器,在小程序中把链接复制到粘贴板,同时提示信息,在H5中通过window.open打开链接。
  16. * @tutorial https://ijry.github.io/uview-plus/components/link.html
  17. * @property {String} color 文字颜色 (默认 color['u-primary'] )
  18. * @property {String | Number} fontSize 字体大小,单位px (默认 15 )
  19. * @property {Boolean} underLine 是否显示下划线 (默认 false )
  20. * @property {String} href 跳转的链接,要带上http(s)
  21. * @property {String} mpTips 各个小程序平台把链接复制到粘贴板后的提示语(默认“链接已复制,请在浏览器打开”)
  22. * @property {String} lineColor 下划线颜色,默认同color参数颜色
  23. * @property {String} text 超链接的问题,不使用slot形式传入,是因为nvue下无法修改颜色
  24. * @property {Object} customStyle 定义需要用到的外部样式
  25. *
  26. * @example <u-link href="http://www.uviewui.com">蜀道难,难于上青天</u-link>
  27. */
  28. export default {
  29. name: "u-link",
  30. mixins: [mpMixin, mixin, props],
  31. computed: {
  32. linkStyle() {
  33. const style = {
  34. color: this.color,
  35. fontSize: addUnit(this.fontSize),
  36. // line-height设置为比字体大小多2px
  37. lineHeight: addUnit(getPx(this.fontSize) + 2),
  38. textDecoration: this.underLine ? 'underline' : 'none'
  39. }
  40. // if (this.underLine) {
  41. // style.borderBottomColor = this.lineColor || this.color
  42. // style.borderBottomWidth = '1px'
  43. // }
  44. return style
  45. }
  46. },
  47. emits: ["click"],
  48. methods: {
  49. addStyle,
  50. openLink() {
  51. // #ifdef APP-PLUS
  52. plus.runtime.openURL(this.href)
  53. // #endif
  54. // #ifdef H5
  55. window.open(this.href)
  56. // #endif
  57. // #ifdef MP
  58. uni.setClipboardData({
  59. data: this.href,
  60. success: () => {
  61. uni.hideToast();
  62. this.$nextTick(() => {
  63. toast(this.mpTips);
  64. })
  65. }
  66. });
  67. // #endif
  68. this.$emit('click')
  69. }
  70. }
  71. }
  72. </script>
  73. <style lang="scss" scoped>
  74. @import "../../libs/css/components.scss";
  75. $u-link-line-height:1 !default;
  76. .u-link {
  77. /* #ifndef APP-NVUE */
  78. line-height: $u-link-line-height;
  79. /* #endif */
  80. @include flex;
  81. flex-wrap: wrap;
  82. flex: 1;
  83. }
  84. </style>