Home >Web Front-end >Vue.js >An in-depth explanation of how to use the Teleport component in Vue
How to use Teleport component in Vuejs? The following article will show you how to use the Teleport component in Vue. I hope it will be helpful to you!
In components with a relatively complex DOM
structure and deep hierarchical nesting, some logic needs to be processed based on the corresponding module business. This logic It belongs to the current component
but from the view of the entire page application, it should be rendered in DOM
elsewhere outside the entire vue
application and cannot affect the component. The structure of In the component, the event bound to the element maintains a certain correlation with the specific
element structure to be controlled in the same component and at the specific location. [Related recommendations:
vuejs video tutorial, web front-end development
] without deliberately separating some DOM structures, however, In the same component, the button that triggers the modal box and the modal box itself are in the same component
Because they are both associated with the switch state of the component, the modal box and the button are rendered together in the application Where the DOM
structure is very deep, it will be very difficult to control the css layout position of the modal box
In view of such scenarios and difficulties,Vue
officially provides a
Components can solve this problem very well, so that developers don’t need to worry about the structure of DOM
##01-When the component hierarchy of components is very deep
For example: Now there are two components, the parent component and the child component. In the descendant component, add a button to pop up a modal box and let it display vertically and horizontally in the center of the page.
<template> <div class="App"> 我是父组件 <Child /> </div> </template> <script setup> import Child from "./Child.vue" </script> <style> .App { width: 400px; height: 400px; background:red; } </style>The following is the
Child component, the sample code is as follows
Child.vue
<template> <div class="child"> <p>我是子组件</p> <button @click="isModel=true">打开模态框</button> <div class="mask-dialog" v-if="isModel"> <div class="box"> <h2>我是标题</h2> <div>我是弹框内容</div> <div> <button @click="isModel=false">关闭</button> </div> </div> </div> </div> </template> <script setup> import { ref } from "vue"; let isModel = ref(false); </script> <style> .child { width: 300px; height:300px; background:green; } /**灰色遮罩层 */ .mask-dialog { width: 100%; height:100%; position:absolute; left:0; top:0; background:rgba(0,0,0,0.5) } .box { width: 200px; height:200px; position:absolute; left:50%; top:50%; transform:translate(-50%,-50%); background:pink; text-align:center; } </style>
There is a
button button in the above sub-component to trigger the opening of the current component The modal box contains the logic to control the display and hiding of the pop-up box. When the nested components are deep and complex
If the parent element has positioning, then when controlling the position of the child element, Using css's
transform
position:absolute to refer to changes in the object will destroy the layout structure and some
cssstyles## will appear.
#Control problems will be very painful to solveThis
Teleport
component is to solve this type of problem. You can specify the
structural fragment , independent of going outside the component and not affected by the current component layout structure
After modification by Teleport
<pre class="brush:js;toolbar:false;"><template>
<div class="child">
<p>我是子组件</p>
<button @click="isModel=true">打开模态框</button>
<Teleport to="body">
<div class="mask-dialog" v-if="isModel">
<div class="box">
<h2>我是标题1</h2>
<div>我是弹框内容</div>
<div>
<button @click="isModel=false">关闭</button>
</div>
</div>
</div>
</Teleport>
</div>
</template>
<script setup>
import { ref } from "vue";
let isModel = ref(false);
</script>
<style>
.child {
width: 300px;
height:300px;
background:green;
}
/**灰色遮罩层 */
.mask-dialog {
width: 100%;
height:100%;
position:absolute;
left:0;
top:0;
background:rgba(0,0,0,0.5)
}
.box {
width: 200px;
height:200px;
position:absolute;
left:50%;
top:50%;
transform:translate(-50%,-50%);
background:pink;
text-align:center;
}
</style></pre>
Receives a to prop
to specify the destination of the transmission. The value of
can be a CSS
selector string, or id
, or it can be a DOM
element object. The function of this code is to tell Vue
to send the following template fragment to the body
tag<pre class="brush:html;toolbar:false;"><Teleport to="#some-id">html结构代码</Teleport>
<Teleport to=".some-class">html结构代码</Teleport>
<Teleport to="body">html结构代码</Teleport>
<Teleport to="html">html结构代码</Teleport></pre>
02-Teleport componentIt is
Vue
is a built-in component officially provided, which can "transfer" a part of the template inside a component to the location of the outer layer of the
structure to a specified location<pre class="brush:js;toolbar:false;"><teleport to="移动到指定的位置,可以是html,body,或id,class">
里面是Html结构模板内容
</teleport></pre>
Note
When mounting, the transferred to target must already exist in
DOM. Ideally, this should be an element outside the entire Vue
application DOM
tree. If the target element is also rendered by Vue
, you need to make sure to mount the element before mounting
095a8c77e9bc5f13289f43baf6c8bdc1
#Place the specified template html
at the specified location on the page. It is conditional and cannot be transferred arbitrarily Before installing the component, the target element must exist, that is ,Targets cannot be rendered by the component itself and should ideally ,be located outside the entire
component tree. The following code will not work
<pre class="brush:js;toolbar:false;"><template>
<div>
<Teleport to=".content">
<div>我是头部的内容</div>
</Teleport>
</div>
<div>
底部内容
<div></div>
</div>
</template>
<script setup>
</script>
<style>
h1 {
color: red;
}
</style></pre>
03-What you need to know
teleport
just changes the rendered
structure, It does not affect the logical relationship between components. That is to say, if
095a8c77e9bc5f13289f43baf6c8bdc1. Incoming props
and triggered events will also work as usual. <p>这也意味着来自父组件的注入也会按预期工作,子组件将在 <code>Vue Devtools
中嵌套在父级组件下面,而不是放在实际内容移动到的地方
位置移动了,提现在结构模板上,但是数据逻辑依旧存在关联的
在某些场景下可能需要视情况禁用 095a8c77e9bc5f13289f43baf6c8bdc1
。举例来说,我们想要在桌面端将一个组件当做浮层来渲染,但在移动端则当作行内组件。我们可以通过对 095a8c77e9bc5f13289f43baf6c8bdc1
动态地传入一个 disabled prop
来处理这两种不同情况
<Teleport :disabled="isMobile"> ... </Teleport>
这里的 isMobile
状态可以根据 CSS media query
的不同结果动态地更新
一个可重用的模态框组件可能同时存在多个实例。对于此类场景,多个 095a8c77e9bc5f13289f43baf6c8bdc1
组件可以将其内容挂载在同一个目标元素上,而顺序就是简单的顺次追加,后挂载的将排在目标元素下更后面的位置上
比如下面这样的用例
<Teleport to=".content"> <div>A</div> </Teleport> <Teleport to=".content"> <div>B</div> </Teleport>
渲染的结果为
<div class="content"> <div>A</div> <div>B</div> </div>
这个teleport
组件在实际开发中还是很实用的,能够解决当组件嵌套层级很深,而后代组件中的模板,想要脱离当前组件结构,解决css
布局层面的干扰,那就可以用这个teleport
组件
The above is the detailed content of An in-depth explanation of how to use the Teleport component in Vue. For more information, please follow other related articles on the PHP Chinese website!