注意:目前通过webview跳转到其他网址支持:
1、与微信小程序绑定的微信公众号文章地址;
2、在微信小程序后台配置好业务域名的地址。
1、新建一个只有webview组件的页面
<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/webView/webView?url=' + encodeURIComponent(url) + '&title=关注公众号'
});
}
}
}
</script>