0%

ref属性

//ref属性
//1、被用来给子组件或html标签注册引用信息
//2、用在html标签上是获取真实DOM元素,用在子组件上是组件的实例对象(vc)

<template>
  <div>
    <h2 ref="title">标题</h2>
    <button @click="showDOM" ref="btn">点我输出上方的DOM元素</button>
    <School ref="sch"></School>
  </div>
</template>

<script>
import School from "./components/School.vue";

export default {
  name: "App",
  components: { School },
  methods: {
    showDOM() {
      console.log(this.$refs.title); //真实的DOM元素
      console.log(this.$refs.btn); //真实的DOM元素
      console.log(this.$refs.sch); //获取组件的实例对象VueComponent
    },
  },
};
</script>

<style>
</style>