Does Vue.js interfere with classList.remove functionality?

When working with Vue.js, I encountered an issue where elements would briefly flash curly braces to the user before being hidden. To combat this problem, I implemented the following solution:

HTML:

<div class="main hide-me" id="my-vue-element">
    <!-- stuff -->
</div>

CSS:

.hide-me {
    display: none !important;
}

JavaScript:

var myVueElement = document.getElementById("my-vue-element");

if (myVueElement) {
    var myApp = new Vue({
        el: myVueElement
      , data: {
            /* stuff */
        }
      , methods: {
            /* stuff */
        }
    });

    console.log(myVueElement);
    console.log(myVueElement.classList);
    myVueElement.classList.remove("hide-me");
    console.log(myVueElement.classList);
    console.log(myVueElement.getAttribute("class"));

The console output is:

DOMTokenList [ "main", "hide-me" ]
DOMTokenList [ "main" ]
main

Despite these console outputs, the element remains hidden, and even after checking in Firefox and Chrome developer tools, the hide-me class is still present. Interestingly, using jQuery's

$("#my-vue-element").removeClass("hide-me");
successfully removes the class. Any suggestions or insights on this issue would be greatly appreciated. Thank you!

Answer №1

It's generally best to avoid directly manipulating the DOM in Vue. Instead, trust Vue to handle it for you (it's really good at it). You can achieve this by using:

<your-element
  :class="['always-present', {'optional':expression}]"
/>

In this setup, the class optional will be added or removed based on the data-bound expression.

Alternatively, if your goal is simply to show or hide an element, you can utilize v-if, v-show, or v-hide (for example: v-if="expression").


If you're unsure about when it's appropriate to resort to direct DOM manipulation in Vue and want to understand the potential pitfalls associated with each situation, check out Handling Edge Cases.

Answer №2

When working with Vue views/components and their internal template code, the elements remain hidden until Vue is fully initialized.

This means that within app.vue, nothing will be displayed until the router finishes loading the view.

<template>
  <div id="app">
    <router-view/>
  </div>
</template>

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

Optimizing CSS usage within Django: top recommendations

Throughout a 6-month-long project, I have continuously added new URLs. However, I am now encountering an issue when using the extend 'base.html' function on other pages where the CSS overlaps and creates confusion. My question is: What are the b ...

Firebase predeploy error code 254 detected

Upon attempting to deploy my website using the command firebase deploy, I encountered the following error: === Deploying to 'project'... i deploying database, functions, hosting Running command: npm --prefix "%RESOURCE_DIR%" run lint npm ERR! ...

Adjust the styling with jQuery according to the selected value from the dropdown menu

Check out this Fiddle I have a jQuery script that is supposed to change the style of an input box to display:block if the selected value is "<>" (Between). However, currently it is changing the css for all selected items instead of just the target i ...

Overlap with upper picture formatting

I've been struggling to create an ion-card with two images: a main picture and a small book cover. Any ideas on how to achieve this? Note: The layout should have 2 images, one at the top and another as a small book cover. Check out this sample on St ...

What is the reason for CSS to be included in the installation process when running npm install with [someLibraryName

After executing the following command: npm install vue-material I noticed that it installed and replaced some of the css styles in my application, leading to conflicts. To resolve this issue, I had to manually search for the specific piece of css and ove ...

increasing the top margin on an HTML document

Looking to create a specific amount of blank space at the top of your HTML page? The space needs to be exactly X pixels tall. How can this be achieved effectively, while ensuring compatibility with Safari? ...

Can you explain the purpose of the behavior: url(); property in CSS?

While browsing the web, I came across a CSS property that caught my eye. It's called behavior, and it looks like this: #element{ behavior: url(something.htc); } I've never used or seen this property before. It seems to be related to Internet ...

The dilemma between Nuxt Js global CSS and Local CSS

Currently, I am in the process of developing a Nuxt application utilizing an admin template. While I am well-versed in Vue.js, I am finding the aspect of loading assets within Nuxt.js to be a bit perplexing. I am in the process of converting the admin temp ...

I'm working on using CSS to design a layout similar to the one shown in the image below, experimenting with flexbox to achieve the

https://i.sstatic.net/4kNAd.png I am currently facing an issue with the layout of my navigation bar, as it is displaying in a column instead of a row like the format shown in the image. I have tried using flexbox and setting the display to inline-block in ...

What are the best methods for detecting devices in order to create customized CSS styles for iPad and Galaxy Tab?

Ensuring my web application is compatible with various devices is crucial. While I have created a common CSS for all mobile and tablet devices, it seems to be causing some problems specifically on the iPad. After finding a fix tailored for the iPad, I now ...

Event handling for Vuetify's rating component - click

I'm currently utilizing Vuetify's v-rating component to display the average rating based on the v-model. An issue arises when a user tries to update the rating by clicking on the component, as there is no event emitted when the current rating mat ...

Leveraging a personalized Vue directive alongside Inertia.js

Working on a Laravel app with Inertia.js and Vue 3, I want to incorporate a custom Vue directive that I created in a previous project not utilizing Inertia. Here's what I tried in my app.js: app.directive('uppercase',{ updated(el){ ...

Unresolved Issue: Jquery Modal Fails to Activate on Subsequent Click for Ajax-

When I make an Ajax call, I load HTML into a div. This HTML content contains a jQuery modal that opens when clicked. However, on the first click, the modal opens correctly. On subsequent clicks, I receive the following error in the console: Uncaught Type ...

The container that utilizes the grid layout is compact and has a near-invisible height

grid-template: display; font-style: "Josefin Sans", sans-serif; size-of-text: 10px; element-height: 0px; spacing-bottom: 0px; spacing-left: 0px; spacing-right: 0px; spacing-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-up: 0 ...

The event in Vue.js does not trigger for items that have been filtered out of the list

I currently have a list of items, each with a "complete" flag. Depending on the value of this flag, the item is displayed in either view A or view B. These items are fetched through an API request and then filtered client-side using a computed method based ...

Placing a div within a 960 grid system can sometimes result in unexpected spacing

Testing on various browsers: Chrome, IE, Firefox. I'm facing an issue where the BusinessNav ID keeps getting pushed over 3 columns. Desired page layout can be found here I could definitely hack this code to adjust the positioning. However, with so m ...

When utilizing Vuex state within a computed property to trigger the opening of a modal, any modifications are neglected, resulting in the

Within my codebase, I have implemented a dynamic method for adding modal states to the Vuex store and activating them throughout the application. Despite successfully changing the state, I encountered an issue where clicking a button that dispatches the to ...

Incapable of stacking one canvas on top of another

I'm new to javascript and looking for help with positioning a canvas element behind another. The code I have may be a bit messy, so any assistance is greatly appreciated. My goal is to have the canvas named "myCanvas" appear behind "coinAnimation". Th ...

The unique identifier for a specific child element in Firefox

While working on this website: I customized the right sidebar navigation using: .sidenav a:after { display: block; position: relative; z-index: 0; top: -3.6em; left: 0.15em; width: 90%; content: "."; font-family: 'R ...

Turn off the CSS hover effect for a custom button if the ng disabled attribute is set to true

My goal is to disable custom buttons, and the disable(pointer) function is working correctly. The issue arises when we try to hover on the button, as the hover effect still works. I therefore need to disable hover as well. I am looking to apply ng-disabl ...