<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() {
console.log(this.$refs.audiod)
console.log(this)
},
methods: {
handlePostMessage: function(data) {
console.log("接收到消息:" + JSON.stringify(data.detail));
},
evalJs: function() {
this.$refs.webview.evalJs("document.body.style.background ='#00FF00'");
}
}
}
</script>
<style>
</style>
<web-view> 的 ref 无法直接获取
<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>