vue2实现Teleport,用来做跨组件DOM挂载
作者:admin 日期:2022-04-20父组件调用:(to="#app"的意思是把#divPopup挂载到#app节点上)
Javascript代码
- <Teleport to="#app">
- <div id="divPopup" style="z-index:20000;" v-show="dialogVisible">
- <el-button @click="clickH1">yes</el-button>
- <el-button @click="clickH2">no</el-button>
- </div>
- </Teleport>
子组件:
Javascript代码
- <script>
- export default {
- name: 'teleport',
- props: {
- /* 移动至哪个标签内,最好使用id */
- to: {
- type: String,
- required: true
- }
- },
- mounted() {
- document.querySelector(this.to).appendChild(this.$el)
- },
- destroyed() {
- document.querySelector(this.to).removeChild(this.$el)
- },
- render() {
- return <div>{this.$scopedSlots.default()}</div>
- }
- }
- </script>