$refs的使用

<template>
  <div class="app">
    <h2 ref="title" class="title" :style="{ color: titleColor }">{{ message }}</h2>
    <button ref="btn" @click="changeTitle">修改title</button>

    <banner ref="banner"/>
  </div>
</template>

<script>
  import Banner from "./Banner.vue"

  export default {
    components: {
      Banner
    },  
    data() {
      return {
        message: "Hello World",
        titleColor: "red"
      }
    },
    methods: {
      changeTitle() {

        // 获取h2/button元素
        console.log(this.$refs.title)
        console.log(this.$refs.btn)

        // 获取banner组件: 组件实例
        console.log(this.$refs.banner)
      }
    }
  }
</script>

<style scoped>
</style>

 

posted @ 2022-08-23 09:32  杨建鑫  阅读(24)  评论(0编辑  收藏  举报