9

Vue3内置组件Teleport用法详解

 3 years ago
source link: https://segmentfault.com/a/1190000040836687
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Vue3内置组件Teleport用法详解

Vue 3.0 新增了一个内置组件teleport,主要是为了解决以下场景:

有时组件模板的一部分逻辑上属于该组件,而从技术角度来看,最好将模板的这一部分移动到 DOM 中 Vue app 之外的其他位置

场景举例:一个Button,点击后呼出模态对话框

这个模态对话框的业务逻辑位置肯定是属于这个Button,但是按照DOM结构来看,模态对话框的实际位置应该在整个应用的中间

这样就有了一个问题:组件的逻辑位置和DOM位置不在一起

按照以前Vue2的做法,一般是使用position: fixed;等CSS属性强行把对话框定位到了应用的中间位置,属于没有办法的办法,下面展示下teleport的基础用法。

用法非常简单,只需要使用to这个属性就可以把组件渲染到想要的位置

// 渲染到body标签下
<teleport to="body">
  <div class="modal">
    I'm a teleported modal! 
  </div>
</teleport>

也可以使用

<teleport to="#some-id" />
<teleport to=".some-class" />
<teleport to="[data-teleport]" />

必须是有效的查询选择器或HTMLElement

现在我们来封装一个标准的模态对话框

<template>
  <teleport to="body">
    <transition name="dialog-fade">
      <div class="dialog-wrapper" v-show="visible">
        <div class="dialog">
          <div class="dialog-header">
            <slot name="title">
              <span class="dialog-title">
                {{ title }}
              </span>
            </slot>
          </div>
          <div class="dialog-body">
            <slot></slot>
          </div>
          <div class="dialog-footer">
            <slot name="footer">
              <button @click="onClose">关闭</button>
            </slot>
          </div>
        </div>
      </div>
    </transition>
  </teleport>
</template>

<script>
import { defineComponent } from 'vue';

export default defineComponent({
  name: 'tdialog'
});
</script>

<script setup>
const props = defineProps({
  title: String,
  visible: Boolean
});

const emit = defineEmits(['close']);

const onClose = () => {
  emit('close');
};
</script>

使用的时候,只需要

<t-dialog title="LGD是不可战胜的" :visible="visible" @close="onClose"> 这是一段内容,萧瑟仙贝。 </t-dialog>

// ...
const visible = ref(false);

const onDialog = () => {
  visible.value = !visible.value;
};

const onClose = () => {
  visible.value = false;
};

上面我们已经把标准的模态对话框组件完成了,还有另外一种相似的,只需要展示少量文字的轻量级提示组件Message

在上面的例子中,我们总是把TDialog组件写在使用的地方,但是这个Messgae组件,我们在想提示的时候使用一个js语句就把它呼出来,类似于下面的这样

// 呼出一个提示
Message({ message: '这是一条Message消息' });

想使用一个函数呼出来,我们需要准备下这个函数,这个函数的作用就是完成组件的渲染。

const Message = options => {
  // 准备渲染容器
  const container = document.createElement('div');
  // 生成vnode
  const vnode = createVNode(MessageConstructor, options);
  // 渲染
  render(vnode, container);
};

MessageConstructor是什么?就是我们的SFC(单文件组件):

<template>
  <teleport to="#app">
    <transition name="message-fade">
      <div v-show="visible" ref="ins" class="message" :style="customStyle">{{ message }}</div>
    </transition>
  </teleport>
</template>

在线体验

查看代码

以上就是关于teleport组件的基础用法和扩展用法,给我们提供了不少的方便。


Recommend

  • 41
    • 微信 mp.weixin.qq.com 5 years ago
    • Cache

    Python内置OS模块用法详解

    大家好,从今天起早起Python将持续更新由小甜同学从 初学者 的角度学习Python的笔记, 其特 点就是 全文大多由新手易理解的 代码与注释及动态演...

  • 14
    • zhuanlan.zhihu.com 4 years ago
    • Cache

    使用 JSX/TSX 开发 Vue3 组件

    使用 JSX/TSX 开发 Vue3 组件写完下面这篇文章之后就想着要不就着手开发一个完全为 Vue3 设计的 JSX 插件吧,于是就有了:https://github.com/Hcy...

  • 11

    本文源码: GitHub·点这里 || GitEE·点这里 一、基础API简介 1、RestHighLev...

  • 5
    • segmentfault.com 3 years ago
    • Cache

    Vue3中的Teleport(传送门)

    Vue3中的Teleport(传送门)发布于 今天 15:00 动画片多啦A梦相信大家很多都看过,而且近几年又出现了相关的电影,作为小编一个男生来说,一直是对里面的静香念念不忘...

  • 3

    作者:Matt Maribojoc译者:前端小智来源:stackabuse有梦想,有干货,微信搜索 【大迁世界】 关注这个在凌晨还在刷碗的刷碗智。本文 GitHub

  • 7

    关于 vue3 的一个新特性已经讨论了一段时间了,那就是 Portals(传送门) ,它的功能是将模板html移动到

  • 8
    • www.fly63.com 3 years ago
    • Cache

    Vue3中的teleport节点传送

    Vue3中的teleport节点传送更新日期: 2022-06-22阅读量: 6标签: 节点...

  • 11
    • www.fly63.com 3 years ago
    • Cache

    Vue3中插槽(slot)用法汇总

    vue中的插槽相信使用过Vue的小伙伴或多或少的都用过,但是你是否了解它全部用法呢?本篇文章就为大家带来Vue3中插槽的全部用法来帮助大...

  • 6

    公司高级表单组件ProForm高阶组件都建立在jsx的运用配置上,项目在实践落地过程中积累了丰富的经验,也充分感受到了jsx语法的灵活便捷和可维护性强大,享受到了用其开发的乐趣,独乐乐不如众乐乐,为了帮助大家更好的运用jsx,开发提效,特此总结...

  • 13

    本篇文章,作者将分析弹窗、抽屉和折叠面板三个组件的用法特征,并对这三个组件的使场景提供对应的使用建议,能给产品设计的伙伴提供一些参考,希望能对你有所帮助。

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK