$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>
本文来自博客园,作者:杨建鑫,转载请注明原文链接:https://www.cnblogs.com/qd-lbxx/p/16615047.html