Vue父子组件传值$parent , ref,$refs,props大总结
子组件:
<template> <div class="child"> <slot name='meiyong'></slot> <p >我是子组件哟 {{num}} {{ttttt}} {{nike}} 这是我独有的----->{{isChi}} </p> <slot name="strong"></slot> </div> </template> <script> import Utils from '@/utils/utils' export default { props:['ttttt','nike'], data(){ return { num:" ", isChi:'★子组件独有★' } }, mounted(){ //传值给父组件 this.$parent.runTime= Utils.formatDate(new Date(), "YYYY-MM-DD hh:mm:ss"); } } </script> <style lang="scss" scoped> .child{ height: 5rem; width: 100%; background-color: skyblue; text-align: center; position: relative; p{ height: 30px; width: 800px; display: block; position: absolute; top: 0; bottom: 0; left: 0; right: 0; margin: auto; // transform: translate(-50%,-50%) } .parent{ color:greenyellow; } .strong{ position: absolute; bottom: 0; left: 50%; transform: translateX(-50%); color: darkmagenta; } } </style>
父组件:
<template> <div > <!-- 父传子--> <tc :ttttt='mongodb' :nike='nike' ref="toChild"> <template v-slot:meiyong > <h1 class="parent">v-slot中的内容 没用?呵呵</h1> </template> <template v-slot:strong> <h2 class="strong">你很强???{{runTime}} </h2> </template> </tc> </div> </template> <script> import testChild from '@/views/testChild' export default { data(){ return { ttttt:'█我是父组件给child传的值█', mongodb:'█我是MongoDB哟█', nike:'███ just do ███', runTime:'null' } }, mounted(){ let chi = this.$refs.toChild console.log('chi===>',chi.isChi); this.$refs.toChild.isChi = '我试试能不能修改子组件的值' // num不是传的值 所以要ref才能传 this.$refs.ttttt.num = '1949' }, components:{ tc:testChild } } </script> <style lang="scss" scoped> </style>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现