1. 程式人生 > 實用技巧 >uniAPP微信小程式通過webview跳轉到其他網址

uniAPP微信小程式通過webview跳轉到其他網址

注意:目前通過webview跳轉到其他網址支援:
1、與微信小程式繫結的微信公眾號文章地址;
2、在微信小程式後臺配置好業務域名的地址。

1、新建一個只有webview元件的頁面

linkOthers:

<template>
	<view>
		<view>
			<!-- url為要跳轉外鏈的地址-->
		    <web-view :src="url">
		    </web-view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				url:""
			}
		},
		onLoad(val) {
			this.url = decodeURIComponent(val.url);
			// 設定當前的title 如果外鏈中有的話將被覆蓋
			if(this.isNotEmpty(val.title)){
				this.setTitle(val.title);
			}
		},
		methods: {
			isNotEmpty(obj) {
				if (typeof obj == undefined || obj == null || obj == "" || obj == "undefined" || obj.length == 0) {
					return false;
				} else {
					return true;
				}
			},
			// 設定title
			setTitle(title) {
				uni.setNavigationBarTitle({
					title: title
				})
			},
		}
	}
</script>

  

二、觸發跳轉

<template>
	<view>
		<button @click="linkOthers()"></button>
	</view>
</template>
<script>
	export default {
		methods: {
			linkOthers(){
				// 此處填寫你要跳轉的繫結公眾號文章的連結地址或者已經在小程式後臺配置好業務域名的地址
				var url = "https://www.baidu.com";	
				uni.navigateTo({
					// 此處的連結為小程式上面新建的webview頁面路徑,引數url為要跳轉外鏈的地址
					url:"/pages/common/linkOthers/linkOthers?url=" + encodeURIComponent(url)
				});
			}
		}
	}
</script>

  來源:https://blog.csdn.net/weixin_44379204/article/details/106648979?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1.control