Using Vue.js to invoke a method from another component with a specified parameter

I am currently working on a project with two components - one for uploading files and the other for displaying files. In my Upload Component, I want to call the Preview Component and pass a parameter so that a method in the Preview Component can use a value generated in the Upload Component.

Here is what I have done so far:

UploadComponent.vue

<template>
    …
    <button @click="upload"></button>
    <preview-component :url="this.location"></preview-component>
</template >

<script>
import PreviewComponent from '@/js/components/PreviewComponent';

export default {
    components: {
        'preview-component': PreviewComponent
    },

    props: ['url'],

    data () {
        return {
            // ...
            location: ''
        }
    },

    methods: {
        upload() {
            // ... upload stuff then update the global var location
            this.location = response.data.location;
        },
    }

}
</script>

This is my Preview Component:

<template>
    <div id="body">
        ///...
    </div>
</template>

<script>
export default {
    props: ['url'],

    methods: {
        loadPdf (url) {
            //
        },
    }    
}
</script>

Currently, I am encountering an error where url is not defined, indicating that the url is not being passed from the UploadCOmponent to the PreviewComponent. How can I resolve this issue?

Answer №1

You have discovered a ninja this hiding within the template of your UploadComponent.

The correct code should look like this:

<preview-component :url="location"></preview-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

Getting your routing to function properly in React.js involves setting up routes for different

I have been working on a React project, which can be found here, and trying to tackle the issue of relative links in the navbar. However, every time I click on About Me, I encounter this message: Cannot GET /about The chunk of code I am currently dealing ...

Assistance needed with Chrome extension - seeking guidance on how to include the URL of the current page into the HTML of the default popup in the manifest.json file

I have a manifest file that looks like this: { "name":"Improve the Web extension", "version":"0.2", "manifest_version":2, "description":"Enhancing web accessibility is crucial. Poorly designed websites can exclude disabled individuals. By ...

"Enhancing Error Handling in Express with Node.js Middleware for Parsing

I've been working on developing a middleware to manage errors, but I'm struggling to send the correct format to my frontend. Below, I'll outline my various attempts in the hopes of getting some guidance on this issue. Attempt 1 app.use(fun ...

Unresolved dependencies causing a rollup error

When I try to use import file from 'file.json' in a Vue component and run npm run build to bundle it with Rollup, I encounter an issue. An error is thrown during the process, preventing the file from being bundled as expected. https://i.sstatic ...

Can the axios version be displayed during runtime?

I have incorporated axios into my project using npm import axios from 'axios' Is there a way to print the version of axios in the console after the entire application has been compiled? ...

Is there a way to dynamically access BEM-style selectors using CSS modules?

For instance, I have this specific selector in my App.module.css file: .Column--active I want to access this selector from the App.js file in React using CSS modules. After importing all selectors from the CSS file as import styles from './App. ...

Identifying the absence of a character at the end of the last line in Node.js to detect

Currently, I am processing data line by line from a buffer. Within the buffer, the data is structured as follows: name,email,phn test1,<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="47332234337607223f262a372b226924282a">[em ...

Sending a parameter to the window.onload callback function

Is there a way to pass parameters from ModelAndView to a window.onload function in JavaScript within an HTML file? Here is an example of how it can be done: @RequestMapping(value = "/admin/printtext") public ModelAndView printtext() { ModelAndView mo ...

Tips for utilizing this RegEx in my PHP code to maintain UTF-8 characters while eliminating symbols

I am striving to eliminate symbol characters such as #$^&%()'"!,.? and whitespace from my string, while keeping all other UTF-8 characters, numbers, and regular characters intact. I have successfully implemented this on the front-end using JavaScr ...

Running tasks in the background with Express.js after responding to the client

Operating as a basic controller, this system receives user requests, executes tasks, and promptly delivers responses. The primary objective is to shorten the response time in order to prevent users from experiencing unnecessary delays. Take a look at the ...

Passing PHP values to JavaScript and then to AJAX requires using the appropriate syntax and techniques for

Currently, I am populating a table with data retrieved from my database. Here is a snippet of the code: <?php //mysqli_num_rows function while($row=mysqli_fetch_array //I know this may be wrong, but that's not the point echo "<tr><t ...

Issue with scroll being caused by the formatting of the jQuery Knob plugin

I am currently utilizing the jQuery Knob plugin and I am looking to have the displayed value shown in pound sterling format. My goal is for it to appear as £123456. However, when I attempt to add the £ sign using the 'format' hook, it causes is ...

I'm struggling to figure out how to use multer npm to process files in my controllers on the frontend with Vue.js and the backend with Node.js. I can't seem to get the backend to

When dealing with the backend, I always encounter an undefined error when trying to locate the file. const mysql = require("mysql"); const Poste = require("../models/post"); // When creating and saving a new post exports.create = (req, res) => { r ...

Switch between showing and hiding two divs using jQuery

I'm attempting to create a link that will toggle hide/show between two divs, and while this concept is not uncommon, I am facing some difficulties. My HTML structure is as follows: <div id="wrapper"> <div id="tog_A"> <div id="tog ...

Rendering various models in separate DIVs using Threejs

(Brand new to venturing into Three.js) Overview Currently, I am immersed in the development of a 3D Viewer that can showcase multiple stl models simultaneously. Each model is supposed to be rendered within its own separate div element, resulting in a g ...

What could be causing my code to become unresponsive when using a for loop compared to a loop with a specific

After spending a solid 4 hours on it, I finally managed to figure it out. There were no errors popping up, so I resorted to using the debug feature which unfortunately didn't provide much insight. Without any error messages to guide me, I wasn't ...

Is it advisable to transpile my Node.js code in order to utilize ES6 features?

My focus in using Node.js is solely for server-side microservices. I'm interested in incorporating ES6 into my code, but I've come across information suggesting that Babel is necessary to transpile the code to ES5 for browser compatibility. My qu ...

Arranging JSON information based on category

I am currently populating an HTML table with data retrieved from a JSON file. It currently displays all the data in the order it appears in the JSON file, but I would like to organize it into different tables based on the "group" category in the file such ...

Navigating through the various iterations of jQuery

Unique Case Study I'm currently facing a dilemma involving the integration of jstree and jquery-ui datepicker. The scenario is this: I am attempting to utilize jquery-ui-datepicker on an input element that is dynamically inserted into the DOM after ...

Adding images in Meteor is simple and effective

As a beginner in Meteor, I have recently begun my journey of learning the platform. I have stored all my images in the "/public" folder and they are named numerically (e.g., "1.jpg", "2.jpg", "3.jpg"). My goal is to add all these images to a collection ...