Is it possible for the connectedCallback() method within a Custom Component to have varying interpretations based on the specific context in which it is implemented?

I have recently developed a Custom Component that incorporates a Vue instance:

class ContentCardExample extends HTMLElement {
  connectedCallback() {
    const card = document.createElement('div');
    card.setAttribute("id", "app")
    card.innerHTML = 'hello is {{hello}}'
    this.appendChild(card);
    new Vue({
      el: "#app",
      data: {
        hello: 5
      }
    })
  }
}

customElements.define('content-card-example', ContentCardExample);
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<content-card-example></content-card-example>

This component functions perfectly individually (as shown in the code snippet above directly in a web browser)

However, when I attempted to integrate it into Home Assistant, which allows for customized cards through a Custom Element displayed by Home Assistant, I encountered a Vue warning (essentially an error): Cannot find element #app

My query now is: Are there scenarios where my Custom Component renders differently based on its usage environment?. This leads to the related question of whether this behavior is normal and expected.

Note: I do not wish to focus on "Home Assistant" specifically (as this may not be the appropriate forum) - but rather understand if this component can be considered "self-contained" and should work universally once functional in a basic HTML file, or if a Custom Element's content still relies on and is influenced by its implementation location. In this case, Home Assistant showcased variations in behavior.

Answer №1

If the Vue Instance is having trouble locating the #app element, you can directly pass your card element to el. To do this, create a variable named vm and use it to call the Vue instance later on. You can then append it to your DOM using vm.$el.

Give this a try:

class ContentCardExample extends HTMLElement {
  connectedCallback() {
    const card = document.createElement('div');

    card.innerHTML = 'hello is {{hello}}';

    const vm = new Vue({
      el: card,
      data: {
        hello: 5
      }
    });

    this.appendChild(vm.$el);
  }
}

customElements.define('content-card-example', ContentCardExample);

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

Warning: An unhandled promise error occurred due to a validation error

I've been facing an issue for the past few days. Currently, I'm diving into learning the MEAN stack, but while trying to create a user on MongoDB using Mongoose schema, I encountered this problem: (node:93337) UnhandledPromiseRejectionWarning: ...

JavaScript communication between clients and servers

I am looking to develop two scripts, one for the client-side and one for the server-side. I came across a snippet that allows for asynchronous calling of JavaScript: <html> <head> </head> <body> <script> (function() { ...

Preventing file overwriting using fs.writefile method

Encountering an issue where the data being saved from a chat room built with socket.io in nodejs is constantly overwritten upon submission of a message. The desired outcome is to append both the username and message to the JSON file as an object, rather th ...

Troubleshooting guide for resolving parse error when installing Open MCT

Hi there! I'm currently in the process of installing NASA's Open MCT (Link) but have hit a roadblock with errors during installation. Upon running npm install, I encountered the following error message: { Error: Parse error using esprima for fil ...

Using SetTimeout with the TextInput component in a React-Native application

Currently, I am working on creating a SearchBar component for my new Android application using React-Native. As a newcomer to React-Native, I created a function called _changeInput() to handle passing text to a local function. Initially, the text pass wor ...

The Lerna command for adding packages fails with an error message stating that the packages are not related

Currently, I am utilizing lerna to manage a multirepo containing interrelated packages. This issue only arose after installing a new operating system. Whenever I attempt to utilize lerna add to add a dependency from one package to another, an error occurs ...

Generate a JSON file using Node.js

I've been honing my skills with Node.js and express by working on a project involving a JavaScript object that generates a dropdown list. The process has been smooth so far. Recently, I integrated mongoose to create a model and saved it. Now, in my co ...

Unable to establish SocketIO callback from client to server resulting in a null object instead

I'm encountering an unusual issue with SocketIO. On the server-side, I am using the emit() method like this: $s.sockets.emit(scope, {some: datas}, function(feedback) { console.log('received callback'); } ) ...

What are the steps for setting up vue-cli on a server?

I have been researching how to install vue-cli. All the tutorials I found show it getting installed on the localhost:8080. Is it not possible to install it on a server? I attempted to install vue-cli on port 8080 on my server, but it ended up being on po ...

Is polymer routing the best choice for large-scale websites?

I am currently working on a large project that consists of various layouts and structures, totaling around 100 different pages. Despite the variations in content, the core elements such as headers and menus remain consistent throughout. To streamline my ...

Filtering and selecting tables in HTML

I am dealing with an HTML table that combines static data and MySQL input. The filtering functionality is working properly, but I am struggling to add the options "yes" and "no" to the selection list. These values are test inputs fetched from MySQL. I need ...

The PHP post method is unable to retrieve the value of a button tag

In my code file login.php, I have the script to validate if the user exists or not, as well as an AJAX call. Inside the form, there is a button tag; When I set type='submit', the PHP works but the AJAX does not; and when I use type='button&a ...

What are the best strategies for addressing cache issues in today's web browsers?

We are currently working on a VueJS-based application and facing a significant caching issue. Despite team members continuously updating the site, we keep receiving feedback about already resolved problems such as typos and misplaced elements. In my atte ...

Capture data from Ajax requests and store them in PHP variables

When setting up a DDBB Insert using $.ajax, I encountered an issue. $(document).on('click','.submitMessage', function(){ content=$('textarea').val(); img=$('#messageImg').val(); stdMsg=$('.ms_stdMsg ...

Issue with Express.js res.append function: Headers cannot be set after they have already been sent

I encountered an issue in my express project where I tried to set multiple cookies using "res.append" in the same request, but I kept getting an error saying "Error: Can't set headers after they are sent.". Can someone help me identify the problem and ...

When using Vuejs2, the list of autosize textareas connected to an expanding array of objects does not properly update their values

After binding https://github.com/jackmoore/autosize to textareas within a v-for loop, I've noticed an unexpected data persistence issue while expanding the array linked to that list: While inputs on the left side move downward as intended, new textar ...

Modifying dynamic input fields based on the selected input field type

Seeking advice on a challenge I'm facing while testing a website. My task is to mask input fields in screenshots after executing tests because we share data with other teams. I've tried using JS before the script by changing the input type to &ap ...

Utilize the power of Facebook login in your Parse client side application by integrating it with the user object

Currently, I am in the process of implementing a login system using both the Parse and Facebook Javascript SDK. While I have successfully implemented authentication on the client side, I am now facing the challenge of accessing the user object (generated ...

How can a child component be imported into a parent component within the same JS file using VueJS3 in Options API mode?

const MamaComponent = { template: ` <div >Mama Component</div> ` } const BabyComponent = { template: ` <div >Baby Component</div> ` } What is the process to import BabyComponent into MamaComponent w ...

Tips for disabling drag slide functionality in Vue with agility

I am working with vue-agile and encountering an issue where videos are causing the slider to move unexpectedly when dragging the player's pointer. Images display fine, but this drag/swipe behavior is disrupting the user experience. Is there a way to d ...