微信小程序訪問html頁面是通過小程序中的WebView作為網頁承載容器實現的,通過配置WebView的src屬性進行html頁面的加載;當html返回小程序需要引用小程序類庫,調用指定接口進行跳轉。
目前小程序與html交互,都是通過跳轉鏈接的參數拼接進行數據的傳遞的。
1、小程序跳轉到html,需要配置跳轉地址到指定的域名白名單內。
2、在白名單配置的域名服務器根目錄下添加簽名文件,文件名如:mAPifgWWWz.txt ,內容如:e1756a5045b2504b499cc1cce1d2c605
3、在頁面內添加webView組件,配置地址:
index.wxml
index.js data: {urlPath:'https://visualstreet.cn/website/index.html?path=web-view&mytest=showWebView' },
當小程序調用html時,可以在WebView的src中寫入html地址;
eg:https://res.wx.qq.com/index.html;
當小程序調用html希望是登錄狀態時,需要將登錄信息以參數形式傳遞給html;
eg:https://res.wx.qq.com/index.html?user=111&unionId=222;
html頁面window.onload將觸發進行參數的獲取:
window.onload = function() { console.log(document.location); console.log(window.location); document.getElementById("txtMsg").innerText=document.location.href; document.getElementById("txtDetail").innerText=window.__wxjs_environment;}
并且可以通過
window.__wxjs_environment
得到小程序的來源
webView內html頁面可以跳轉回指定的小程序頁面,也就是html頁面可以經過一系列操作,將結果參數返回給小程序即可;
使用html返回小程序需要在返回的html頁面引用小程序類
<script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.3.2.js"></script>
調用小程序類中的指定接口進行返回
wx.miniProgram.navigateTo( )wx.miniProgram.navigateBack( )wx.miniProgram.switchTab( )wx.miniProgram.reLaunch( )wx.miniProgram.redirectTo( )wx.miniProgram.postMessage( )wx.miniProgram.getEnv( )
當html 跳轉回小程序時,可以通過到小程序指定router在該頁面獲取參數信息;
eg: wx.miniProgram.navigateTo( 'login/index?htmlvalue=12dasf&htmlvalue2=231')
新聞熱點
疑難解答