Programmatically remove a component from the DOM in Vue 3

I am working on a project similar to a spreadsheet with draggable cells, where I can move events, which are components, around.

One of the challenges I'm facing is removing a component after adding it dynamically. I have tried using this code snippet:

let element = document.getElementById("newComponent")
element.parentNode.removeChild(element)

While this solution partially works, I've encountered an issue where the old component's data remains in the 'data-v-app' dataset, preventing me from adding new components to the same div. Is there a way to fully remove the old component and clear its associated dataset, or is this a limitation of Vue?

Answer №1

v-if remains the preferred method here as it leverages Vue's reactivity.
It is advised not to alter the DOM directly as it follows an imperative approach similar to jQuery, requiring explicit instructions on how to manipulate elements and manage event listeners.

VueJS operates in a declarative manner, resulting in a simpler API. By establishing bindings between variables and actions, Vue automatically responds to changes without the need for detailed step-by-step commands.


As your project scales, using imperative techniques such as jQuery or vanilla JS (document.getElementById) can become inefficient as they demand manual descriptions of all reactions to variable changes. This often leads to repetitive code for multiple dependencies.

In contrast, with Vue, you can define conditions through the API which handles updates autonomously, significantly reducing complexity. Additionally, powerful directives and Vue devtools simplify development.


Attempting to combine Vue state with imperative coding results in a challenging hybrid approach that complicates maintaining reactivity while still requiring declarative code. Utilizing Vue's API along with template refs when necessary is strongly recommended.
Avoid reliance on imperative practices like jQuery or manual DOM element selection for easier management, reduced complexity, and improved performance.


Vue/React were created to streamline development by minimizing complexity. Embrace their capabilities to maximize efficiency.

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

Is it possible for a Titanium application to utilize the iOS sharing functionalities?

I am looking to integrate a share feature in my app that allows users to easily share content by clicking on a share icon. I want the iOS share page to appear from the bottom of the screen, giving users options to share via message, mail, Twitter, and Face ...

Create a dynamic HTML page using JavaScript

When I click on the following links: How can I create a new page using JavaScript? Create and dynamically append elements I'm looking to dynamically add HTML elements with JavaScript within a div, but I don't want my code to become overly comp ...

Retrieving a database value in Node.js Firestore containing a space

Hello, I'm just getting started with node Js and I anticipate that it will be a smooth ride! Currently working on an application using node JS with Firestore where I need to retrieve data like "display name": James and "Age": 22 Since Age does not h ...

Increasing the top padding of a resized iframe thanks to iframe-resizer

Currently, I am utilizing the remarkable iframe-resizer library to resize an iFrame while keeping it in focus. The challenge I am facing is my inability to figure out how to include additional padding at the top of the iFrame once it's resized. The ...

CSRF Token currently disabled

I am working on creating a VueJs Form to be included in a php.blade file where users can leave comments and rate between 0 and 5 stars. The challenge I am facing is the possibility of a CSRF attack, even though I have implemented a CSRF token in my form. ...

View a pink map on Openlayers 2 using an iPhone

I am currently working on a project where I am trying to track my location using my smartphone and display it on a map. To achieve this, I am utilizing openlayers 2. However, I am encountering an issue. When I implement the code below in a Chrome Browser ...

Attempting to bring in an image file into a React component

My attempt to add an image at the top of my file failed, so I am looking for help with importing it. The code I originally tried did not work. The image does not display using the code below <img src={require('../../blogPostImages/' + post.bl ...

I have been unable to find a solution for the non-functioning jQuery (3.4.1 / 3.3.1) load() issue

I have been working with jQuery's .load() function Take a look at my code snippet: <html> <head> <meta charset="utf-8"> <title>load demo</title> <script src="https://code.jquery.com/jquery-3.4.1.js"> ...

Configuring properties of a child component in VueJS

Currently, I'm exploring the method of utilizing a Vue template as a Kendo UI template in their components. However, I found that there's a lack of clarity on how to provide properties to components rendered using this approach instead of directl ...

Tips for accessing the Vue store from a component while implementing it with a router

I'm encountering an issue with accessing a variable in the Vuex store from a component. Despite trying to access the value, it consistently appears as empty. I am utilizing routers for navigation between components. router.js const routes = [ { ...

Attempting to conceal a div element along with its contents using AngularJS

I am attempting to use AngularJS to hide a div and its contents. I have defined a scope variable initialized as false and passed it to the div in order to hide it. However, the div is still visible along with its content. <script type="text/javascr ...

What causes the error "property does not exist on type" when using object destructuring?

Why am I encountering an error in TypeScript when using Object destructuring? The JavaScript code executes without any issues, but TypeScript is showing errors. fn error: This expression is not callable. Not all elements of type '(() => void) | ...

Column reverse flex-direction is functioning correctly, however, it is imperative that it does not automatically scroll to the bottom

I have a Flexbox set up where I need the contents to display in reverse order. Everything is working well, but the list has many items and the newest ones are appearing at the top. Currently, the Flexbox automatically scrolls to the bottom, but I want it t ...

Using a vanilla JS object as a prop for a child component

I have created a custom Message class in my application to handle incoming messages, which is defined in message.js. Within message.js, I've implemented two classes: Message and EventEmit. The render function in my Message class requires passing an E ...

Is your webpage slow to respond after a page-refresh? (Delayed HTML rendering causing lag)

Whenever I adjust the screen size to that of a phone or any device smaller than 768px, the search bar doesn't display in its proper position until the page is refreshed. It should be correctly placed right from the start. Furthermore, when I resize th ...

Can Node.js be utilized to generate an XLS excel file while specifying the cell type?

I've noticed that many libraries have the capability to export to XLSX (Excel > 2007) or CSV formats, but not in XLS (which I assume is due to its outdated file format). I came across something where if you use fs.createOutputStream("filename. ...

Guide to dynamically displaying different URLs based on checkbox selection using Ajax

My goal is to dynamically change the view of a page based on which checkbox is checked. Additionally, I want to ensure that when one checkbox is selected, another becomes unchecked. <input class="searchType" type="checkbox"></input> <input ...

Is it possible to transform JSON Array Data from one format to another?

As someone who is new to the software field, I am working with a JSON array of objects: var treeObj = [ { "name": "sriram", "refernce_id": "SAN001", "sponer_id": "SAN000" }, { "name": "neeraja", "r ...

Is there a way to ensure that the await subscribe block finishes before moving on to the next line of code?

My goal is to utilize the Google Maps API for retrieving coordinates based on an address. In my understanding, using await with the subscribe line should ensure that the code block completes before moving on to the subsequent lines. async getCoordinates ...

Implementing a function in jQuery to create a "Check All" and "Uncheck All" button

Can someone please guide me on how to implement a check all and uncheck all functionality when I check individual checkboxes one by one? Once all checkboxes are checked, the 'checkall' checkbox should automatically be checked. Below is the code s ...