1***@163.com
1***@163.com
  • 发布:2024-03-31 15:34
  • 更新:2024-03-31 20:31
  • 阅读:542

uniapp在app平台使用evalJS执行html中的js函数提示函数未定义

分类:uni-app

uniapp可以获取修改html的js中定义的变量并可以修改,但是无法执行里面定义函数。

下面是在Html的js中定义了一个函数 uniMsg(),但是在uniapp是无法调用这个函数,

Uncaught ReferenceError: uniMsg is not defined

代码如下:

<template>  
    <view class="qrt_root">  

        <web-view src="/static/html/index.html"></web-view>  

    </view>  
</template>  

<script setup>  
    import { onMounted } from 'vue';  
    onMounted(() => {  
        // 获取dom元素  
        let pages = getCurrentPages();  
        let page = pages[pages.length - 1];  
        let wv = page.$getAppWebview();  

        setTimeout(() => {  
            wv.evalJS("uniMsg('uniapp message!');");  
        }, 1000)  
    });  
</script>

html文件

<!DOCTYPE html>  
<html lang="en">  
    <head>  
        <meta name="viewport" content="width=device-width, initial-scale=1.0">  
    </head>  

    <body>  
        <h3>html页面</h3>  
        <div class="msg_box"></div>  
    </body>  

    <script>  
        let el = document.getElementsByClassName('msg_box')[0];  
        let uniMsg = val => {  
            el.innerHTML = val;  
        }  
        /* 全局定义无效  
        window.uniMsg = val => {  
            el.innerHTML = val;  
        }  
        */  
    </script>  

</html>
2024-03-31 15:34 负责人:无 分享
已邀请:
喜欢技术的前端

喜欢技术的前端 - QQ---445849201

改一下写法

<script setup>  
    import {onMounted,getCurrentInstance} from 'vue';  
    var wv; //计划创建的webview  
    const {ctx} = getCurrentInstance()  
    onMounted(() => {  
        setTimeout(() => {  
            var currentWebview = ctx.$scope.$getAppWebview()  
            wv = currentWebview.children()[0]  
            wv.evalJS("uniMsg()")  
        }, 1000)  
    });  
</script>

要回复问题请先登录注册