Passing parameters from a div to a single page component in Vue.js: A complete guide

There is code that appears on multiple pages:

<div id="contact-us" class="section md-padding bg-grey">
        <div id="contact"></div>

        <script src="/dist/build.js"></script>
</div>

Included in main.js is:

import Vue from 'vue'
import Contact from './Contact.vue'

new Vue({
    el: '#contact',
    render: h => h(Contact)
})

The Contact.vue file contains a template.

The goal is to determine which page the component was used on. To achieve this, a parameter needs to be passed from the div like

<div id="contact" page="main"></div>
. How can this be implemented?

Answer №1

How can I pass parameters from a div to a single page component in Vue.js?

It is not possible to directly pass parameters from a div since it is an HTML tag and not a custom component. You will need to create your own component that can accept the properties you want to pass.

To achieve this, first define your component and specify which properties it can receive. Then, use your component as demonstrated in the example below. For more information on passing props, refer to this documentation.

Vue.component('your-component', {
  props: ['property'],
  template: '<h3>{{ property }}</h3>'
})

new Vue({
  el: '#app'
})
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7f090a1a3f4d514a514e49">[email protected]</a>/dist/vue.js"></script>

<div id="app">
  <your-component :property="'Hello props'" />
</div>

Example using Single File Component structure.

Parent component:

<template>
  <ChildComponent :property="propValue" />
</template>


<script>
  import childComponent from './childComponent.vue';

  export default {
    components: {
      ChildComponent: childComponent
    },
    data() {
      return {
        propValue: 'Hello prop'
      }
    }
  }

</script>

Children component:

<template>
  <h3>{{ property }}</h3>
</template>

<script>
export default {
  props: ['property'] // You can add more properties separeted by commas
}
</script>

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

The incorrect ordering of my array within a nested ng-repeat in AngularJS

I have extracted Json data from a mongodb database collection. Successfully displayed the keys of this json. Currently attempting to present an array in a table using a double ng-repeat in my view. Although I am close to achieving my desired outcome, th ...

Executing a JavaScript function to trigger a PHP script that saves data into a MySQL database

I have a button in my HTML file that, when clicked, should insert data into a database and then reload the page. I am calling a JavaScript function within the onclick event to handle this. Below is the JavaScript function: function grandFinale() { i ...

Tips for updating Angular HTML with data received from Socket.IO

I am currently working on a socket program that is listening and providing log data. The socket is sending the correct data as I can see it in the console. Below is a snippet of my code: export class RoboLogComponent implements OnInit { dataToShow:any @V ...

Navigating efficiently with AngularJS directives

Hello, I am currently searching for a solution to dynamically modify the navigation links within the navigation bar based on whether a user is logged in or not. Of course, a logged-in user should have access to more pages. I have developed a UserService t ...

Having trouble with tabs in jQuery?

I'm having trouble setting up tabs in a reservation form with 3 tabs that include text boxes for user input. I can't seem to get it working properly and I'm not sure where I've gone wrong. Could it be due to the placement of the content ...

Using javascript to store HTML tags in a variable

Hey there, I have a quick question. Can someone help me figure out why this code isn't working? let plus = "+" + '<h1>'+"This is a heading"+'</h1>'; When I run the code, the output I get is: +<h1 ...

Issue encountered while attempting to create entity in jhipster

After executing the creation of an entity in the command line, JHipster successfully generated Java and script files but encountered issues with updating the database. No new table was inserted despite starting MySQL and turning off the application befor ...

Tips for achieving an AJAX-like appearance in jQuery for my code

The question may seem a bit confusing, but here's the basic idea: I'm currently working on a JavaScript library and I want to incorporate some of jQuery's style. I have a function that will take in 3 parameters, and I want it to work simila ...

Exploring the Relationship Between jQuery and JSON through Iterating Over JSON Arrays

There is an array stored in a database: a:4:{i:1;s:4:"1993";i:2;s:4:"1994";i:3;s:4:"1995";i:4;s:4:"1996";} To manipulate this array, I first unserialize it using PHP and then encode it with JSON. The code snippet is as follows: $unwp = unserialize(&apos ...

` `issues with fmt:massage tag` `

When I try to update my page elements using ajax, I encountered a problem: the fmt:message tag doesn't work when I set it in javascript. In the JSP page everything works fine: <div id="div"> <fmt:message key="search_select_country"/> & ...

Retrieve a remote text file using vanilla JavaScript instead of relying on jQuery

Currently, I am aiming to retrieve the content of a text file using JavaScript for parsing purposes. Although I previously relied on jQuery and JSONP for this task, I now prefer to achieve it without any framework. Despite numerous attempts, none have bee ...

Using JavaScript to insert a value through AJAX

I'm currently working on a website that displays the value of a .TXT file, and here is the progress I've made so far: <script> $(document).ready(function() { $("#responsecontainer").load("info.txt"); var refreshId = setInterval(function( ...

In Angular 5, a variable's value becomes undefined once it is subscribed to outside of its assigned

I keep encountering an undefined value when trying to assign the subscribed value to a variable in my code snippet below. service.ts getIpAddress() : Observable<any> { return this.http .get(this.Geo_Api) .map((response: ...

the ajax post method

I recently wrote some code that involves two files (index.html and jeu.php). In the index.html file, when I submit a form, I am supposed to be redirected to the other page (jeu.php). However, the issue seems to be with the click event. <html> <he ...

FormData enables uploading of several images through distinct fields simultaneously

Looking to upload images to the server before submitting the form? Unable to nest forms, FormData() is being utilized. The form includes a title and 5 images with captions. The goal is to initiate the upload once an image is selected without clicking &apo ...

steps to attach click event to row of a-table component in ant design vue

Here is the code snippet for the table I am working on. I have imported a-table from antd. To enable click functionality to route to a details page from this listing page, an additional td can be added. <a-table :columns="companiesColumns" :d ...

How does the Express next() function trigger the execution of the following middleware?

Can someone please explain how the next() function is able to call the subsequent middleware or route? I've searched everywhere but can't seem to find a good resource with the actual code. If anyone could share where I may find this information, ...

Utilizing the HTML5 Download attribute for linking to external files

I am currently developing a web application for my own personal needs. One feature I would like to implement is the ability to set the download attribute on certain links. However, I have run into an issue where the files to be downloaded are hosted on ex ...

Employing v-if in conjunction with a sliced string literal

I would like to switch on and off the rendering of a card in my Vue application. Currently, I am utilizing v-if with v-card. Code: myString == "MONEY <v-card v-if="`${myString.slice(4,5)}` == "EY". An error is being triggered saying ...

Cannot trigger event ascn.onchange does not exist as a function

Need to trigger the onChange function and pass parameters within it. Here's what I have tried: setTimeout(function() { document.getElementById(input.key)?.onchange({}) }, 200); Encountering the following error message: cn.onchange is not a function ...