vue 组件传值
App组件 <template> <div class="app"> <h2>App组件</h2> <h3>接收到子组件的消息为:{{twoVal}}</h3> <One :val="msg"/> <Two @handle="handleGetDate"/> </div> </template> <script> import One from "./components/one.vue"; import Two from "./components/two.vue"; export default { components:{ One, Two }, data(){ return { msg:"我是APP组件的信息", twoVal:"" } }, methods:{ handleGetDate(val){ console.log(val); this.twoVal = val; } } } </script> <style> .app{ padding:40px; background: peru; } </style>
子组件1
<template> <div class="one"> <h2>one</h2> <h3>接收到父组件的消息为:{{val}}</h3> <button @click="handleSend()">发送给Two组件</button> <OneOne/> </div> </template> <script> import OneOne from "./oneone.vue"; export default { // props:["val"], props:{ val:{ type:String, required:true } }, components:{ OneOne }, created() { console.log(this,"one") }, methods:{ handleSend(){ this.$observer.$emit("handleTwo","我是ONE组件的消息") } } } </script> <style> .one{ padding:30px; background:red } </style>
子组件2
<template> <div class="two"> <h2>two</h2> <input type="text" v-model="msg" /> <button @click="send()">发送给父组件</button> <h3>接收到one组件的消息为:{{ONEval}}</h3> </div> </template> <script> export default { created() { console.log(this, "two"); this.$observer.$on("handleTwo", val => { console.log(val); this.ONEval = val; }); }, data() { return { msg: "", ONEval: "" }; }, methods: { send() { this.$emit("handle", this.msg); } } }; </script> <style> .two { padding: 30px; background: yellow; } </style>
子组件3
<template> <div class="oneone"> <h2>oneone</h2> </div> </template> <script> export default { } </script> <style> .oneone{ padding:30px; background:green } </style>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY