显示和隐藏 v-show.html 926 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <!-- 引入Vue 3 CDN -->
  8. <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
  9. </head>
  10. <body>
  11. <div id="app">
  12. {{ web.show }} <br>
  13. <p v-show="web.show">请问是</p>
  14. <button @click="toggle">显示</button>
  15. </div>
  16. <script type="module">
  17. const { createApp, reactive } = Vue;
  18. createApp({
  19. setup() {
  20. // 定义响应式数据
  21. const web = reactive({
  22. show: true
  23. });
  24. const toggle = () =>{
  25. web.show = !web.show
  26. }
  27. return {
  28. web,
  29. toggle
  30. };
  31. }
  32. }).mount('#app'); // 挂载到DOM元素上
  33. </script>
  34. </body>
  35. </html>