赞
踩
这里看到一个up主大概说了一下:
13 Teleport
index.vue
普通版本
<template>
<div>
<h1>父组件内容</h1>
<div id="childrenOne">#childrenOne</div>
<div id="childrenTwo">#childrenTow</div>
<A </A>
<B></B>
</div>
</template>
<script setup>
import { useIntersectionObserver } from "@vueuse/core";
// import A from "./a.vue";
import B from "./b.vue";
import C from "./c.vue";
const A = defineAsyncComponent(() => import("./a.vue"));
</script>
<style>
</style>
A.vue
<template>
<div>
<h1>父组件内容</h1>
<div id="childrenOne">#childrenOne</div>
<div id="childrenTwo">#childrenTow</div>
<A </A>
<B></B>
</div>
</template>
<script setup>
// import A from "./a.vue";
import B from "./b.vue";
import C from "./c.vue";
const A = defineAsyncComponent(() => import("./a.vue"));
</script>
<style>
</style>
b.vue
<template>
<div>这是b组件</div>
</template>
<script>
export default {};
</script>
<style scoped>
div {
width: 100vw;
height: 100vh;
}
</style>
index.vue
记得瞄一眼这里:记得看target这个属性,它拿到的是一个dom元素,我推测这个是检测是不是已经到这个组组件的位置了,如果到了就显示,没到就不显示。
<template>
<div>
<h1>父组件内容</h1>
<div id="childrenOne">#childrenOne</div>
<div id="childrenTwo">#childrenTow</div>
<B></B>
<div ref="target">
<A v-if="targetIsVisible"></A>
</div>
</div>
</template>
<script setup>
import { useIntersectionObserver } from "@vueuse/core";
// import A from "./a.vue";
import B from "./b.vue";
const A = defineAsyncComponent(() => import("./a.vue"));
// target是一个dom
const target = ref(null);
const targetIsVisible = ref(false);
const { stop } = useIntersectionObserver(target, ([{ isIntersecting }]) => {
console.log(isIntersecting);
if (isIntersecting) targetIsVisible.value = isIntersecting;
});
</script>
<style>
</style>
a.vue
<template>
<div>
<h1>子组件内容</h1>
<Teleport :to="teleportValue"><div>传送组件内容</div> </Teleport>
<button @click="teleportChange">修改传送组件</button>
</div>
</template>
<script setup>
import { useIntersectionObserver } from "@vueuse/core";
let teleportValue = ref("#childrenTwo");
let teleportChange = () => {
teleportValue.value = "#childrenOne";
console.log(teleportValue.value);
};
</script>
<style>
</style>
ps:为什么使用异步组件,使用异步组件可以实现分包加载,减小单个包的体积。用到的时候再进行加载,你看我这个可能理解不了,你把我的代码跑一边,然后打包一下,你就可以很直观的看到了
index.vue
<Suspense>
<template #default> <B></B></template>
<template #fallback>加载中1</template>
</Suspense>
//一定要是用suspense,官方提供的
b.vue
<template>
<div>这是b组件{{ list }}</div>
</template>
<script setup>
import axios from "axios";
let list = ref([]);
let res = await axios.get("www.baidu.com");
// list.value = res.data.data;
list.value = res;
</script>
<style scoped>
div {
width: 100vw;
height: 100vh;
}
</style>
const AsyncComp = defineAsyncComponent({
// 加载函数
loader: () => import("./Foo.vue"),
// 加载异步组件时使用的组件
loadingComponent: Loading,
// 展示加载组件前的延迟时间,默认为 200ms
delay: 200,
// 加载失败后展示的组件
errorComponent: Error,
// 如果提供了一个 timeout 时间限制,并超时了
// 也会显示这里配置的报错组件,默认值是:Infinity
timeout: 30000,
});
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。