0

I'm new to Vue.js, I just read the documentation on conditional rendering here (https://v2.vuejs.org/v2/guide/conditional.html) but somehow can't get it to work...

My code:

<button onclick="showTemplate()">Teste</button>


<template v-if="app">

    <div id="app">
      {{ message }}
    </div>

</template>

function showTemplate(){
    var app = new Vue({
      el: '#app',
      data: {
        message: 'Hello Vue!'
      }
    })
}

I want the template tag to render only after app has been instantiated. I have also tried variations of this code without success. Can someone help out?

4
  • what you are using? CDN or CLI? Commented Jul 6, 2017 at 14:33
  • I'm using CDN.. <script src="unpkg.com/vue"></script> does the code differ from both? Commented Jul 6, 2017 at 14:34
  • Yeah, if you are starting to learn vue.js, much better you do the best practice. Commented Jul 6, 2017 at 14:47
  • 1
    One minor comment: you are using v-if on the template tag, which is outside of the div which is having the Vue instance actually mounted to it. So I believe that that <template> tag will never have access to that Vue function, even after the Vue instance is created. Commented Jul 6, 2017 at 14:49

2 Answers 2

2

Use v-cloak. The intended use of v-cloak is to hide the Vue until it is instantiated.

function showTemplate() {
  var app = new Vue({
    el: '#app',
    data: {
      message: 'Hello Vue!'
    }
  })
}
[v-cloak] {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.3.4/vue.js"></script>
<button onclick="showTemplate()">Teste</button>

<div id="app" v-cloak>
  {{ message }}
</div>

Here is how you might hide your "loading" screen when data is retrieved. In this case, the button will serve as our loading screen.

var app = new Vue({
  el: '#app',
  data: {
    message: 'Hello Vue!',
    loaded: false
  },
  methods: {
    getData() {
      setTimeout(() => this.loaded = true, 2000)
    }
  }
})
[v-cloak] {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.3.4/vue.js"></script>

<div id="app" v-cloak>
  <button v-if="!loaded" @click="getData">Get Data</button>

  <div v-if="loaded">
    {{ message }}
  </div>
</div>

Sign up to request clarification or add additional context in comments.

2 Comments

This is good, but how do I do the opposite? If I want to hide my "loading" screen after I fetch data from my API and create the vue object?
@raphadko That is not quite the question you asked. However, there are a number of ways to do that. One, you could have the loading screen controlled by Vue, show the loading screen when the page is loaded and hide it when data is returned using the conditional rendering techniques you read about. If, for some reason, your loading screen is outside Vue, you could still hide it from Vue code once data is retrieved.
0

Try this and remove the button. You don't need to call the instance within the function.

// put it on your index.html
<div id="app">
  {{ message }}
</div>

// put it on your script make sure that you have already a CDN of vuejs.
var app = new Vue({
   el: '#app',
   data: {
      message: 'Hello Vue!'
   }
})

2 Comments

well, I don't want to load 'app' right away, but to the click of a button.. and I dont want to display the raw tag {{message}} on my page before I click the button.
@raphadko you want to display that message when you click the button? Am I correct?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.