u-link.vue 2.6 KB

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