u-link.vue 2.6 KB

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