使用 this.$refs
来获取元素和组件
通过实例的vm.
r
e
f
s
(
t
h
i
s
.
refs(this.
refs(this.refs)取得通过ref注册过的组件
ref是被用来给元素或子组件注册引用信息。引用信息将注册在父组件的 $refs 对象身上。如果在普通的DOM元素身上使用,引用指向就是DOM元素;如果用在子组件身上,引用就是指向组件实例。
因为ref本身就是作为渲染结果被创建的,在初始渲染的时候不能被范文-他们还不存在! $ref也不是响应式的,因此你不应该试图它用在模板中做数据绑定。
<template>
<div id="components-affix-demo-target" ref="container" class="scrollable-container">
<div class="background">
<a-affix :target="() => this.$refs.container">
<a-button type="primary">
Fixed at the top of container
</a-button>
</a-affix>
</div>
</div>
</template>
<style>
#components-affix-demo-target.scrollable-container {
height: 100px;
overflow-y: scroll;
}
#components-affix-demo-target .background {
padding-top: 60px;
height: 300px;
background-image: url('https://zos.alipayobjects.com/rmsportal/RmjwQiJorKyobvI.jpg');
}
</style>