uniapp框架自定义组件

以新组件ulink为例;

步骤:

1. components文件夹下创建新组件ulink.vue;

<template name="ulink">
	<text style="text-decoration:underline" :href="href" @click="openURL" :inWhiteList="inWhiteList">{{text}}</text>
</template>

<script>
	/**
	 * @description u-link是一个外部网页超链接组件,在小程序内打开内部web-view组件或复制url,在app内打开外部浏览器,在h5端打开新网页
	 * @property {String} href 点击后打开的外部网页url,小程序中必须以https://开头
	 * @property {String} text 显示的文字
	 * @property {Boolean} inWhiteList 是否在小程序白名单中,如果在的话,在小程序端会直接打开内置web-view,否则会只会复制url,提示在外部打开
	 * @example * <u-link href="https://ext.dcloud.net.cn" text="https://ext.dcloud.net.cn" :inWhiteList="true"></u-link>
	 */
	export default {
		name: 'u-link',
		props: {
			href: {
				type: String,
				default: ''
			},
			text: {
				type: String,
				default: ''
			},
			inWhiteList: {
				type: Boolean,
				default: false
			}
		},
        //组件生命周期
        created:function(e){
        },
		methods: {
			openURL() {
				// #ifdef APP-PLUS
				plus.runtime.openURL(this.href) //这里默认使用外部浏览器打开而不是内部web-view组件打开
				// #endif
				// #ifdef H5
				window.open(this.href)
				// #endif
				// #ifdef MP
				if (this.inWhiteList) { //如果在小程序的网址白名单中,会走内置webview打开,否则会复制网址提示在外部浏览器打开
					uni.navigateTo({
						url: '/pages/Aa/web-view/web-view?url=' + this.href
					});
				} else {
					uni.setClipboardData({
						data: this.href
					});
					uni.showModal({
						content: '本网址无法直接在小程序内打开。已自动复制网址,请在手机浏览器里粘贴该网址',
						showCancel: false
					});
				}
				// #endif
			}
		}
	}
</script>

<style>

</style>

  2. index.vue页面引入新组件ulink:

<u-link class="hello-link" :href="'https://uniapp.dcloud.io/component/'" :text="'点击进去详情页'" :inWhiteList="true"></u-link>
<script>
	import uLink from '@/components/uLink.vue'
	export default {
	    components: {
	        uLink
	    },
      ....
    }
</script>

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值