v-cloak does not work in vue.js?

vue.js

vue.js Problem Overview


There is a div in my page that for show the error message.When I refresh the page,it will appear for a while then it disappear. I added v-cloak but it doesn't work.

this is the code, showErrorMsg is false

<div v-cloak v-show="showErrorMsg" style="z-index:100" class="h5_tips tips_error">
  <a href="#" v-on:click="showErrorMsg = false" class="del"><i>&#xe906;</i></a>
  <p v-text="errorMsg"></p>
</div>

How to fix this?

vue.js Solutions


Solution 1 - vue.js

Just include this code to your css file

[v-cloak] { display:none; }

http://vuejs.org/api/#v-cloak

Usage example:

<div class="xpto" v-cloak>
    Hello
</div>

> This directive will remain on the element until the associated Vue > instance finishes compilation. Combined with CSS rules such as > [v-cloak] { display: none }, this directive can be used to hide > un-compiled mustache bindings until the Vue instance is ready.

http://vuejs.org/api/#v-cloak

Solution 2 - vue.js

I faced the same issue, and it was due to a conflicting display property on my div. To solve it, I used the !important flag on the [v-cloak] as:

[v-cloak] {
	display: none !important;
}

.my-class {
	display: table-cell;
}

Solution 3 - vue.js

Vue.js - 2.3.4, I added the v-cloak on the app container, adding this on the parent container, I find your not repeating the code keeping it DRY.

HTML:

<div id="app" v-cloak>
 Anything inside gets the v-cloak
</div>

CSS:

[v-cloak] {
 display:none;
}

Codepen Example:

Solution 4 - vue.js

I fixed this problem by rewriting the CSS and adding a class in the CSS file

CSS:

[v-cloak] .v-cloak--hidden{
  display: none;
}

HTML:

<div v-show="showErrorMsg" style="z-index:100" class="h5_tips tips_error v-cloak--hidden">
  <a href="#" v-on:click="showErrorMsg = false" class="del"><i>&#xe906;</i></a>
  <p v-text="errorMsg"></p>
</div>

Solution 5 - vue.js

If you are using CDN to get Vue and using Bootstrap CSS then the reason might be you are loading the Bootstrap CSS in the bottom of the body tag, moving it back to the head tag worked for me. Make sure that you keep that vueJS file in the button as it is.

<HTML>
<head>
  All link and script tag here
</head>
<body> 
  <div id='app' v-cloak>
    {{ something }}
  </div>
  <script src="app.js"></script>
</body>

Solution 6 - vue.js

I have a scenario where I've got a search form and below, the results with a v-if block, waiting for submit.

I changed the v-if to v-show, that seemed to help. I tried the v-cloak--hidden class but didn't work either (v-cloak is already styled to display: none). What did work apparently was to set the data results container display style to none and then when submitting the form (in the processForm method), setting the display to block and show all the results.

  processForm() {
      // reset page number on new search          
      document.getElementById("data-container").style.display="block"
      this.pageNumber = 1;
      this.remoteRequest();
  },

Without that change, on a full page refresh I could still see the moustaches coming up. Here the full HTML page, simplified.

<div id="app" v-cloak>
      <main class="main-section">
        <form method="POST" class="p-4 text-center" @submit.prevent="processForm">
            <button type="submit" name="button" class="btn btn-primary">
               Find
            </button>
        </form>

        <!-- Results  -->
        <div id="data-container" class="container-fluid pt-2 pb-4" style="display:none">
           <div class="row">
              <div class="col">
                 <div id="recordsContainer" v-show="totalInScreen > 0">
                    <div class="card mt-5" v-for="(result, index) in results" :id="'itm-'+index" :key="result.id">
                      <div class="card-body">
                        <div class="row">
                          <div class="col">
                             <h2 class="doctor-name">{{ result.doct_name }}</h2>
                             <h2 class="doctor-bio">Bio</h2>
                             <p>{{result.doct_bio}}</p>
                          </div>
                        </div>
                      </div>
                    </div>
                 </div>
              </div>
           </div>
        </div>
      </main>
  </div>

Solution 7 - vue.js

Unfortunately the above 2 answers didn't work for me as the problem was something else. Since this questions pops up #1 on Google, I thought I'd share my solution.

If you've defined a display css rule somewhere that's more specific, it will break the v-cloak functionality. However! Do not despair - simply copy this into your CSS file and it will work!

[v-cloak] .v-cloak--block {
  display: block!important;
}

[v-cloak] .v-cloak--inline {
  display: inline!important;
}

[v-cloak] .v-cloak--inlineBlock {
  display: inline-block!important;
}

[v-cloak] .v-cloak--hidden {
  display: none!important;
}

[v-cloak] .v-cloak--invisible {
  visibility: hidden!important;
}

.v-cloak--block,
.v-cloak--inline,
.v-cloak--inlineBlock {
  display: none!important;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSong YongtaoView Question on Stackoverflow
Solution 1 - vue.jsEduardo StuartView Answer on Stackoverflow
Solution 2 - vue.jsPleymorView Answer on Stackoverflow
Solution 3 - vue.jsMichael BuckView Answer on Stackoverflow
Solution 4 - vue.jsSong YongtaoView Answer on Stackoverflow
Solution 5 - vue.jsR. GurungView Answer on Stackoverflow
Solution 6 - vue.jscdsaenzView Answer on Stackoverflow
Solution 7 - vue.jsStan SmuldersView Answer on Stackoverflow