【发布时间】:2021-10-07 08:42:42
【问题描述】:
我有一个带有 2 个插槽的 vue 组件(两个插槽都已填充),我需要获取相对于页面的位置(例如使用 getBoundingClientRect 方法),但我什至无法访问 watchable 插槽与refs。没有this.$el.querySelector 方法有什么办法吗?
<template>
<div class="watchable-container">
<slot name="watchable" ref="refWatchable"></slot>
<div class="bubble-container">
<div class="bubble">
<slot name="content">
</slot>
</div>
<div class="bubble-tail"></div>
</div>
</div>
</template>
<script>
export default {
name: "BubblePopover",
mounted() {
console.log(this.$slots)
console.log(this.$refs);
},
}
</script>
【问题讨论】:
-
问题是,
slots不渲染,只渲染它们的内容。所以你需要引用像<div class="bubble">这样的包装器。 -
谢谢,是的,我做到了,但我看到人们在互联网上使用了
elm插槽的属性,但我总是在那里不确定。我以为我做错了什么 -
今天也遇到了一些问题,尝试使用嵌套的
slots。所以我感觉到你了。
标签: javascript vue.js vuejs2 vue-component