Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Crazylife's avatar

How can i dynamically add component template within vue?

I have a main.vue and then got some action buttons there. When i clicked on the button, i wanna to add or loop the component template into my main.vue. The component template consists form data like input, select option, checkboxes.

How can i dynamically add the component into my main.vue?

0 likes
12 replies
tykus's avatar

Your description is unintelligible without the underlying code.

MohamedTammam's avatar

Use boolean variable.

<your-component v-if="yourVariable" />
<button @toggle="yourVariable = !yourVariable">Toggle</button>
Crazylife's avatar

For example, In my main.vue

<template>
<div>
<button @click="addPlan">Add Plan</button>
</div>
</template>

Then i have a AddPlan.vue looks like this

<template>
<div>
<form>
<input />
<input />
<input />
.....
</form>
</div>
</template>

How can i keep adding the AddPlan.vue when i click the button in Main.vue?

tykus's avatar

@Crazylife assuming the addPlan method in the main.vue component toggles some state (e..g addingPlan), then this will work:

<template>
  <div>
    <button @click="addPlan">Add Plan</button>
    <AddPlan v-if="addingPlan" />
  </div>
</template>

Your main.vue component will import and use the AddPlan component.

// main.vue
import AddPlan from './AddPlan'
export default {
  components: { AddPlan },
  data () {
    return {
      addingPlan: false
    }
  },
  methods: {
    addPlan() {
      this.addingPlan = true
    }
  }
}
Crazylife's avatar

@tykus I am actually want to dynamically add the addPlan component, not just adding one but can be multiple.

MohamedTammam's avatar

@Crazylife Use for loop with a counter.

<template>
  <div>
    <button @click="addPlan">Add Plan</button>
    <AddPlan v-for="i in counter" :key="i" />
  </div>
</template>

<script>
import AddPlan from './AddPlan'
export default {
  components: { AddPlan },
  data () {
    return {
      counter: 0
    }
  },
  methods: {
    addPlan() {
      this.counter += 1;
    }
  }
}
</script>
Crazylife's avatar

@tykus It will be multiple AddPlan component, when i clicked add. The AddPlan component is a set of form with input, etc which need user to fill in plan details.

<AddPlan />
<AddPlan />
<AddPlan />

I will need store the data in this format when the plan details filled up by user

plans: [
{....}, // plan 1 details
{....}, // plan 2 details
... so on
]
Crazylife's avatar

@MohamedTammam I will need store the data in this format when the plan details filled up by user

plans: [
{name: "plan1",....}, // plan 1 details
{name: "plan2",....}, // plan 2 details
... so on
]

If i am using the method you mentioned, how should i do to get the filled input value in object and assign to my plans?

I need to do something like this right?

  <AddPlan v-for="i in counter" :key="i" :plans="plans"/>
Crazylife's avatar

@tykus Oh, i think i got what you mean. Instead of put action button in Main.vue. I just move it into Plan.vue and use v-model to return the value right?

Please or to participate in this conversation.