ref的使用只有在特殊的情况下使用

1.如果给标签添加ref,获取的就是真实的DOM节点
2. 如果给子组件添加ref,获取的就是当前的子组件对象

例子:

<div >
    <App></App>
</div>



<script>
    const Test={
        data(){
            return{

            }
        },
        components:{

        },
        template: `
        <div>

        </div>


        `
    };
    const App={
        data(){
            return{

            }
        },
        components:{

        },
        mounted(){
            // 1.如果给标签添加ref,获取的就是真实的DOM节点
            // 2. 如果给子组件添加ref,获取的就是当前的子组件对象
            // ---------
            // 有可能有很多ref所有写成refs
          console.log(this.$refs.btn);
          // input 框自动获取焦点
            this.$refs.input.focus();
        },
        template: `
        <div>
            <Test></Test>
            <button ref="btn">改变生死</button>
            <input type="text" ref="input">
        </div>


        `
    };
    let app = new Vue({
        el:'#app',
        data:{

        },
        components:{
            App
        }
    })
</script>

 

相关文章:

  • 2022-12-23
  • 2021-10-24
  • 2021-07-30
  • 2022-12-23
  • 2021-11-08
  • 2022-12-23
  • 2022-12-23
  • 2021-12-24
猜你喜欢
  • 2021-06-17
  • 2021-06-28
  • 2021-08-11
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案