HMVue4.4【ref引用】

 

1 引言

2 什么是ref

3 使用ref引用DOM元素

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

3 使用ref引用组件实例

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

4 ref案例:控制文本框和按钮的按需切换

 

 

 

 

 

 

5 ref案例:让文本框自动获得焦点、this.$nextTick(cb)

 

 

cb是指callback,回调

 

 

 

 

报错原因:this.$refs.iptRef.focus()中iptRef是undefined
原因是this.inputVisible = true(数据改变)后页面还没来得及渲染完成,所以获取不到页面dom元素

 

 

 

为啥不能放在updated()中实现该功能?

 

 

 

 

 

 

 

 原因是下面这个圈被循环了两次,

点击按钮令flag由false变为true,展示文本框&隐藏按钮,同时数据变化触发了这个圈

待到页面渲染完毕后,执行updated方法令文本框获得焦点ok

但鼠标从文本框中移开后,触发了showButton(),令flag由true变为flase

此时,文本框被隐藏&按钮显示,同时数据变化又一次触发了这个圈,

但次flag=false,不会渲染文本框到页面上

等到执行到updated方法时发现获取不到文本框而报错

黑马程序员Vue全套视频教程,从vue2.0到vue3.0一套全覆盖,前端必会的框架教程_哔哩哔哩_bilibili

6 源码

<template>
  <div class="app-container">
    <h1 ref="myh1">App 根组件</h1>
    <button @click="showThis">打印this</button>
    <button @click="onReset">重置Left组件的count值为0</button>
    <hr>
    <!-- 需求:输入框与按钮不同时显示,只显示其一 
              输入框失去焦点时自动隐藏输入框,并展示按钮
              点击按钮,展示输入框,并隐藏按钮
              文本框展示出来的那一刻瞬间获得焦点功能
    -->
    <input type="text" v-if="inputVisible" @blur="showButton" ref="iptRef">
    <button v-else @click="showInput">展示输入框</button>
    <hr>
    <div class="box">
        <Left ref="comLeft"></Left>
    </div>
  </div>
</template>

<script>
import Left from '@/components/Left.vue'
export default {
    data(){
        return{
            //控制输入框和按钮的按需切换;默认值为false,即隐藏输入框&展示按钮
            inputVisible: false,
        }
    },
    /*
    updated(){
        this.$refs.iptRef.focus() //错误
    },
    */
    methods: {
        showInput(){
            //1、切换布尔值,将文本框展示出来
            this.inputVisible = true //点击按钮,展示输入框

            //2、将展示出来的文本框自动获得焦点
            /*
            // this.$refs.iptRef.focus()
            console.log(this.$refs.iptRef) //undefined,原因数据改变后,页面还没来得及渲染完成,所以获取不到页面dom元素
            //要想让this.$refs.iptRef.focus()不报错,前提是需要等到页面渲染完毕之后再执行这句
            */
           this.$nextTick(()=>{
               this.$refs.iptRef.focus()
           })
        },
        showButton(){
            this.inputVisible = false //输入框失去焦点,展示按钮
        },
        showThis(){
            //this是当前App组件的实例对象
            console.log(this)
            console.log(this.$refs.myh1)
            this.$refs.myh1.style.color = 'red'
        },
        onReset(){
            //点击按钮,重置组件Left中count的值
            // this.$refs.comLeft.resetCount() //方法1(父组件调用子组件中的方法)
            this.$refs.comLeft.count = 0 //方法2(父组件修改子组件中的数据)
        },
    },
    components: {
        Left
    }
}
</script>

<style lang="less">
.app-container {
  padding: 1px 20px 20px;
  background-color: #efefef;
}
.box {
  display: flex;
}
</style>
View Code

 

<template>
<div class="left-container">
    <h3>Left 组件</h3>
    <p>count={{count}}</p>
    <button @click="count+=1">+1</button>
    <button @click="resetCount">重置归零</button>
</div>
</template>

<script>
export default {
    data(){
        return{
            count: 0
        }
    },
    methods: {
        resetCount(){
            this.count = 0
        }
    }
}
</script>

<style lang="less">
.left-container {
  padding: 0 20px 20px;
  background-color: orange;
  min-height: 250px;
  flex: 1;
}
</style>
View Code

 

posted @ 2021-11-22 18:07  yub4by  阅读(78)  评论(0)    收藏  举报