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>
复制代码

 

posted @   技术渣渣Z  阅读(192)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· 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
点击右上角即可分享
微信分享提示