dcloudio / uni-app

A cross-platform framework using Vue.js
https://uniapp.dcloud.io
Apache License 2.0
40.18k stars 3.64k forks source link

vue3 的 setup script 中 onUpdated钩子在 h5端不生效 #3364

Closed likecreep closed 2 years ago

likecreep commented 2 years ago

问题描述 [问题描述:尽可能简洁清晰地把问题描述清楚] setup script 中 onUpdated钩子在 h5端不生效

复现步骤 [复现问题的步骤]

  1. 使用setup script
  2. 使用onUpdated钩子
  3. 在h5端不生效
<template>
        <text>{{test}}</text>
        <button type="default" @click="onClick">++</button>
</template>

<script setup>
    import {ref, onUpdated} from 'vue'
    const test = ref(0)

    function onClick(){
        test.value += 1
    }

    onUpdated(()=>{
        console.log('clicked')
    })
</script>

预期结果 onUpdated钩子函数的回调被执行

实际结果 onUpdated钩子函数的回调没有被执行

报错信息

[Vue warn]: Unhandled error during execution of native event handler 
 at <ResizeSensor>
at <Image>
at <View>
at <View>
at <UniLoadMore>
at <View>
at <SwiperItem>
at <Swiper>
at <View>
at <Hot>
at <AsyncComponentWrapper>
at <PageBody>
at <Page>
at <Anonymous>
at <KeepAlive>
at <RouterView>
at <Layout>
at <App>
12:29:28.891 UncaughtTypeError: Cannot read property 'offsetWidth' of null
    at http://localhost:3000/@fs/Applications/HBuilderX.app/Contents/HBuilderX/plugins/uniapp-cli-vite/node_modules/@dcloudio/uni-h5/dist/uni-h5.es.js:6274:25
    at callWithErrorHandling (http://localhost:3000/@fs/Applications/HBuilderX.app/Contents/HBuilderX/plugins/uniapp-cli-vite/node_modules/@dcloudio/uni-h5-vue/dist/vue.runtime.esm.js:1284:22)
    at callWithAsyncErrorHandling (http://localhost:3000/@fs/Applications/HBuilderX.app/Contents/HBuilderX/plugins/uniapp-cli-vite/node_modules/@dcloudio/uni-h5-vue/dist/vue.runtime.esm.js:1293:21)
    at HTMLDivElement.invoker (http://localhost:3000/@fs/Applications/HBuilderX.app/Contents/HBuilderX/plugins/uniapp-cli-vite/node_modules/@dcloudio/uni-h5-vue/dist/vue.runtime.esm.js:9394:13)

系统信息:

fxy060608 commented 2 years ago

h5端,text是 vue 的自定义组件(跟你自己写的自定义组件并无本质区别),<text>{{test}}</text> 的写法,修改test变量,仅更新text组件(此时仅会触发text组件内部的onUpdated),不会更新整个页面 你模板代码修改成下列代码,是可以触发你页面中的onUpdated

<template>
        {{test}}
        <button type="default" @click="onClick">++</button>
</template>