Trouble accessing properties in Vue.js mounted function

Despite my experience with Vue.js, I found myself feeling a bit like a mad scientist this morning :). The task at hand involved initializing Google Maps within the mounted function of a component - a process I'm quite familiar with. However, this time, I encountered an unexpected behavior where accessing the same `this` variable within the `mounted` function seemed to be shared when using the component multiple times on my page.

While the props returned correct values within the template and methods, the mystery deepened as both instances logged the exact same object and ID in the console, even sharing the `_uid` property. This oddity extended to modifications made to the `this` variable in one instance affecting the other component as well, leading to a perplexing situation of shared objects.

If anyone has faced a similar predicament or has insights on how to resolve it, I would greatly appreciate your input.

Answer №1

Do not use self-closing tags for components.

It is important for Vue templates to adhere to valid HTML standards. Self-closing tags, which are more common in XHTML syntax, should be avoided as they are considered outdated.

(Update:)

In version 2.0 of Vue, self-closing tags can be used if you refrain from using in-dom templates.

You might also encounter issues with camelCase vs. kebab-case. The code snippet below demonstrates the correct behavior.

Vue.component('locationInput', {
  template: '#location-input-template',
  props: ['id'],
  mounted() {
    console.log('Component object representation ==> ', this._uid)
    console.log('ID ==> ', this.id)
  }
});

new Vue({
  el: '#my-travel-app'
});
<script src="//cdnjs.cloudflare.com/ajax/libs/vue/2.2.6/vue.min.js"></script>
<template id="location-input-template">
  <div class="LocationInput">
    <input type="text" :id="id">
  </div>
</template>

<div id="my-travel-app">
  <location-input id="id1"></location-input>
  <location-input id="id2"></location-input>
</div>

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

Displaying a PDF file in a browser using a byte array with JavaScript without the need for base64 encoding

I am currently dealing with an ASP .NET Web API where the POST function is sending back a byte array in C# code like this: return Ok(outputStream.ToArray()); outputStream is of type System.IO.MemoryStream and ToArray() returns a byte array. The purpose ...

Enhance ant design modal functionality by enabling resizing capabilities

I'm experiencing an issue with the Ant Design Modal component as it does not support resizing the modal window. I'm looking for a way to enable manual resizing of the modal window once it has been opened. Is there a solution available for this fu ...

Tips on building a carousel with slot elements

Many people have shown me the traditional way of creating a carousel using an array of images. However, I find this method to be limiting because I want each slide of my carousel to include a lightbox component. Instead of having to rewrite the lightbox fu ...

Sending information from a parent component to a child component in Vue.js and then showcasing the data

Hey there! I'm new to vue.js and I'm struggling to figure out why my data is not being passed to the child component. I've tried a few different approaches, but none seem to be working as expected. It feels like I'm really close, but so ...

Display the webpage exclusively when the application has been set with `app.use('/api')` in Express 4

Here is what I currently have: app.js ... var api = require('./routes/api'); app.use('/', api); app.use('/api', api); ./routes/api ... var router = express.Router(); router.get('/', passport.authenticate(' ...

Having trouble with clicking a button in HTML?

I'm facing an issue with a button on my HTML page that features a background created using the popular particle library. The button is visible but not clickable. I've attempted adjusting the background size and alignment without success. How can ...

Delving Into the Mysteries of Bootstrap Modal Fading

After updating our Bootstrap plugins file to version 2.2.1, I encountered an issue with adding the 'fade' class to modals. Previously, we had been using data-* api references for modals but decided to switch over. However, when I tried to incorpo ...

Improve the translation animation on an element containing numerous child nodes

Looking for ways to enhance the smoothness of the transition in the "infinity list" animation. While it's just a demo at the moment, the real app will have various elements emerging from each "pin". The main performance bottleneck seems to stem from t ...

Automatically Transmit Input Values Using JavaScript

I encountered a challenge while trying to transfer the value from one text input box to another as the user types. Everything was functioning perfectly on jsfiddle, but I couldn't replicate the success elsewhere. It's worth noting that I am utili ...

How can I implement Google Analytics Event tracking for all <audio> tags using Javascript?

I am looking to implement a Google Analytics Event for all audio tags on my website. Currently, I have a script that targets audio tags with a specific class: <script> /* a test script */ $(function() { // Execute function when any element with t ...

Passing an event handler from one Vue component to another Vue component results in an error: The handler for the "updatefilters" event is invalid and is undefined

I am dealing with some nested components. The top level component that is present in the page HTML looks like this: <advanced-options-model ... @updatefilters="updateFilters"></advanced-options-model> It's important to note how the updat ...

What is the best method for choosing all options in a select box in IE without experiencing the scrolling effect?

Here's the scenario: I have a select element with multiple options and a checkbox that allows users to select/deselect all options. The issue arises in Internet Explorer, where selecting all options using code causes the entire select box to scroll un ...

Javascript and the Cookie Conundrum

My goal is to use Javascript to create a cookie that stores the value of an input field with the id "username" every time a button is pressed. Then, I want to retrieve and display that cookie value on the website. I attempted to implement this myself to te ...

"Proceeding to" express this redirection

Greetings, I am currently struggling to understand why res.redirect('/index') is rendering like this: <p>OK. Redirecting to <a href="/index">/</a></p> Instead of directly redirecting on the page. I searched through th ...

How to organize a nested JSON array object in JavaScript

var dataSource = ({ "Items": ({ "Deserts": ({}), "Veg": ({ "VegPulao": "Veg Pulao", "PalakPaneer": "Palak Paneer", "PaneerButterMasala": "Paneer Butter Masala" }), "Chicken": ({ "Tandoori": "Tandoori special ...

What is the best approach to displaying multiple instances of a single class in React, similar to the layout used in platforms such as Instagram, Facebook

I am looking to display a component (card) showcasing a product multiple times. The data for the component is pulled from a database, so all I need to do is render the <Product /> component with different props. In the image below you can see the com ...

Pinpointing a specific region within an image by utilizing offsets

Would it be possible to detect a specific area within an image using a predefined set of image offsets? Here is the image and accompanying offsets: Sample Image Below are the sample image offsets for the highlighted area: Offset(63.4, 195.2) Offset(97.7 ...

Why does jQuery.get() keep altering the URL I'm using?

I encountered an issue with the code snippet below, where I attempted to fetch a resource using an Ajax jQuery call. $.get({ url: "http://jennifer-lawrence/mygirl/FreeTonight.php", type: "GET", dataType: "json", ...

Transferring String data between Java and JavaScript using Webview in both directions

I'm currently developing an application that allows two users to communicate via a webview. My goal is to transfer a String variable from JavaScript to Java in order to store it in my SQLite database, and also be able to do the reverse operation as we ...

In search of the most recent content inputted into a textarea

I'm facing an issue with a textarea that I have on my webpage. The problem arises when I select values from an autocomplete field and then try to update the textarea with these selected values. For instance, if I enter the characters 'a', & ...