m***@163.com
m***@163.com
  • 发布:2025-04-10 16:59
  • 更新:2025-04-10 17:57
  • 阅读:68

微信小程序vue3 ref总是获取不到实例,加载不到页面

分类:uni-app

/**

  • 绘制海报成功的回调函数
  • @param {string} e 生成的海报图片数据URL
    */
    const painterSsuccess = (e) => {
    console.log('海报生成成功:', e)
    posterIsShow.value = true
    pictureImage.value = e
    uni.hideLoading()
    }

// 渲染海报的方法
const renderPoster = () => {
nextTick(() => {
if (posterRef.value) {
posterRef.value.render(posterJson.value)
} else {
console.error('posterRef.value 未正确初始化', posterRef.value)
}
})
}

onLoad(() => {
uni.showLoading({
title: '正在生成海报',
icon: 'loading',
})

// 延迟执行以确保组件已挂载
setTimeout(() => {
renderPoster()
}, 3000)
})

2025-04-10 16:59 负责人:无 分享
已邀请:
DCloud_UNI_JBB

DCloud_UNI_JBB

可以发个示例项目代码吗

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

    <template>

    <!-- 显示图像的元素 -->

    <image :src="pictureImage" v-if="pictureImage" mode="widthFix" style="width: 700rpx"></image>

    <l-painter

    ref="posterRef"

    @success="painterSsuccess"

    @fail="fail"

    isCanvasToTempFilePath

    performance

    path-type="url"

    custom-style="position: fixed; left: 200%"

    >

    </l-painter>

    </template>


    <script setup>

    import { ref, onMounted, nextTick } from 'vue'

    import { onLoad } from '@dcloudio/uni-app'


    // 海报元素的引用,用于后续操作DOM

    const posterRef = ref(null)


    // 控制海报是否显示

    const posterIsShow = ref(false)


    // 存储最终生成的海报图片URL

    const pictureImage = ref('')


    // 海报的JSON配置

    const posterJson = ref({

    css: {

    width: '750rpx',

    paddingBottom: '40rpx',

    background: 'linear-gradient(,#000 0%, #ff5000 100%)',

    },

    views: [

    // ... 其他视图配置 ...

    ],

    })


    /**



    • 绘制海报成功的回调函数

    • @param {string} e 生成的海报图片数据URL

      */

      const painterSsuccess = (e) => {

      console.log('海报生成成功:', e)

      posterIsShow.value = true

      pictureImage.value = e

      uni.hideLoading()

      }


    // 渲染海报的方法

    const renderPoster = () => {

    nextTick(() => {

    if (posterRef.value) {

    posterRef.value.render(posterJson.value)

    } else {

    console.error('posterRef.value 未正确初始化', posterRef.value)

    }

    })

    }


    onLoad(() => {

    uni.showLoading({

    title: '正在生成海报',

    icon: 'loading',

    })


    // 延迟执行以确保组件已挂载

    setTimeout(() => {

    renderPoster()

    }, 3000)

    })

    </script>


    <style scoped></style>

    2025-04-10 17:12

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

    这是完整代码

    2025-04-10 17:12

  • DCloud_UNI_JBB

    回复 m***@163.com: 可以发个压缩包吗?

    2025-04-10 17:39

DCloud_UNI_JBB

DCloud_UNI_JBB

三秒这个时间也不一定能保证一定就渲染完了,应该找找有没有相关事件,渲染结束之后去隐藏loading,类似于这个文章,https://juejin.cn/post/6938574522673004581

要回复问题请先登录注册