teleport 传送门
传送 组件 或者内容 到特定的区域
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>lesson 30</title> <style> .area { position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); width: 200px; height: 300px; background: green; } .mask { position: absolute; left: 0; right: 0; top: 0; bottom: 0; background: #000; opacity: 0.5; color: #fff; font-size: 100px; } </style> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id="root"></div> <div id="hello"></div> </body> <script> // teleport 传送门 const app = Vue.createApp({ data() { return { show: false, message: 'hello' } }, methods: { handleBtnClick() { this.show = !this.show; } }, template: ` <div class="area"> <button @click="handleBtnClick">按钮</button> <teleport to="#hello"> <div class="mask" v-show="show">{{message}}</div> </teleport> </div> ` }); const vm = app.mount('#root'); </script> </html>
可以把组件的某些 dom 元素传递到特定位置
越努力越幸运