【Vue2-02】ref属性
ref属性
- 被用来给元素或子组件
注册引用信息
(id的替代者) - 应用在html标签上获取的是
真实DOM元素
,应用在组件标签上是组件实例对象(vc)
- 使用方式:
- 打标识:
<h1 ref="xxx">...</h1>
或<School ref="xxx"></School>
- 获取:
this.$refs.xxx
- 打标识:
<template>
<div>
<h1 ref="title">获取标题dom元素</h1>
<School ref="sch"></School>
<button @click="show">点我输出上方DOM元素</button>
</div>
</template>
<script>
// 引入组件
import School from './components/School.vue'
export default {
name: 'App',
// 注册组件
components: {
School,
},
methods: {
show() {
console.log(this.$refs.title);
console.log(this.$refs.sch);
}
},
}
</script>
<style scoped>
</style>
本文作者:Ac_c0mpany丶
本文链接:https://www.cnblogs.com/keyongkang/p/16886669.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步