vue3teleport传送

 Teleport 是一种能够将我们的模板移动到 DOM 中 Vue app 之外的内置组件

teleport 里的to 指向渲染组件的dom

在哪里显示就通过注册id使用,to和id必须一样

使用场景

例:比如可以注册一个全局的提示信息,这里我封装了一个子组件,子组件里写teleport内置组件,在app.vue里注册id,teleport页面调用子组件。

子组件

<template>
	<div class='portals'>
		<button @click='submit'> 提交 </button>
		<teleport to='#portal-target'>
			<div v-if="flag" class='notification'>
				提交成功
			</div>
		</teleport>
	</div>
</template>

<script setup>
	import {ref} from 'vue'
	const flag = ref(false)
	const submit = () => {
		flag.value = true
		setTimeout(() => {
			flag.value = false
		}, 2000)
	}
</script>

<style scoped>
	.notification {
		font-family: myriad-pro, sans-serif;
		position: fixed;
		bottom: 20px;
		left: 20px;
		width: 300px;
		padding: 30px;
		background-color: #fff;
	}
</style>

 teleport页面

<template>
	<div>
	<Page/>
	</div>
</template>

<script setup>
	import Page from '../components/Page.vue'
</script>

<style scoped>

</style>

app.vue页面 

<template>
  <nav>
    <router-link to="/">Home</router-link> |
    <router-link to="/about">About</router-link> |
	<router-link to="/teleport">teleport</router-link> 
	<div id="portal-target"></div>
  </nav>

  <router-view/>
</template>

<style lang="scss">
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}

nav {
  padding: 30px;

  a {
    font-weight: bold;
    color: #2c3e50;

    &.router-link-exact-active {
      color: #42b983;
    }
  }
}
</style>

效果图 

 

 

 

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值