javascript - 引用 vue 中的命名槽
问题描述
我有一个带有 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>
解决方案
该this.$slots
成员应该工作。
this.$slots.watchable
// or
this.$slots.watchable.$el
尝试nextTick
在您的挂载函数中使用,以给插槽时间进行渲染。
mounted() {
nextTick(()=>{
// your code here
})
}
您还可以从您的插槽内容中添加一个 ID 添加访问权限getElementById
另一种选择是使用组合 API 插件,然后从 ref 访问内容。
推荐阅读
- ios - 是否可以仅更改一个视图控制器导航栏的颜色?
- java - 删除wifi配置并重启android设备后连接到wifi
- c - 如何在保存之前阻止我的程序跳过字符
- c# - SharpAdbClient.DeviceCommands.PackageManager.Install
- javascript - 在 React 中,如何在 .map 函数中正确设置状态以限制并行 HTTP 请求
- python - 我的推特搜索只返回 1 个结果
- java - 为什么返回 org.h2.jdbc.JdbcSQLException?
- php - VScode 格式化程序,将左括号保持在同一行(PHP)
- postman - 如何在邮递员中读取动态请求参数值?
- c# - 将对象序列化为具有多个命名空间的字符串 XML