2
<template>
  <!--This is my main file -->
  <div id="inputs">
    <h1>언어 관리</h1>
    <v-btn color="primary" elevation="10" large @click="duplicateEl"
      >Add row</v-btn
    >
    <Contents />
  </div>
</template>
<script>
import Contents from "./Contents.vue";
export default {
  name: "LanguageMainMenu",
  components: { Contents },
  methods: {
    duplicateEl() {
      alert("You can duplicate buttons");
    },
  },
};
</script>
<style>
h1 {
  text-align: center;
  font-size: 38px;
  padding-top: 20px;
  margin: auto;
}
</style>
3
  • I just need function to make component duplicate thanks!! Commented Jan 7, 2022 at 6:32
  • Donot paste the screenshot of the code. Instead paste the code itself Commented Jan 7, 2022 at 6:49
  • <template> <!--This is my main file --> <div id="inputs"> <h1>언어 관리</h1> <v-btn color="primary" elevation="10" large @click="duplicateEl">Add row</v-btn> <Contents/> </div> </template> <script> import Contents from './Contents.vue' export default { name:"LanguageMainMenu", components:{ Contents }, methods:{ duplicateEl() { alert("You can duplicate buttons") } } } </script> <style> h1 { text-align:center; font-size:38px; padding-top:20px; margin:auto; } </style> Commented Jan 7, 2022 at 6:52

2 Answers 2

3

The best apprach is to use the component inside v-for.

Increment the index when the button is clicked.

Dont forget to use key inside the v-for

Working Fiddle

var example1 = new Vue({
  el: '#app',
  name: "LanguageMainMenu",
  components: {
    Contents: {
      template: `<div>Contents Component</div>`,
    }
  },
  data() {
    return {
      totalCount: 1,
    }
  },
  methods: {
    duplicateEl() {
      this.totalCount++;
    }
  },
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.1.4/vue.js"></script>
<div id="app">
    <!--This is my main file -->
    <div id="inputs">
      <h1>언어 관리</h1>
      <button @click="duplicateEl">Add row</button>
      <Contents v-for="count in totalCount" :key="`component-${count}`" />
    </div>
</div>

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

4 Comments

I did as you said but still the component appears just once. I'm beginner for Vue hope u understand thanks!
Click the Add row button. Can you check the above fiddle? Its working as you said
I'm in development environment of project, there was an error with template when I was running
The overall implementation of the code is with this logic itself. Fixt the template error and try to integrate this logic in your code.
1

You can add a property in data object and use v-for for render buttons. Let method duplicateEl to change the property value.

<v-btn v-for="item in btnNumber" ....>
duplicateEl(){
  this.btnNumber++
}

Comments

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.