收藏
回答

给web-view设置了ref,但是无法获取到ref里面的值?

<template>  
    <view>  
        <web-view ref="webDiv" src="https://www.baidu.com"></web-view>  
        <audio ref="audiod" style="text-align: left" src="https://web-ext-storage.dcloud.net.cn/uni-app/ForElise.mp3"  
            controls></audio>  

        <image ref="asda3434333" style="width: 200px; height: 200px; background-color: #eeeeee;"  
            src="https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/shuijiao.jpg"></image>  

    </view>  
</template>  

<script>  
    export default {  
        data() {  
            return {  
                title: 'Hello'  
            }  
        },  
        onLoad() {  
            console.log(this.$refs.audiod)  
            console.log(this)  
        },  
        mounted() {  
            // 也可以在 mounted 生命周期里获取引用  
            console.log(this.$refs.audiod)  
            console.log(this)  
        },  

        methods: {  
            // webview向外部发送消息  
            handlePostMessage: function(data) {  
                console.log("接收到消息:" + JSON.stringify(data.detail));  
            },  
            // 调用 webview 内部逻辑  
            evalJs: function() {  
                this.$refs.webview.evalJs("document.body.style.background ='#00FF00'");  
            }  
        }  
    }  
</script>  

<style>  

</style>


回答关注问题邀请回答
收藏

2 个回答

  • Y.
    Y.
    03-28

    <web-view> 的 ref 无法直接获取

    03-28
    有用
    回复
  • 揪一口布丁🍮
    揪一口布丁🍮
    03-28

    <template>

        <view>

            <!-- web-view 组件 -->

            <web-view ref="webDiv" src="https://www.baidu.com"></web-view>

            

            <!-- audio 组件 -->

            <audio ref="audiod" style="text-align: left" src="https://web-ext-storage.dcloud.net.cn/uni-app/ForElise.mp3" controls></audio>


            <!-- image 组件 -->

            <image ref="asda3434333" style="width: 200px; height: 200px; background-color: #eeeeee;" src="https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/shuijiao.jpg"></image>

        </view>

    </template>


    <script>

    export default {

        data() {

            return {

                title: 'Hello'

            };

        },

        onLoad() {

            console.log(this.$refs.audiod); // 打印 audio 组件的引用

            console.log(this); // 打印组件实例

        },

        mounted() {

            // 也可以在 mounted 生命周期里获取引用

            console.log(this.$refs.audiod); // 打印 audio 组件的引用

            console.log(this); // 打印组件实例

        },

        methods: {

            // 调用 webview 内部逻辑

            evalJs: function() {

                // 确保 web-view ref 名字正确,并调用 evalJs 方法

                const webView = this.$refs.webDiv;

                if (webView) {

                    webView.evalJs("document.body.style.background ='#00FF00'");

                }

            }

        }

    };

    </script>


    <style>

    </style>

    03-28
    有用
    回复
登录 后发表内容