【vue】父子组件通信遇到的错误信息
大意:
父组件控制子组件Dialog 对话框的显隐
错误demo:
父组件:
<template> <div class="app-container "> <my-sun-component :is-show="isShow"></my-sun-component> <el-button type="text" @click.native="handleButton">点击打开Dialog</el-button> </div> </template> <script> import mySunComponent from '@/views/skill/csun.vue'; export default { components: {mySunComponent}, data() { return { isShow: false, } }, created(){ }, methods: { handleButton(){ this.isShow = true; } }, } </script>
子组件
<template> <div class="app-container"> <el-dialog title="提示" :visible.sync="isShow" width="30%"> <span>这是一段信息</span> <span slot="footer" class="dialog-footer"> <el-button @click="isShow = false">取 消</el-button> <el-button type="primary" @click="isShow = false">确 定</el-button> </span> </el-dialog> </div> </template> <script> export default { name:'mySunComponent', data() { return { } }, created(){ }, props:{ isShow:{ type: Boolean, default: function(){ return false } } }, methods: { }, } </script>
页面效果:
错误截图:
错误文字版:
[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being mutated: "isShow"
解决方案:
父组件:
<template> <div class="app-container "> <my-sun-component :is-show="isShow"></my-sun-component> <el-button type="text" @click.native="handleButton">点击打开Dialog</el-button> </div> </template> <script> import mySunComponent from '@/views/skill/csun.vue'; export default { components: {mySunComponent}, data() { return { isShow: 0, } }, created(){ }, methods: { handleButton(){ this.isShow = Math.random()*10 + 1; } }, } </script>
子组件
<!-- des:子组件 --> <template> <div class="app-container"> <el-dialog title="提示" :visible.sync="isVisible" width="30%"> <span>这是一段信息</span> <span slot="footer" class="dialog-footer"> <el-button @click="isVisible = false">取 消</el-button> <el-button type="primary" @click="isVisible = false">确 定</el-button> </span> </el-dialog> </div> </template> <script> export default { name:'mySunComponent', data() { return { isVisible: false, } }, created(){ }, watch: { isShow(val){ this.isVisible = val ? true : false } }, props:{ isShow:{ type: Number, default: function(){ return 0 } } }, methods: { }, } </script>
ps:
- 在父组件时这样写this.isShow = Math.random()*10 + 1; 是因为watch监控的值只有变化的时候才能监听到
- isVisible:this.isVisible 不可以这样写,因为会报如下错误(在demo中未报错,在项目使用中报错了,待解析)
错误文案:
[Vue warn]: Invalid prop: type check failed for prop "visible". Expected Boolean, got Number with value 0.
作者:smile.轉角
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
欢迎关注我,一起进步!扫描下方二维码即可加我QQ

分类:
vue
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通