vue父子兄弟之间的组件通信

vue组件通信的方式

动态处理事件

父组件


<operate 
	@onOperate="handleOperate"
></child>

methods: {
    handleOperate(e) {
      let id = e.target.id;
      let handle = this[`handle${id}`];
      handle && handle();
    },
    handleModify() {
      console.log("modify=编辑");
    },
    handleAdd() {
      console.log("add == 添加");
    },
    handleDelete() {
      console.log("delete == 删除");
    }
  },

子组件operate

<div class="wrapper">
   <a href="javascript:;" id="Modify" @click="$emit('onOperate',$event)">编辑</a>
    <a href="javascript:;" id="Add" @click="$emit('onOperate',$event)">添加</a>
    <a href="javascript:;" id="Delete" @click="$emit('onOperate',$event)">删除</a>
</div>

父子组件通信

父组件:father.vue

<template>
  <div class="father">
    <h1>父组件</h1>

    <!-- 父组件 接收 子组件的数据 -->
    <p :style="{ fontSize: fontsize + 'px', color: setColor }">
      改变颜色和字体大小
    </p>

    <!-- 父组件 传值 子组件 -->
    <child-f :msg="content" @sendMsg="handle($event)"></child-f>

    <hr />
  </div>
</template>

<script type="text/javascript">

// 引入子组件
import childF from './son'

export default {
  data () {
    return {
      content: '父组件传递的数据',
      fontsize: 5,
      setColor: 'orange'
    }
  },
  components: {
    childF
  },
  methods: {
    handle (val) {
      console.log('子组件传递的数据:', val);
      this.fontsize += val
      this.setColor = 'red'
    }
  }
}
</script>

子组件:son.vue

<template>
  <div class="">
    <h1>son子组件</h1>
    <button @click="click">点击</button>
    <p>子组件页面:{{ msg }}</p>
  </div>
</template>

<script type="text/javascript">
export default {
  props: ['msg'],
  data () {
    return {

    }
  },
  methods: {
    click () {
      this.$emit('sendMsg', 5)
    }
  }
}
</script>


兄弟组件通信的三种方式

方式一: 子-父-子

需求:子组件1 传值 子组件2

原理:通过子组件1 传值 父组件,然后父组件 传值 子组件2 ,从而实现:子组件1 传值 子组件2

父组件:parent.vue

<template>
  <div class="">
    <h1>父组件</h1>
    <p>name: {{ name }}</p>
    <hr />
    <Child1 :name="name" @ChildOneClick="handleParentClick" />
    <hr />
    <Child2 :name="name" />
  </div>
</template>

<script type="text/javascript">

// 引入子组件
import Child1 from './child1';
import Child2 from './child2';

export default {
  data () {
    return {
      name: '学习编程'
    }
  },
  methods: {
    handleParentClick (val) {
      this.name = val;
    }
  },
  components: {
    Child1,
    Child2
  }
}
</script>

子组件1:child1.vue

<template>
  <div class="">
    <h1>子组件1</h1>
    <p>name: {{ myName }}</p>
    <button @click="handle">按钮</button>
  </div>
</template>

<script type="text/javascript">

export default {
  data () {
    return {
      myName: 'child1'
    }
  },
  props: ['name'],
  methods: {
    handle () {
      this.$emit('ChildOneClick', '自学HTML')
    }
  }
}
</script>

子组件2:child2.vue

<template>
  <div class="">
    <h1>子组件2</h1>
    <p>name: {{ name }}</p>
  </div>
</template>

<script type="text/javascript">

export default {
  props: ['name'],
  data () {
    return {

    }
  }
}
</script>

方式二: eventBus

main.js

// 创建中间事件eventBus
Vue.prototype.eventBus = new Vue();

parent.vue

<template>
  <div class="">
    <h1>父组件</h1>
    <p>name: {{ name }}</p>
    <hr />
    <Child1 :name="name" />
    <hr />
    <Child2 :name="name" />
  </div>
</template>

<script type="text/javascript">

// 引入子组件
import Child1 from './child1';
import Child2 from './child2';

export default {
  name: 'Parent',
  data () {
    return {
      name: '学习编程'
    }
  },
  components: {
    Child1,
    Child2
  }
}
</script>

child1.vue

<template>
  <div class="">
    <h1>子组件1</h1>
    <p>name: {{ myName }}</p>
    <button @click="handle">按钮</button>
  </div>
</template>

<script type="text/javascript">

export default {
  name: 'Child1',
  data () {
    return {
      myName: 'child1'
    }
  },
  props: ['name'],
  methods: {
    handle () {
      // 发送消息
      this.eventBus.$emit('ChildOneClick', '自学Vue')
    }
  }
}
</script>

child2.vue

<template>
  <div class="">
    <h1>子组件2</h1>
    <p>name: {{ name }}</p>
  </div>
</template>

<script type="text/javascript">

export default {
  name: 'Child2',
  data () {
    return {
      name: '学习React'
    }
  },
  mounted () {
    // 接收消息
    this.eventBus.$on('ChildOneClick', (data) => {
      console.log('child2接收参数', data);
      this.name = data;
    })
  }
}
</script>

方式三: vuex

需求:子组件1 传值 子组件2

安装插件:npm i vuex --save

src/store/index.js

import Vue from 'vue'
import Vuex from 'vuex';

Vue.use(Vuex);

const store = new Vuex.Store({
  state: {
    name: 'HTML + CSS + JS'
  },
  mutations: {
    changeName (state, val) {
      state.name = val
    }
  },
  actions: {

  }
})

export default store;

main.js

import Vue from 'vue'
import App from './App.vue'
import router from './router'
// 引入store
import store from './store/index'

Vue.config.productionTip = false

// 创建中间事件eventBus
Vue.prototype.eventBus = new Vue();
// 将store挂载到vue实例
new Vue({
  store,
  router,
  render: h => h(App)
}).$mount('#app')

父组件:parent.vue

<template>
  <div class="">
    <h1>父组件</h1>
    <p>name: {{ name }}</p>
    <hr />
    <Child1 :name="name" />
    <hr />
    <Child2 :name="name" />
  </div>
</template>

<script type="text/javascript">

// 引入子组件
import Child1 from './child1';
import Child2 from './child2';

export default {
  name: 'Parent',
  data () {
    return {
      name: '学习编程'
    }
  },
  components: {
    Child1,
    Child2
  }
}
</script>

子组件:child1.vue

<template>
  <div class="">
    <h1>子组件1</h1>
    <p>name: {{ name }}</p>
    <button @click="handle">按钮</button>
  </div>
</template>

<script type="text/javascript">

export default {
  name: 'Child1',
  data () {
    return {
      myName: 'child1'
    }
  },
  props: ['name'],
  methods: {
    // 触发事件
    handle () {
      this.$store.commit('changeName','mysql')
    }
  }
}
</script>

子组件:child2.vue

<template>
  <div class="">
    <h1>子组件2</h1>
    <p>技术栈: {{ $store.state.name }}</p>
  </div>
</template>

<script type="text/javascript">

export default {
  name: 'Child2',
  data () {
    return {
      name: '学习React'
    }
  }
}
</script>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

落花流雨

你的鼓励将是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值