Created
February 3, 2020 10:27
-
-
Save crazyones110/79c3a7f928fbe01b9cd995182430b50d to your computer and use it in GitHub Desktop.
创建的时候如何判断时候已经存在了,然后创建新的
保证环境中只有一个,不能有多个
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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