Is it possible for me to activate a function on a different component using my header?

Hi there, I'm curious about how Vue routing and the tree structure work together. In my setup, I have a parent router that contains both my router-view and header at the same level.

I want to be able to trigger some functions from my header component to a specific route called dashboard within the router-view.

For example: header.vue

<a href="#" @click.prevent="update()"> Click me to update the dashboard </a>

dashboard.vue

<p> {{datafrom filldata}} </p>

  methods: { 
   fillDataToP() {
    // Function to fill data
   }
  }

Do you think it's possible to achieve this functionality in Vue?

Answer №1

If you are looking to increase communication between components in Vue.js, utilizing the EventBus feature is a great option.

Simply go to your main.js file and insert:

const EventBus = new Vue()

Vue.prototype.$bus = EventBus;

In your header.vue file, you can now trigger an event:

For instance:

this.$bus.emit('someString', SomeObjectToPass);

Next, navigate to your Dashboard.vue where you can listen for the event like so:

this.$bus.on('sameStringAsInEmit', () => {

// Insert Data

})

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Enhancing the performance of your code by optimizing it using jQuery when a checkbox is either checked or

Looking for ways to optimize a jquery function that toggles a URL parameter based on checkbox status. Any suggestions on how to streamline this code? $('#mapControl').live('click', function(){ var thisUrl = $(location).attr(' ...

"Ways to retrieve an array of dates within a specified range of date and time

I am working with date fields in my project { tripScheduleStartDate: '2018-12-05T18:30:00.000Z', tripScheduleEndDate: '2018-12-07T18:30:00.000Z', } Is there a way to generate a datetime array from the start date to the end date, lik ...

Tips for determining if a user is refreshing the page or navigating to a different page

Scenario: I need to store a specific variable in the local storage every time a user exits the page. By utilizing the window.onbeforeunload method, I am able to capture the event when a user is leaving the page. However, I want to assign different values ...

"Utilizing GroupBy and Sum functions for data aggregation in Prisma

I am currently working with a Prisma schema designed for a MongoDB database model orders { id String @id @default(auto()) @map("_id") @db.ObjectId totalAmount Int createdAt DateTime @db.Date } My ...

Ensure that Javascript waits for the image to be fully loaded before triggering the Ajax function

function addResource() { var imgIndex = getIndexByImageID(currentDraggedImgID); var newImageID = resourceCollectionSize.length; // Insert the image $('#thePage').append('<img alt="Large" id="image' + newImageID + &a ...

Stopping before destruction in Vue 2 Lifecycle phases - how is it done?

Is there a way to modify beforeDestroy in order to prevent the component from being destroyed? Alternatively, is there any method to halt the destruction process of the component? In my specific situation, I am switching SPA pages using Vue Router. Even ...

The function for executing the specific command is not recognized and is resulting in a TypeError: client.commands

I'm having an issue with the code below and need a solution. Please help. Error : TypeError: client.commands.get(…).execute is not a function I am encountering difficulty with this specific command in my code: client.command ...

Ways to set the base URL on the development server for a call to react-scripts start

I am facing an issue with configuring my primary PHP server to run the ReactJS dev server created using yarn start within a directory named /reactive on the PHP site (using nginx proxy_pass). The problem is that I cannot set the root of the node server to ...

Searching for a jQuery plugin that can dynamically rearrange tables while automatically updating their corresponding IDs

Is there a way in jQuery to dynamically move tables around on a webpage? Currently, I have implemented a button that clones a hidden table when needed and another button to delete unwanted tables. Now, I am looking to incorporate a feature that allows the ...

tips for personalizing your jQuery image preview script

Currently, I have implemented a jQuery script that allows me to preview multiple images before uploading them. Although the functionality works well, I am facing difficulties customizing it to meet my specific requirements. <script> $(document).r ...

Perform a search within an iframe

Despite the fact that iframes are considered outdated, I am attempting to create a browser within an HTML browser. I have implemented a search bar that opens the typed input in a new window which searches Google. However, I would like to modify it so that ...

Utilizing an ActiveX control embedded within another ActiveX control on a webpage

Struggling with accessing a non IDispatch method in an ActiveX control I created. In my web page, I have two separate Active X objects that were developed by me. The issue arises when I call a method on the first object, which returns an interface pointer ...

What is the best way to bundle an Express server into an Electron application?

Currently in the process of developing an Electron app using vue-cli-electron-builder. My setup includes a local MySQL database and an Express server. I am wondering how to bundle my Express server with the Electron app? The Express server is used for d ...

Having trouble with DataTables functionality in Laravel blade views?

Blade template with a query: @extends('2a.layouts.master') <link rel="stylesheet" href="//cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css"> <script src="https://code.jquery.com/jquery-3.3.1.js"></script> <script s ...

Once the form has been submitted, proceed to open a new page following processing

Trying to remove an entry or offer from the database and then return to the page with all entries/offers. Attempted using Javascript, but it successfully deletes the item without redirecting to the overview page. Is this the correct approach with Javascri ...

The consistent failure of the 201 status node express API is causing major

I am currently working on creating an API using Express. However, when I receive a response from the server, it shows '201 created'. The issue arises when I attempt to make an HTTP request through promises and encounter a false interpretation of ...

What is the best way to ensure the search box remains fixed in the top navigation bar while maintaining a fluid and responsive design?

I've been struggling as a novice programmer, and even with the help of more experienced programmers, we haven't been able to figure it out. I'm trying to integrate a search box into the top navigation that adjusts responsively to different ...

Using React as a dependency for @vue/apollo-composable allows for seamless integration of Apollo

My VueJs application is built using the Composition API. I am trying to implement Apollo for making queries to my GraphQL backend, but I keep encountering an error stating Could not resolve "react". The dependencies in my project include: "@apollo/client" ...

Displaying properties in Vue using a bound component

Offspring Vue.component('xms',{ template : `<div> {{failureMsg}} </div>`, props : { failureMsg : '', minimumLength : '', } } ) Forefather html <xms errorMsg="Is ...

The Vue template syntax utilizes double curly braces for incrementing values within must

Embarked on learning Vue.js recently and I am intrigued by the unusual behavior in my simple code. I cannot seem to figure out what is going wrong. I am attempting to increment a counter inside mustache syntax, but something strange is happening with this ...