Skip to content

Instantly share code, notes, and snippets.

@crazyones110
Created February 3, 2020 10:27
Show Gist options
  • Save crazyones110/79c3a7f928fbe01b9cd995182430b50d to your computer and use it in GitHub Desktop.
Save crazyones110/79c3a7f928fbe01b9cd995182430b50d to your computer and use it in GitHub Desktop.
创建的时候如何判断时候已经存在了,然后创建新的 保证环境中只有一个,不能有多个
<script>
export default {
data() {
return {
currentDiv: null
}
},
methods: {
createDiv() {
if (!this.currentDiv) {
const div = document.createElement('div')
div.id = 'test-div'
div.innerText = parseInt(Math.random() * 100)
document.body.appendChild(div)
this.currentDiv = div
} else {
this.currentDiv.remove()
const div = document.createElement('div')
div.id = 'test-div'
div.innerText = parseInt(Math.random() * 100)
document.body.appendChild(div)
this.currentDiv = div
}
}
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment