to communicate between two indivudual components in vues ( there is no relation like child and parent)

 When you have two individual components in Vue.js that are not directly related as parent and child components, you can still establish communication between them using various techniques. Here are a few methods you can use:

  1. Event Bus: Create an event bus as a separate Vue instance and use it as a central communication hub between components. Emit events from one component and listen for those events in another component.

 s

// Event Bus
const bus = new Vue();

// Emit event from Component A
bus.$emit('event-name', eventData);

// Listen for event in Component B
bus.$on('event-name', eventData => {
  // Handle event in Component B
});

  1. Global Store (Vuex): Utilize Vuex, the state management library for Vue.js. Store shared data in the Vuex store and access it from both components.

// Vuex store
const store = new Vuex.Store({
  state: {
    sharedData: ''
  },
  mutations: {
    updateSharedData(state, data) {
      state.sharedData = data;
    }
  }
});

// Set shared data from Component A
store.commit('updateSharedData', data);

// Get shared data from Component B
const sharedData = this.$store.state.sharedData;

s

  1. Custom Events (Non-Parent-Child): Use custom events to communicate between components, leveraging Vue's event handling system.

Example:


// Emit custom event from Component A
this.$emit('custom-event', eventData);

// Listen for custom event in Component B
this.$on('custom-event', eventData => {
  // Handle event in Component B
});

s






  1. Vue.observable: Use Vue's observable function to create reactive objects that can be shared between components. Components can subscribe to changes and react accordingly.

Example:


// Shared object
const sharedObject = Vue.observable({ data: '' });

// Update shared data from Component A
sharedObject.data = newData;

// React to changes in shared data from Component B
Vue.watch(
  () => sharedObject.data,
  (newData, oldData) => {
    // Handle data changes in Component B
  }
);

s

These approaches allow components to communicate even when they do not have a direct parent-child relationship. Choose the method that best fits your application's architecture and communication needs.






Certainly! Here's a full example demonstrating the usage of an event bus for communication between two Vue components:

  1. Create an event bus as a separate Vue instance:


// eventBus.js

import Vue from 'vue';

const eventBus = new Vue();

export default eventBus;

  1. Emit an event from Component A:

 s

// ComponentA.vue

<template>
  <div>
    <button @click="emitEvent">Emit Event</button>
  </div>
</template>

<script>
import eventBus from './eventBus';

export default {
  methods: {
    emitEvent() {
      eventBus.$emit('custom-event', 'Hello from Component A');
    }
  }
};
</script>

s

  1. Listen for the event in Component B:

 s

// ComponentB.vue

<template>
  <div>
    <p>{{ message }}</p>
  </div>
</template>

<script>
import eventBus from './eventBus';

export default {
  data() {
    return {
      message: ''
    };
  },
  created() {
    eventBus.$on('custom-event', this.handleEvent);
  },
  destroyed() {
    eventBus.$off('custom-event', this.handleEvent);
  },
  methods: {
    handleEvent(data) {
      this.message = data;
    }
  }
};
</script>

s


n this example, we have two components, ComponentA and ComponentB, which are not directly related. The event bus acts as a mediator between them.

When the button in ComponentA is clicked, it emits the 'custom-event' with a message payload using eventBus.$emit('custom-event', 'Hello from Component A').

ComponentB listens for the 'custom-event' using eventBus.$on('custom-event', this.handleEvent). The handleEvent method is triggered when the event is emitted, and it updates the message data property, which is then displayed in the template.

Remember to import the event bus in both ComponentA and ComponentB as shown in the examples.

This way, when you click the button in ComponentA, the event is emitted and received by ComponentB, allowing them to communicate indirectly through the event bus.























1 comment:

  1. Vue.js / Frontend Developer - Bhumika
    Description
    | Must Have -

    Professional experience in software development in the frontend development area [min. 3-5 years]

    Good knowledge of multi-tier and cloud-based architectures (Microsoft Azure, Azure DevOps, AWS)

    Extensive experience frontend development: VueJS, Java based Frontend Frameworks (Apache Struts, Java Server Faces), Typescript, HTML5, CSS, JS

    Good Understanding of Rest based development and backend development, experience, and willingness to work in Backend Development (Java) is an advantage

    Understanding in databases (SQL/ NoSQL)

    Knowledge of common software development tools (Sonar, Git, Confluence, JIRA/Azure Devops)

    Experience in writing Unit test cases(eg. Jest framework/ react)

    Methodical knowledge of agile software development (Scrum, Kanban, DevOps)

    Excellent ability to work in a team, independently organized work

    Willingness to work in shifts

    Willingness to work On-Call Duty

    Good to have domain knowledge -how in the automotive area

    Strong micro-service architecture knowledge

    DevOps-Toolchain (Git, Jenkins, Nexus…)

    Understands high-level architectural design and patterns

    Aid in planning product development and timelines

    Provide feedback to marketing or sales teams

    Leading the team in translating user stories and other requirements into clear and focused implementation plans

    Work closely with other project team members (engineers, designers and project managers)

    Deliver high-quality outputs at all stages of development, from documentation to code

    Communicate clearly with both clients and colleagues

    ReplyDelete

Event listening in react

 How we can listen to som eevents some envents fire like click or automatically user enters into input button , that is event on word type i...