- v-show
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
<h2>{{web.show}}</h2>
<hr>
<p v-show="web.show">这是一段文字,可通过按钮隐藏</p>
<button @click="toggle">切换显示状态</button>
</div>
<script type="module">
import { createApp, reactive } from './vue.esm-browser.js'
createApp({
setup() {
const web = reactive({
show: true,
})
const toggle = () => {
web.show = !web.show;
}
return {
web,
toggle
}
}
}).mount("#app")
</script>
</body>
</html>
原理
style="display: none;"