5

I have code that looks like the code below. how do I set this up so the components "dataPart1' and "dataPart2" won't load unless a user clicks a button to view the data? In other words, I don't want the data to just automatically show, but only if the user chooses to see it via a button click option.

var app = new Vue({
  el: '#data',
  data: {
    show: true,
    something: true,
  },
  components: {
    dataPart1,
    dataPart2
  },
  ...other stuff...
  }
});
3
  • What do you mean by "load"? Do you mean you don't want the component definitions to be loaded, saving bandwidth or do you mean you just don't want them to display? Commented Oct 25, 2018 at 23:54
  • hi @Phil by load i want the definitions to be loaded Commented Oct 26, 2018 at 15:33
  • 3
    Then why have you accepted an answer that does not do that? Commented Oct 26, 2018 at 22:21

1 Answer 1

7

In order to conditionally display content in the templates (text, html, components, etc), we can use conditional rendering of Vue.js and specifically v-if directive.

Short explanation: we can display content depending on certain variables defined in our component. And change value of that variable on certain events, e.g. button click.

Please, take a look at example on CodeSandbox of loading two components conditionally on buttons clicks.

In App.vue file, we have 2 buttons that trigger a handler on click and display component's if corresponding value is true.

Edit Vue Template

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

2 Comments

is there a way to add a new component each time I click on a single button? then I would use a close button in each component so it works properly.
Displaying and Mounting are 2 separate things...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.