组件之间的通讯

组件之间的通讯

1.父组件传到子组件

父组件是通过props属性给子组件通信的
数据是单向流动 父—>子 (子组件中修改props数据,是无效的,会有一个红色警告)

父组件代码如下:
 <template>
   <div class="parent">
     <h2>{{ msg }}</h2>
     <son :fa-msg="msg"></son> <!-- 子组件绑定faMsg变量,注意驼峰-->
 </div>
 </template>
 <script>
 import son from './Son' //引入子组件
 export default {
   name: 'HelloWorld',
   data () {
     return {
       msg: '父组件',
     }
   },
   components:{son},
 }
 </script>
子组件代码如下:
 <template>
   <div class="son">
     <p>{{ sonMsg }}</p>
     <p>子组件接收到内容:{{ faMsg }}</p>
   </div>
 </template>
 <script>
 export default {
   name: "son",
   data(){
     return {
       sonMsg:'子组件',
     }
   },
   props:['faMsg'],//接收psMsg值
 }
</script>

子组件中通过props接受

第一种接收方式
props: ['childCom']
第二种接收方式
props: {
    childCom: String //这里指定了字符串类型,如果类型不一致会警告的哦
       }
第三种接收方式
props: {
    childCom: {
        type: String,
        default: 'sichaoyun' 
     }
}

2.子组件传到父组件

通过在子组件中绑定事件结合$emit传值

父组件代码如下

父组件中通过绑定自定义事件接受子组件传递过来的值

 <template>
   <div class="parent">
     <h2>{{ msg }}</h2>
     <p>父组件接手到的内容:{{ username }}</p>
     <son psMsg="我是你爸爸" @transfer="getUser"></son> 
      <!-- 监听子组件触发的transfer事件,然后调用getUser方法 -->
   </div>
 </template>
 <script>
 import son from './Son'
 export default {
   name: 'HelloWorld',
   data () {
     return {
       msg: '父组件',
       username:'',
     }
   },
   components:{son},
   methods:{
     getUser(msg){
       this.username= msg
     }
   }
 }
 </script>
子组件代码如下

子组件通过$emit传递值

 <template>
   <div class="son">
     <p>{{ sonMsg }}</p>
     <p>子组件接收到内容:{{ psMsg }}</p>
     <!--<input type="text" v-model="user" @change="setUser">-->
     <button @click="setUser">传值</button>
   </div>
 </template>
 <script>
 export default {
   name: "son",
   data(){
     return {
       sonMsg:'子组件',
       user:'子传父的内容'
     }
   },
   props:['psMsg'],
   methods:{
     setUser:function(){
       this.$emit('transfer',this.user)//触发transfer方法,this.user 为向父组件传递的数据
     }
   }
 }
 </script>

非父子传值(兄弟之间传值)

假如有两个组件需要通信:A和B,A组件上绑定点击事件,发送一则消息,B组件接收

1.初始化,全局创建$bus
// main.js
window.$bus=new Vue();

注意,这种方式初始化了一个全局的事件总线

2.发送事件

$bus . $emit(‘msg’,“来自A页面的消息”)

<!-- A.vue -->
<template>
   <button @click="sendMsg">-</button>
</template>

<script> 
//import $bus from "../bus.js";
export default {
 methods: {
   sendMsg() {
     $bus.$emit("aMsg", '来自A页面的消息');
   }
 }
}; 
</script>

接下来 需要在B页面接受消息

接收事件

$bus . $on(“事件名”,callback)

<!-- IncrementCount.vue -->
<template>
 <p>{{msg}}</p>
</template>

<script> 
//import $bus  from "../bus.js";
export default {
 data(){
   return {
     msg: ''
   }
 },
 mounted() {
   $bus.$on("aMsg", (msg) => {
     // A发送来的消息
     this.msg = msg;
   });
 }
};
</script>

兄弟之间通讯还可以用本地存储和vuex.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3 中,组件之间通讯可以使用以下几种方式: 1. Props 和 Emit 在父组件中通过向子组件传递属性(props),子组件可以接受到父组件的数据。而子组件可以通过触发事件(emit)向父组件发送消息。 父组件传递props: ```html <template> <child-component :message="message" @send-message="handleSendMessage"/> </template> <script> import ChildComponent from './ChildComponent.vue' export default { components: { ChildComponent }, data() { return { message: 'hello' } }, methods: { handleSendMessage(msg) { console.log(msg) } } } </script> ``` 子组件接受props并emit: ```html <template> <div> <h2>{{ message }}</h2> <button @click="sendMessage">Send Message</button> </div> </template> <script> export default { props: { message: String }, methods: { sendMessage() { this.$emit('send-message', 'Hello Parent') } } } </script> ``` 2. Provide 和 Inject 在父组件中通过 provide 方法向子孙组件提供数据,子孙组件可以通过 inject 方法来注入数据。 父组件 provide: ```html <template> <div> <grand-child-component></grand-child-component> </div> </template> <script> import GrandChildComponent from './GrandChildComponent.vue' export default { components: { GrandChildComponent }, provide() { return { message: 'hello' } } } </script> ``` 子孙组件 inject: ```html <template> <div> <h2>{{ message }}</h2> </div> </template> <script> export default { inject: ['message'] } </script> ``` 3. $refs 父组件可以通过 `ref` 属性来获取子组件的引用,从而调用子组件的方法或直接操作子组件的属性。 父组件获取子组件引用: ```html <template> <div> <child-component ref="child"></child-component> <button @click="handleClick">Click Me</button> </div> </template> <script> import ChildComponent from './ChildComponent.vue' export default { components: { ChildComponent }, methods: { handleClick() { this.$refs.child.someMethod() } } } </script> ``` 子组件中的一些方法: ```html <template> <div> <h2>{{ message }}</h2> </div> </template> <script> export default { data() { return { message: 'hello' } }, methods: { someMethod() { console.log('hello from child component') } } } </script> ``` 以上是 Vue3 中组件之间通讯的三种方式,开发者可以根据实际情况选择使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值