子组件中能否使用getDrawableContext()方法?
<template>
<view>
<view ref="myyy"></view>
</view>
</template>
<script>
export default {
name: "s-goodsView",
props: {
},
data() {
return {
};
},
mounted() {
// setTimeout(() => {
// this.drawHeader()
// }, 10000)
this.drawHeader()
},
methods: {
drawHeader() {
const refs = this.$refs['myyy'] as UniElement
let ctx = refs.getDrawableContext()
if (ctx == null) return
},
}
}
</script>
执行这段报错,
16:03:19.135 error: java.lang.ClassCastException: io.dcloud.uts.UTSArray cannot be cast to io.dcloud.uniapp.runtime.UniElement
16:03:19.135 at components/s-goodsView/s-goodsView.uvue:156:4
16:03:19.135 154| methods: {
16:03:19.136 155| drawHeader() {
16:03:19.136 156| const refs = this.$refs['myyy'] as UniElement
16:03:19.136 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
16:03:19.136 157| let ctx = refs.getDrawableContext()
16:03:19.136 158| if (ctx == null) {
mounted中做延迟也不行
2***@qq.com (作者)
是的。做了嵌套循环了。。。
const refs = this.$refs['myyy'] as Array<UniElement>
console.log(refs.length)
没问题了
2025-07-17 16:43