Implementing click functionality in parent component to trigger a function in child component in Vue 3

Need help with executing the 'upload' function in the 'MediaImport' component upon clicking a button in the parent component. The goal is to navigate to another route after execution. Any insights on how to achieve this correctly?

Code for VideoRecognition page:

<script lang="ts">
import Button from 'primevue/button';

import MediaImport from '@/components/MediaImport';
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import WebSocketService from '@/utils/WebSocketService';

export default {
  name: "VideoRecognition",
  emits: ['upload-media'],
  setup(_, { emit }) {
    // Handling logic and methods
  },
  components: {
    Button,
    MediaImport,
  },
};
</script>

<template>
    <div class="container">
        // Template structure 
    </template>

Code for MediaImport component:

<script lang="ts">
import Message from 'primevue/message';
import BaseMediaImport from './BaseMediaImport.vue';
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome';
import { Comment, ref } from 'vue';
import { randomNumber } from '@/utils/randomNumber';

export default {
    name: 'MediaImport',
    extends: BaseMediaImport,
    emits: ['select', 'before-upload', 'progress', 'error', 'clear', 'remove', 'has-files', 'media-object-url', 'upload', 'uploader', 'has-uploaded', 'before-send'],
    setup() {
        // Set up logic for media upload
    },
    data() {
        // Data properties initialization 
    },
    methods: {
        // Methods for uploading files and handling related actions
    },
    computed: {
        // Computed properties calculations
    },
    components: {
        ErrorMessage: Message,
        FontAwesomeIcon
    },
}
</script>

<template>...</template>

Answer №1

Looking to accomplish this task? Take a look at these two strategies:

  1. Trigger the execution of a method in the child component by passing a new prop when a button is clicked in the parent component. Create a watch on that prop and use it to trigger the desired method.
  2. Utilize defineExpose() and access it through the ref in the parent component.

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

What is the method to retrieve the image's value after dropping it onto the droppable area?

I have implemented a drag and drop feature using jQuery, and I am trying to extract the value of an image and insert it into a database. Additionally, I want to update and remove the image value when it is removed from the droppable area. How can I achie ...

retrieve the information from a specific field within the store and store it in an array

Perhaps this is a question that pertains to using pure JavaScript, but for some reason I can't seem to figure it out. I am currently working on extjs4.2 using Sencha Architect and have received a JSON response from the server in the following format: ...

Building a ReactJS application that displays an array of images with a distinct pop-up feature for each image

Having trouble creating unique popups for each image in React. I have a set of 20 images that should be clickable, with only one popup open at a time, each containing text and an image. Currently mapping out the images from an array. Looking for assistan ...

Retrieve the query parameters from an URL string

I'm responsible for an application that serves SSRS reports. To access a specific report, users typically navigate to the following URL: https://reportserver.com/reportname On clicking View Report, a postback is sent to the report server with user-d ...

Trouble encountered while setting up Firebase Auth for React Native by utilizing AsyncStorage

Trying to implement SMS authentication via Firebase has presented some challenges for me. Despite poring over the documentation and scouring Google for solutions, I've hit a dead end. My setup is pretty basic - just a single input field and a "Send" b ...

Learn the simple technique for transferring text to the clipboard using the data-title attribute

Hey there, I need some help with the following code snippet: <h3 class="project discord" data-title="test1234"><i class='bx bx-fw bx-tada-hover bxl-discord-alt'></i>Discord</h3> I want to be able to c ...

The mat-table fails to populate with data retrieved from the rest service

I have successfully fetched an array from my REST service and displayed some information from the response on the page. However, I am facing issues populating my mat-table and I'm unsure of the cause. The mat-table was functioning properly in the past ...

Error occurs in ASP.NET AJAX Control Toolkit while uploading files within Scriptresource.axd

I recently encountered an issue with my AJAX Control Toolkit File Upload (Version 15.1.4) in my ASP.Net Web Application. Up until this week, it was functioning perfectly fine. However, starting yesterday, I started receiving a JavaScript error right after ...

Creating duplicates of elements and generating unique IDs dynamically

I'm in the process of cloning some form elements and I need to generate dynamic IDs for them so that I can access their content later on. However, I'm not well-versed in Jquery/Javascript and could use some guidance. Here's a snippet of my ...

Import a JSON file into TypeScript declaration files (*.d.ts)

I am currently working on a declaration file where I need to establish a global type that is specifically tied to a predetermined list of string phrases. These string phrases are part of property keys within an object located in a JSON file. I have a coup ...

A file can be generated with PHP, however, it takes thirty minutes for it to be viewable on the browser

Currently, I am working on a browser-based game where the process of creating an account involves using a JS file to call a PHP file (POST) to write an XML file. The issue at hand is that after successfully writing the XML file with the correct content in ...

Issue encountered with Azure DevOps during TypeScript (TS) build due to a type mismatch error: 'false' being unable to be assigned to type 'Date'. Conversely, the build functions correctly when run locally, despite the type being defined as 'Date | boolean'

I am facing an issue with my NestJS API while trying to build it using Azure DevOps pipeline. The build fails with the following error: src/auth/auth.controller.ts(49,7): error TS2322: Type 'false' is not assignable to type 'Date'. src/ ...

You are unable to call the Class constructor Construct without using the 'new' keyword - AWS CDK

I have a custom NPM library that is designed to be utilized in various CDK applications and AWS accounts. export class FrontendConstruct extends Construct { constructor(parent: Construct, id: string, props: FrontendConstructProps) { super(parent, id) ...

Incorporating an additional ion-item alongside the existing one instead of substituting it

I am retrieving a list of questions from an API with pagination. I have a button that triggers a function to load the next page of questions. Instead of replacing the previous page, I want to append the new questions below the existing ones. Here is my cur ...

What is the best way to loop through ul elements inside a div and hide each one separately upon submission?

I have a question regarding my node application that uses express. I have a view function that generates a list of inactive companies, with each company having two submit input types: "Active" and "Delete". My goal is to be able to click on the submit butt ...

The presence of markers will not be found on the Google Map

Before reaching out for help, I took the time to explore the site and consult with my classmates. They were able to successfully implement the code that is causing me trouble on my end. It seems like my markers are not appearing for some unknown reason. T ...

Best Practices for Iterating over Nested Objects and Arrays Using ng-repeat

I've been trying to use Angular's ng-repeat, but nothing seems to work for me. I'm attempting to loop through companies.users and display all the first names. Any assistance would be greatly appreciated! Thank you! <div ng-app="app" ng-c ...

Is it possible to share a MySQL connection for cross-module usage in Node/Express by utilizing promise-mysql?

Currently, I am trying to import and utilize a database module within one of my controllers. Despite successfully establishing the initial connection, I am encountering an error when accessing any of my routes through the browser: "Cannot read property ...

What steps do I need to take to create a calendar with this appearance?

I am working on my college project website and I would like to display a calendar similar to this. How can I achieve this using CSS and JavaScript? I want the user to be able to hover over a date to see all dates of the month, as well as have options to se ...

What is the best way to incorporate a scroll bar into a table that updates dynamically?

If I were to create an empty table in my index.html: <body> <table width="800" border="0" class="my-table"> <tr> </tr> </table> </body> Afterward, I would dynamically add rows and columns to my-table using ...