简体   繁体   English

如何在 Vue.JS 中将现有组件添加到模板 onclick?

[英]How to add an existing component to a template onclick in Vue.JS?

I want to be able to add a component to my template by clicking on a button.我希望能够通过单击按钮将组件添加到我的模板中。 Let me show you what I have: Draggable.vue is the component that I would like to add when I click on a button.让我告诉你我有什么: Draggable.vue 是我想在单击按钮时添加的组件。

<template>
  <div ref="draggableContainer" id="draggable-container" @mousedown="dragMouseDown">
    <div id="draggable-header" >
      <slot name="header"></slot>
    </div>
    <slot name="main"></slot>
    <slot name="footer"></slot>
  </div>
</template>

<script>
export default {
  name: 'DraggableDiv',
  data: function () {
    return {
      positions: {
        clientX: undefined,
        clientY: undefined,
        movementX: 0,
        movementY: 0
      }
    }
  },
  methods: {
    dragMouseDown: function (event) {
      event.preventDefault()
      // get the mouse cursor position at startup:
      this.positions.clientX = event.clientX
      this.positions.clientY = event.clientY
      document.onmousemove = this.elementDrag
      document.onmouseup = this.closeDragElement
    },
    elementDrag: function (event) {
      event.preventDefault()
      this.positions.movementX = this.positions.clientX - event.clientX
      this.positions.movementY = this.positions.clientY - event.clientY
      this.positions.clientX = event.clientX
      this.positions.clientY = event.clientY
      // set the element's new position:
      this.$refs.draggableContainer.style.top = (this.$refs.draggableContainer.offsetTop - this.positions.movementY) + 'px'
      this.$refs.draggableContainer.style.left = (this.$refs.draggableContainer.offsetLeft - this.positions.movementX) + 'px'
    },
    closeDragElement () {
      document.onmouseup = null
      document.onmousemove = null
    }
  }
}
</script>

<style>
#draggable-container {
  position: absolute;
  z-index: 9;
  height: 240px;
  width: 152px;
  background-color: rgb(170, 104, 43);
}
#draggable-header {
  z-index: 10;
}
</style>

And here is my App.vue:这是我的 App.vue:

<template>
<div id = 'dd'>
  <button @click="createDraggableDiv">Créer bloc</button>
  <DraggableDiv class="col-11">
    <template>

    </template>
  </DraggableDiv>
</div>


</template>

<script>
import DraggableDiv from './components/DraggableDiv.vue'


export default {
  name: 'App',
  components: {
    DraggableDiv,
  },
  methods:{
    createDraggableDiv(){
       //function to add the DraggableDiv component to my template
    }
  }

}
</script>

<style>


</style>

I already have a DraggableDiv on my app when I launch it but I want to be able to add some more with the button.当我启动它时,我的应用程序上已经有一个 DraggableDiv,但我希望能够使用该按钮添加更多内容。

I you have any idea to help me do that, please tell me!我你有什么想法可以帮助我做到这一点,请告诉我! Thanks!谢谢!

You can achieve this using the v-for directive:您可以使用 v-for 指令实现此目的:

<template>
  <div>
    <your-component v-for="index in count" :key="index"></p>
    <button v-on:click="addComponent">Add</button>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        count: 1
      };
    },
    methods: {
      addComponent() {
        this.count += 1;
      }
    }
  };
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM