当 运行 我的 VodaPay 小程序 web-view 应用程序从浏览器访问时,我无法访问我的图书馆

I cannot access the my library when running my VodaPay Mini-Program web-view app from a browser

我正忙于使用 VodaPay 小程序开发一个小程序,并在 H5 网页视图中呈现我的网页。

我已经导入了 CDN,它在模拟器中正常工作,而 运行 在设备上的 VodaPay 超级应用程序中。但是当 运行 在设备上时,我收到以下错误:

my is not defined

如何防止这种情况发生?为什么会出现这个问题?

<script type="text/javascript" src="https://appx/web-view.min.js"></script>
<script>

  // Sends message to VodaPay Mini-Program.
  my.postMessage({name:"test web-view"});

  // Did receive message from VodaPay Mini-Program.
  my.onMessage = function(e) {
    console.log(e); // {'sendToWebView': '1'}
  }
</script>

当您的迷你应用程序 运行 在设备或模拟器中运行时,Web-View cdn 只会 运行。因此有必要首先检查 my.库可用,以便确定它 运行在小程序上下文中。

<!-- MINI: Used to communicate with mini app. Have to include the script body here otherwise we get my. not recognized errors <START>-->
<script src="https://appx/web-view.min.js"></script>
<script id="messageSend">
    function sendMessage(data) {
        //If not in mini app webview then my. throws errors
        try {
            my.postMessage(data);
        } catch(error) {
        //Prevent my. from throwing error in browser
        }
    }
</script>

<script id="messageReceive">
    try {
        //Store message in session storage, and then change messageReceiveListener innerText to trigger a mutation event to notify that a message has arrived.
        my.onMessage = function (e) {
            sessionStorage.setItem('message',JSON.stringify(e)); 
            document.getElementById('messageReceiveListener').innerText = e; 
        }
        sessionStorage.setItem('inMiniProgram',true);
    } catch(error){          //Prevent my. from throwing error in browser          
            sessionStorage.setItem('inMiniProgram',false);        
    }      
</script>
<input id="messageReceiveListener" hidden></input>
 <!-- MINI:<END> -->