vue的实例属性$refs

$refs 用于获取指定的dom元素。

首先在标签中定义ref="xxx"

通过this.$refs.xxx获取到该dom元素

注意:在created阶段,是不能获取到$ref绑定的元素的。

    <p ref="test">p标签</p>
    <button @click="certain()">确定</button>
  methods: {
    certain() {
        //$refs不能用在created中
        this.$refs.test.style.color = "green"
    }
  }
原文地址:https://www.cnblogs.com/luguankun/p/10856944.html