【发布时间】:2018-09-24 10:11:18
【问题描述】:
这是我的代码,这只是一个示例代码,如果以下内容有效,那么这将帮助我构建我正在处理的其他东西。
<template>
<div id="wrapper">
<div id="divOne">
<!-- Add or remove component here dynamically -->
</div>
<div id="divTwo">
<!-- Add or remove component here dynamically -->
</div>
<!-- There will be more divs like #divOne #divTwo above -->
<div>
<input type="radio" id="one" value="divOne" v-model="pickedDiv">
<label for="one">One</label>
</div>
<div>
<input type="radio" id="two" value="divTwo" v-model="pickedDiv">
<label for="two">Two</label>
</div>
<button @click="addComponent">Add Component</button>
</div>
</template>
<script>
import SomeComponent from './SomeComponent'
export default {
data() {
return {
pickedDiv: '',
pickedDivPreviously: ''
propItems: ['item1', 'item2']
}
}
methods: {
addComponent () {
//-- This is not working code but I need something like this --//
this.pickedDivPreviously = this.pickedDiv // event not sure how to get previously selected div
const divThatIsPicked = document.getElementById(this.pickedDiv)
const divThatWasPickedPreviously = document.getElementById(this.pickedDivPreviously)
// code here to remove/empty/destroy old component from 'divThatWasPickedPreviously'
divThatWasPickedPreviously.innerHTML = ""
// code here to add new component in 'divThatIsPicked'
divThatIsPicked.appendChild('<some-component :someProp="propItems" @someEvent="someFn">')
}
}
}
</script>
我不想分散您回答实际问题的注意力,但是如果您对我的工作感到好奇,请查看this :) 在这里,我尝试在任何行的末尾添加新的子 DIV项目被点击。
如果this 被转换为 vue 比上面提出的实际问题,我会非常高兴,如上所述,如果你觉得很难,请不要从实际问题中分心:)
【问题讨论】:
-
有可能。查看这篇文章:css-tricks.com/…
标签: javascript vue.js vuejs2 vue-component