1***@163.com
1***@163.com
  • 发布:2024-01-17 15:22
  • 更新:2024-05-09 12:59
  • 阅读:186

uniapp的webview

分类:uni-app

5和app都是uni开发的,在app里用webview打开h5,在h5用uni.postmessage()说 uni.postMessage is not a function,用uni.webview.postmessage说TypeError: Cannot read property 'postMessage' of undefined,是因为都是uniapp开发的不能通信吗?

2024-01-17 15:22 负责人:无 分享
已邀请:
BoredApe

BoredApe - 有问题就会有答案。

uni.webView

  • 1***@163.com (作者)

    这个也是说ypeError: Cannot read property 'postMessage' of undefined 我是在index.html引入的js文件,不是在.vue文件中引入的,和我放的位置有关系吗?

    2024-01-17 15:27

  • BoredApe

    回复 1***@163.com: 在index.html中引入webview.js。你的代码大小写有问题。uni.webView.postMessage() V是大写

    2024-01-17 15:29

  • 1***@163.com (作者)

    回复 BoredApe: 大写也是这个错,您看一下我下面的代码片段

    2024-01-17 15:40

1***@163.com

1***@163.com (作者) - 不会写代码


  <script type="text/javascript" src="https://gitcode.net/dcloud/hello-uni-app-x/-/blob/alpha/hybrid/html/uni.webview.1.5.5.js"></script>  
  <script>  
<script>  
        document.addEventListener('UniAppJSBridgeReady', function() {  
            uni.webView.getEnv(function(res) {  
                console.log('当前环境:' + JSON.stringify(res));  
            });  
        });  
    </script>
```这么写document.addEventListener这个监听都不执行,我把webview.1.5.5.js文件复制到script 标签下会执行监听,但是就会报错Uncaught TypeError: Cannot read property 'getEnv' of undefined 
  • BoredApe

    https://gitcode.net/dcloud/hello-uni-app-x/-/blob/alpha/hybrid/html/uni.webview.1.5.5.js 用这个

    2024-01-17 15:42

  • 1***@163.com (作者)

    回复 BoredApe: 还是这个问题,您看下我的全部代码

    2024-01-17 15:52

1***@163.com

1***@163.com (作者) - 不会写代码


<!DOCTYPE html>  
<html>  
    <head>  
        <meta charset="UTF-8" />  
        <script>  
            var coverSupport = 'CSS' in window && typeof CSS.supports === 'function' && (CSS.supports('top: env(a)') ||  
                CSS.supports('top: constant(a)'))  
            document.write(  
                '<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0' +  
                (coverSupport ? ', viewport-fit=cover' : '') + '" />')  
        </script>  

        <title></title>  
        <!--preload-links-->  
        <!--app-context-->  
    </head>  
    <body>  
        <div id="app">  
            <!--app-html-->  
        </div>  
        <script type="module" src="/src/main.ts"></script>  
    </body>  
    <script type="text/javascript"  
        src="https://gitcode.net/dcloud/hello-uni-app-x/-/blob/alpha/hybrid/html/uni.webview.1.5.5.js"></script>  
    <script>  
        document.addEventListener('UniAppJSBridgeReady', function() {  
            uni.webView.getEnv(function(res) {  
                console.log('当前环境:' + JSON.stringify(res));  
            });  
        });  
    </script>  

</html>  

```换成您的js文件,还是document.addEventListener这里都没执行,如果把js文件代码粘过去,又报错Uncaught TypeError: Cannot read property 'getEnv' of undefined at
  • BoredApe

    打印一下uni、webView是什么。能进UniAppJSBridgeReady回调说明已经加载成功了

    2024-01-17 16:03

  • 1***@163.com (作者)

    回复 BoredApe: uni是{}空对象,uni.webView是undefined

    2024-01-17 16:11

  • BoredApe

    回复 1***@163.com: 是在什么环境下运行的?这个inde.html是在webview中打开的吗

    2024-01-17 16:19

  • 1***@163.com (作者)

    回复 BoredApe: 打开的不是index.html,我的h5也是uniapp写的,所以页面在page下面.vue文件,webview打开的路径其实是page/xx/xx

    2024-01-17 16:21

w***@foxmail.com

w***@foxmail.com

都是uni的项目的话,引入的脚本和uni冲突了,改一下变量名就好了

要回复问题请先登录注册