温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

vue2怎么实现传送门效果

发布时间:2023-04-26 15:01:59 阅读:83 作者:iii 栏目:开发技术
Vue开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

本篇内容主要讲解“vue2怎么实现传送门效果”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“vue2怎么实现传送门效果”吧!

先看效果

vue2怎么实现传送门效果

vue2怎么实现传送门效果

代码

Teleport 组件:

<script>
export default {
  props: {
    to: {
      typeString,
      requiredtrue
    },
    disabled: {
      typeBoolean,
      requiredtrue
    }
  },
  inject: ['parent'],
  //   inject 选项应该是:
  // 一个字符串数组,或 一个对象,对象的 key 是本地的绑定名,value 是:
  //     在可用的注入内容中搜索用的 key (字符串或 Symbol),或
  //     一个对象,该对象的:
  //          from property 是在可用的注入内容中搜索用的 key (字符串或 Symbol)
  //          default property 是降级情况下使用的 value
 
  // Vue 选项中的 render 函数若存在,则 Vue 构造函数不会从 template 选项或通过 el 选项指定的挂载元素中提取出的 HTML 模板编译渲染函数。
  render() {
    return <div class="Teleport">{this.$scopedSlots.default()}</div>
    // $scopedSlots用来访问作用域插槽。对于包括 默认 slot 在内的每一个插槽,该对象都包含一个返回相应 VNode 的函数。
  },
  watch: {
    disabled() {
      if (!this.disabled) {
        // this指向组件的实例。$el指向当前组件的DOM元素。
        document.querySelector(this.to).appendChild(this.$el);
      } else {
        this.parent.toSourceDom(this.$el);
      }
    }
  },
  mounted() {
    if(!this.disableddocument.querySelector(this.to).appendChild(this.$el)
  },
  methods: {},
};
</script>
<style lang="scss" scoped>
.Teleport {
  width100%;
  height100%;
}
</style>

index.vue 中引用 Teleport.vue 组件

<template>
  <div>
    <Teleport v-if="isShow" :disabled="isTeleport" to="body">
      <div class="cover">
        <div class="inner">
          我是弹窗
          <div class="close" @click="closed">关闭按钮</div>
        </div>
      </div>
    </Teleport>
    <button @click="show">显示</button>
  </div>
</template>
<script>
import Teleport from "./Teleport.vue";
import model from "./model.vue";
export default {
  data() {
    return {
      isShowfalse// 控制 Teleport 组件挂载时机
      isTeleportfalse// 控制什么时候被传送
    };
  },
  provide() {
    return {
      parentthis,
    };
  },
  components: { Teleport, model },
  methods: {
    show() {
      this.isShow = true;
    },
    closeModel() {
      this.isShow = false;
    },
    toSourceDom(dom) {
      document.getElementById("sourceBox").appendChild(dom);
    },
  },
};
</script>
<style lang="scss" scoped>
.cover {
  position: fixed;
  top0;
  left0;
  right0;
  bottom0;
  backgroundrgba($color: #000000, $alpha: 0.3);
  .inner {
    width500px;
    height300px;
    border-radius10px;
    background#fff;
    color: red;
    font-weight600;
    position: absolute;
    left40%;
    top30%;
    text-align: center;
    font-size30px;
    .close{
      position: absolute;
      bottom0;
      right0;
      background: skyblue;
      padding10px;
      cursor: pointer;
      border-radius10px 0 0 0;
    }
  }
}
</style>

到此,相信大家对“vue2怎么实现传送门效果”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

原文链接:https://blog.csdn.net/yinge0508/article/details/125760442

vue
AI

开发者交流群×