zoukankan      html  css  js  c++  java
  • Vue 中的 ref $refs

    ref 被用来给DOM元素或子组件注册引用信息。引用信息会根据父组件的 $refs 对象进行注册。如果在普通的DOM元素上使用,引用信息就是元素; 如果用在子组件上,引用信息就是组件实例

    注意:只要想要在Vue中直接操作DOM元素,就必须用ref属性进行注册

    这里写图片描述

    实例:

    这里写图片描述

    这里写图片描述

    这里为了在create的时候引用DOM元素,先在DOM中使用ref标签进行了注册,然后便可以通过’this.$refs’再跟注册时的名称来引用DOM元素了

    第二部分

    vue中的 ref 和 $refs

     

    如图,ref 被用来给元素或子组件注册引用信息。引用信息将会注册在父组件的 $refs 对象上。如果在普通的 DOM 元素上使用,引用指向的就是 DOM 元素;如果用在子组件上,引用就指向组件实例:

    在上面的例子中,input的引用信息为input1 ,$refs 是所有注册过的ref的一个集合,

    console.log(this.$refs.input1)//<input type="text" id="input1">
    console.log(document.getElementById('input1'))//<input type="text" id="input1">

     这两种方法获得的都是Dom节点,而$refs相对document.getElementById的方法,会减少获取dom节点的消耗。

    ref和v-for在一起的情况

    li里的ref的无法读取item里面的值,即item.name或被直接读取为字符串“item.name”,

    此时的$refs

     第三部分:

    一、ref使用在外面的组件上

    HTML 部分

    1.  
      <div id="ref-outside-component" v-on:click="consoleRef">
    2.  
      <component-father ref="outsideComponentRef">
    3.  
      </component-father>
    4.  
      <p>ref在外面的组件上</p>
    5.  
      </div>
    6.  
       

    js部分

    1.  
      var refoutsidecomponentTem={
    2.  
      template:"<div class='childComp'><h5>我是子组件</h5></div>"
    3.  
      };
    4.  
      var refoutsidecomponent=new Vue({
    5.  
      el:"#ref-outside-component",
    6.  
      components:{
    7.  
      "component-father":refoutsidecomponentTem
    8.  
      },
    9.  
      methods:{
    10.  
      consoleRef:function () {
    11.  
      console.log(this); // #ref-outside-component vue实例
    12.  
      console.log(this.$refs.outsideComponentRef); // div.childComp vue实例
    13.  
      }
    14.  
      }
    15.  
      });
    16.  
       

    二、ref使用在外面的元素上

    HTML部分

    1.  
      <!--ref在外面的元素上-->
    2.  
      <div id="ref-outside-dom" v-on:click="consoleRef" >
    3.  
      <component-father>
    4.  
      </component-father>
    5.  
      <p ref="outsideDomRef">ref在外面的元素上</p>
    6.  
      </div>

    JS部分

    1.  
      var refoutsidedomTem={
    2.  
      template:"<div class='childComp'><h5>我是子组件</h5></div>"
    3.  
      };
    4.  
      var refoutsidedom=new Vue({
    5.  
      el:"#ref-outside-dom",
    6.  
      components:{
    7.  
      "component-father":refoutsidedomTem
    8.  
      },
    9.  
      methods:{
    10.  
      consoleRef:function () {
    11.  
      console.log(this); // #ref-outside-dom vue实例
    12.  
      console.log(this.$refs.outsideDomRef); // <p> ref在外面的元素上</p>
    13.  
      }
    14.  
      }
    15.  
      });
    16.  
       

    三、ref使用在里面的元素上---局部注册组件

    HTML部分

    1.  
      <!--ref在里面的元素上-->
    2.  
      <div id="ref-inside-dom">
    3.  
      <component-father>
    4.  
      </component-father>
    5.  
      <p>ref在里面的元素上</p>
    6.  
      </div>

    JS部分

    1.  
      var refinsidedomTem={
    2.  
      template:"<div class='childComp' v-on:click='consoleRef'>" +
    3.  
      "<h5 ref='insideDomRef'>我是子组件</h5>" +
    4.  
      "</div>",
    5.  
      methods:{
    6.  
      consoleRef:function () {
    7.  
      console.log(this); // div.childComp vue实例
    8.  
      console.log(this.$refs.insideDomRef); // <h5 >我是子组件</h5>
    9.  
      }
    10.  
      }
    11.  
      };
    12.  
      var refinsidedom=new Vue({
    13.  
      el:"#ref-inside-dom",
    14.  
      components:{
    15.  
      "component-father":refinsidedomTem
    16.  
      }
    17.  
      });
    18.  
       

    四、ref使用在里面的元素上---全局注册组件

    HTML部分

    1.  
      <!--ref在里面的元素上--全局注册-->
    2.  
      <div id="ref-inside-dom-all">
    3.  
      <ref-inside-dom-quanjv></ref-inside-dom-quanjv>
    4.  
      </div>

    JS部分

    1.  
      Vue.component("ref-inside-dom-quanjv",{
    2.  
      template:"<div class='insideFather'> " +
    3.  
      "<input type='text' ref='insideDomRefAll' v-on:input='showinsideDomRef'>" +
    4.  
      " <p>ref在里面的元素上--全局注册 </p> " +
    5.  
      "</div>",
    6.  
      methods:{
    7.  
      showinsideDomRef:function () {
    8.  
      console.log(this); //这里的this其实还是div.insideFather
    9.  
      console.log(this.$refs.insideDomRefAll); // <input type="text">
    10.  
      }
    11.  
      }
    12.  
      });
    13.  
       
    14.  
      var refinsidedomall=new Vue({
    15.  
      el:"#ref-inside-dom-all"
    16.  
      });
  • 相关阅读:
    SpringCloud Ribbon实现负载均衡,SpringCloud Ribbon自定义策略
    springCloud zookeeper整合,Java Zookeeper微服务注册中心整合
    SpringCloud Eureka安装和使用,SpringCloud使用Eureka作为服务注册中心
    Linux yum安装Consul服务中心,Centos7在线安装consul
    SpringCloud consul安装和使用,Windows Consul安装和使用,Java consul服务中心安装和使用
    哔哩哔哩视频下载到电脑,bilibili UWP下载的视频重命名,blibli视频下载到电脑
    elasticsearch kibana安装和配置
    elasticsearch安装和配置,elasticsearch启动报错:can not run elasticsearch as root
    cmd打开当前文件所在目录,cmd进入当前文件目录,cmd进入指定目录
    京东到家 首页 笔记
  • 原文地址:https://www.cnblogs.com/mouseleo/p/11503412.html
Copyright © 2011-2022 走看看