实现父组件调用子组件方法时报错:[Vue warn]: Invalid vnode type when creating vnode: null.

devtools/2024/9/25 7:54:40/

使用uniapp实现父组件调用子组件方法时报错:[Vue warn]: Invalid vnode type when creating vnode: null. 

实现代码如下:

子组件:

<template><view><view class="toolsHeader"><view class="toolsTitle">{{toolsName}}</view><uni-icons :type="collectType" @click="collection"></uni-icons></view></view>
</template><script setup>
import { ref,defineEmits,defineExpose } from 'vue';const emit = defineEmits(['collect','refresh'])
const collectType = ref('star');const props = defineProps({toolsName:{type: String,default: ''}
});const changeStatus = (tar, status)=>{if(tar == 'collect'){collectType.value = status == 1 ? 'star-filled' : 'star';}
}defineExpose({changeStatus
})const collection = ()=>{let toStatus = collectType.value == 'star' ? 1 : 0;emit("collect", toStatus);
}</script><style lang="scss" scoped>
</style>

父组件:

<template><view><tools-header ref="toolsHeader" toolsName="name" @collect="collectools"></tools-header></view>
</template><script setup>
import { ref } from 'vue';const toolsHeader = ref({});const collectools = (status)=>{toolsHeader.value.changeStatus('collect', status);
}
</script><style lang="scss" scoped></style>

运行项目就报如上报警:[Vue warn]: Invalid vnode type when creating vnode: null. 

经过一番查询和尝试,发现是ref和组件名冲突了,虽然这里ref使用的是驼峰格式,组件名是-分隔,但底层应该是将tools-header转换成了toolsHeader,导致了名称冲突。修改下ref名称即可:

<template><view><tools-header ref="toolsHeaderChild" toolsName="name" @collect="collectools"></tools-header></view>
</template><script setup>
import { ref } from 'vue';const toolsHeaderChild= ref({});const collectools = (status)=>{toolsHeaderChild.value.changeStatus('collect', status);
}
</script><style lang="scss" scoped></style>

再次运行,结果正常。

参考文章:

https://www.cnblogs.com/lpkshuai/p/17176606.html

nuxt3 Vue 3 报错 Invalid vnode type when creating vnode:null_invalid vnode type when creating vnode: null.-CSDN博客


http://www.ppmy.cn/devtools/94536.html

相关文章

Redis20-通信协议

目录 RESP协议 概述 数据类型 模拟Redis客户端 RESP协议 概述 Redis是一个CS架构的软件&#xff0c;通信一般分两步&#xff08;不包括pipeline和PubSub&#xff09;&#xff1a; 客户端&#xff08;client&#xff09;向服务端&#xff08;server&#xff09;发送一条命…

忘记iPhone锁屏密码,多次输出密码导致iPhone停用了怎么解锁?

iphone已停用怎么解锁&#xff1f;当因忘记iPhone锁屏密码&#xff0c;多次输入错误密码而被停用时&#xff0c;怎么解锁恢复对设备的访问是非常重要的。下面小编将给大家介绍几种解锁已停用iPhone的方法&#xff0c;一起来看看吧&#xff01; 一、使用恢复模式解锁 将iPhone连…

flask直播流 截图

class VideoCapture:def __init__(self, name):self.cap cv2.VideoCapture(name)self.q queue.Queue()t threading.Thread(targetself._reader)t.daemon Truet.start()# 帧可用时立即读取帧&#xff0c;只保留最新的帧def _reader(self):while True:ret, frame self.cap.r…

将A服务器上指定文件夹中的文件,批量同步到B服务器上

需求&#xff1a;最近有一个需求&#xff0c;需要定期将A服务器上的PDF文件&#xff0c;同步到B服务器上&#xff0c;于是便写个脚本记录一下&#xff01; 下面是使用Python3脚本实现的方法 import os import paramikodef copy_pdf_files(source_ip, source_user, source_pas…

Unity射击游戏开发教程:(31)制造一定追踪行为的敌人

在本文中,我们将介绍如何在两种敌人行为之间切换。本文是前两篇文章的延续,分别介绍了敌人躲避玩家射击以及敌人不断旋转并向玩家射击的情况。我只是介绍如何在这两种行为之间进行转换。 这种新的敌人行为的目标: 当不开火时,敌人可以躲避玩家的射击。射击时,敌人无法躲避…

【LeetCode每日一题】——623.在二叉树中增加一行

文章目录 一【题目类别】二【题目难度】三【题目编号】四【题目描述】五【题目示例】六【题目提示】七【解题思路】八【时间频度】九【代码实现】十【提交结果】 一【题目类别】 广度优先遍历 二【题目难度】 中等 三【题目编号】 623.在二叉树中增加一行 四【题目描述】…

【Qt】QPluginLoader 类学习

文章目录 一、简介二、常用方法2.1 构造函数2.2 动态加载方法——load()2.3 检查是否加载成功——isLoaded()2.4 访问插件中的根组件——instance()2.5 卸载插件——unload() 一、简介 QPluginLoader 类在运行时加载插件。 QPluginLoader 提供对Qt插件的访问。Qt插件存储在共享…

计算机网络中点到点与端到端协议的区别

计算机网络中的点到点协议和端到端协议的主要区别在于它们的服务层次、‌通信方式、‌可靠性和资源利用方面。‌ 服务层次和通信方式&#xff1a;‌点到点通信主要发生在物理层、‌数据链路层和网络层&#xff0c;‌它直接连接的两个节点之间的通信&#xff0c;‌不涉及程序或…