cxycode
cxycode
  • 发布:2022-10-24 18:19
  • 更新:2023-07-15 16:32
  • 阅读:446

【报Bug】uni-vue3-cli工程创建的项目provide/inject无效

分类:uni-app

产品分类: uniapp/小程序/微信

PC开发环境操作系统: Windows

PC开发环境操作系统版本号: win10 1909 专业工作站版 x64

HBuilderX类型: Alpha

HBuilderX版本号: 3.6.7

第三方开发者工具版本号: 1.06.2210192

基础库版本号: 2.26.2

项目创建方式: HBuilderX

示例代码:

c-page的内容

<template>  
    <view>c-page</view>  
    <global-config>  
        <slot></slot>  
    </global-config>  
</template>  

<script>  
    import {  
        defineComponent,  
        provide  
    } from 'vue';  

    export default defineComponent({  
        setup() {  
            provide('count1', 9999)  
            return {};  
        },  
    });  
</script>  

<style scoped></style>

global-config的内容

<template>  
    <view>global-config</view>  
    <slot></slot>  
</template>  

<script>  
import { defineComponent, provide, reactive, ref } from 'vue';  

export default defineComponent({  
    setup() {  
        const state = reactive({  
            count: 666,  
        });  
        const count = ref(777);  
        provide('state', state);  
        provide('count', count);  
        return {};  
    },  
});  
</script>  

<style scoped></style>  

使用的页面的内容

<template>  
    <c-page>  
        <view>index page</view>  
        <button>state count is {{ state.count || 0 }}</button>  
        <button>count is {{ count || 0 }}</button>  
        <button>count1 is {{ count1 || 0 }}</button>  
    </c-page>  
</template>  

<script>  
import { defineComponent, inject, watchEffect } from 'vue';  

export default defineComponent({  
    setup() {  
        const state = inject('state', {});  
        const count = inject('count', 0);  
        const count1 = inject('count1', 0);  

        watchEffect(()=>{  
            console.log('state.count', state.count);  
            console.log('count', count.value);  
        })  

        return {  
            state,  
            count,  
            count1  
        };  
    },  
});  
</script>  

<style scoped></style>

操作步骤:

1.直接在HX3.6.7-alpha中新建uni-app工程默认模板
2.将上述代码分别copy到对应的文件内进行微信小程序编译
3.小程序中无论是日志还是界面均无法获取正确的内容

预期结果:

1.界面能正确显示provide传递的内容
2.watch中日志能正常打印传递的内容

实际结果:

1.界面上无法显示正常的内容
2.日志打印全部为undefined

bug描述:

组件之间通过provide传递数据,在子集组件中或页面中无法通过inject获取到,获取的内容全部都是undefined

2022-10-24 18:19 负责人:无 分享
已邀请:
DCloud_UNI_WZF

DCloud_UNI_WZF

provide inject 是从祖先向子孙组件传递数据,而你的示例代码看起来是 子孙provide,祖先inject
请检查代码逻辑或提供测试工程

  • cxycode (作者)

    不好意思,熬夜太多码魔怔了

    2022-10-26 13:59

  • Mrchenyong

    回复 cxycode: 我现在在头条小程序平台没有效果怎么回事,子组件接收到的是undfine

    2023-07-15 16:33

  • hhyang

    能否看看这个问题? 一个月了 https://github.com/dcloudio/uni-app/issues/4386

    2023-07-15 17:02

  • cxycode (作者)

    回复 Mrchenyong: 可以弄个在线demo或者zip方便分析下代码有没有问题,如果代码没问题那就是平台兼容性

    2023-11-03 18:08

Mrchenyong

Mrchenyong - hbuilder

我现在在头条小程序平台没有效果怎么回事,子组件接收到的是undfine

  • hhyang

    这种 https://github.com/dcloudio/uni-app/issues/4386 ?没人鸟 没人修复

    2023-07-15 16:41

  • Mrchenyong

    回复 hhyang: 你也遇到了吗,你的链接我暂时打不开

    2023-07-15 16:49

  • Mrchenyong

    回复 hhyang: 看到了

    2023-07-15 17:13

要回复问题请先登录注册