this.$nextTick 与window.setTimeout

本文介绍Vue中$nextTick方法的应用场景,通过实例演示如何利用$nextTick确保DOM更新后执行特定操作。示例中,$nextTick被用来初始化名为orgAddOrUpdate的组件,确保在其DOM更新后正确调用了组件内的init方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

两个都可以设置运行先后。前者,方式:

this.$nextTick(() => {
this.$refs.orgAddOrUpdate.init(row, isAdd)
})

其中orgAddOrUpdate是一个组件的ref,组件里的metheds里边有init方法。

转载于:https://www.cnblogs.com/sweeeper/p/11206172.html

<template> <div class="ellipsis-container" ref="container" :class="{ 'has-tooltip': showTooltip }" @mouseenter="handleMouseEnter" @mouseleave="handleMouseLeave" > <div ref="content" class="ellipsis-content" :class="[singleLine ? 'ellipsis-single' : 'ellipsis-multi']" :style="{ '--lines': lines }" > <slot>{{ content }}</slot> </div> <div v-if="showTooltip" class="custom-tooltip" :style="tooltipStyle" > {{ content }} </div> </div> </template> <script> export default { name: 'TextOverflow', props: { // 文本内容 content: { type: String, default: '' }, // 是否单行显示(默认多行) singleLine: { type: Boolean, default: false }, // 多行显示时的行数 lines: { type: Number, default: 3 }, // tooltip最大宽度 tooltipMaxWidth: { type: String, default: '400px' } }, data() { return { showTooltip: false, tooltipStyle: { maxWidth: this.tooltipMaxWidth } }; }, mounted() { this.checkOverflow(); window.addEventListener('resize', this.checkOverflow); }, beforeDestroy() { window.removeEventListener('resize', this.checkOverflow); }, methods: { // 检测文本是否溢出 checkOverflow() { if (!this.$refs.content) return; if (this.singleLine) { this.showTooltip = this.$refs.content.scrollWidth > this.$refs.content.clientWidth; } else { const lineHeight = parseInt(getComputedStyle(this.$refs.content).lineHeight); const maxHeight = lineHeight * this.lines; this.showTooltip = this.$refs.content.scrollHeight > maxHeight; } }, // 鼠标移入处理 handleMouseEnter() { if (this.showTooltip) { // 动态计算tooltip位置(防止超出视口) const rect = this.$refs.container.getBoundingClientRect(); this.tooltipStyle = { ...this.tooltipStyle, left: '0', top: 'calc(100% + 8px)', maxWidth: this.tooltipMaxWidth }; } }, handleMouseLeave() { // 不需要额外处理 } }, watch: { content() { this.$nextTick(this.checkOverflow); }, lines() { this.$nextTick(this.checkOverflow); } } }; </script> <style scoped> .ellipsis-container { position: relative; cursor: default; } .ellipsis-single { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } .ellipsis-multi { display: -webkit-box; -webkit-box-orient: vertical; overflow: hidden; text-overflow: ellipsis; -webkit-line-clamp: var(--lines, 3); } .custom-tooltip { position: absolute; background: #333; color: #fff; padding: 8px 12px; border-radius: 4px; font-size: 14px; z-index: 1000; box-shadow: 0 4px 12px rgba(0,0,0,0.15); white-space: normal; word-break: break-word; opacity: 0; transform: translateY(10px); transition: opacity 0.3s, transform 0.3s; pointer-events: none; } .has-tooltip:hover .custom-tooltip { opacity: 1; transform: translateY(0); } </style> 怎么控制文字的宽度?
最新发布
08-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值