欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

淺談Vue.js中ref ($refs)用法舉例總結(jié)

 更新時間:2017年12月19日 09:25:07   作者:該帳號已被查封  
本篇文章主要介紹了淺談Vue.js中ref ($refs)用法舉例總結(jié),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

本文介紹了Vue.js中ref ($refs)用法舉例總結(jié),分享給大家,具體如下:

看Vue.js文檔中的ref部分,自己總結(jié)了下ref的使用方法以便后面查閱。

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

HTML 部分

<div id="ref-outside-component" v-on:click="consoleRef">
  <component-father ref="outsideComponentRef">
  </component-father>
  <p>ref在外面的組件上</p>
</div>

js部分

  var refoutsidecomponentTem={
    template:"<div class='childComp'><h5>我是子組件</h5></div>"
  };
  var refoutsidecomponent=new Vue({
    el:"#ref-outside-component",
    components:{
      "component-father":refoutsidecomponentTem
    },
    methods:{
      consoleRef:function () {
        console.log(this); // #ref-outside-component   vue實(shí)例
        console.log(this.$refs.outsideComponentRef); // div.childComp vue實(shí)例
      }
    }
  });

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

HTML部分

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

JS部分

  var refoutsidedomTem={
    template:"<div class='childComp'><h5>我是子組件</h5></div>"
  };
  var refoutsidedom=new Vue({
    el:"#ref-outside-dom",
    components:{
      "component-father":refoutsidedomTem
    },
    methods:{
      consoleRef:function () {
        console.log(this); // #ref-outside-dom  vue實(shí)例
        console.log(this.$refs.outsideDomRef); //  <p> ref在外面的元素上</p>
      }
    }
  });

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

HTML部分

<!--ref在里面的元素上-->
<div id="ref-inside-dom">
  <component-father>
  </component-father>
  <p>ref在里面的元素上</p>
</div>

JS部分

  var refinsidedomTem={
    template:"<div class='childComp' v-on:click='consoleRef'>" +
            "<h5 ref='insideDomRef'>我是子組件</h5>" +
         "</div>",
    methods:{
      consoleRef:function () {
        console.log(this); // div.childComp  vue實(shí)例 
        console.log(this.$refs.insideDomRef); // <h5 >我是子組件</h5>
      }
    }
  };
  var refinsidedom=new Vue({
    el:"#ref-inside-dom",
    components:{
      "component-father":refinsidedomTem
    }
  });

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

HTML部分

<!--ref在里面的元素上--全局注冊-->
<div id="ref-inside-dom-all">
  <ref-inside-dom-quanjv></ref-inside-dom-quanjv>
</div>

JS部分

  Vue.component("ref-inside-dom-quanjv",{
    template:"<div class='insideFather'> " +
          "<input type='text' ref='insideDomRefAll' v-on:input='showinsideDomRef'>" +
          " <p>ref在里面的元素上--全局注冊 </p> " +
         "</div>",
    methods:{
      showinsideDomRef:function () {
        console.log(this); //這里的this其實(shí)還是div.insideFather
        console.log(this.$refs.insideDomRefAll); // <input type="text">
      }
    }
  });

  var refinsidedomall=new Vue({
    el:"#ref-inside-dom-all"
  });

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Vue 自定義指令實(shí)現(xiàn)一鍵 Copy功能

    Vue 自定義指令實(shí)現(xiàn)一鍵 Copy功能

    指令 (Directives) 是帶有 v- 前綴的特殊特性。這篇文章主要介紹了Vue 自定義指令實(shí)現(xiàn)一鍵 Copy的功能,需要的朋友可以參考下
    2019-09-09
  • 關(guān)于Vue腳手架中render 理解

    關(guān)于Vue腳手架中render 理解

    這篇文章主要介紹了Vue腳手架中的 render 理解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-10-10
  • vue自定義封裝按鈕組件

    vue自定義封裝按鈕組件

    這篇文章主要為大家詳細(xì)介紹了vue自定義封裝按鈕組件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • 淺談vite和webpack的性能優(yōu)化和區(qū)別

    淺談vite和webpack的性能優(yōu)化和區(qū)別

    本文主要介紹了淺談vite和webpack的區(qū)別,從性能優(yōu)化的幾個方便講解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • vue使用pdf-dist實(shí)現(xiàn)pdf預(yù)覽以及水印添加

    vue使用pdf-dist實(shí)現(xiàn)pdf預(yù)覽以及水印添加

    這篇文章主要為大家詳細(xì)介紹了vue如何使用pdf-dist實(shí)現(xiàn)pdf預(yù)覽以及水印添加的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-10-10
  • 在vue-cli腳手架中配置一個vue-router前端路由

    在vue-cli腳手架中配置一個vue-router前端路由

    這篇文章主要給大家介紹了在vue-cli腳手架中配置一個vue-router前端路由的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面跟著小編一起來學(xué)習(xí)學(xué)習(xí)吧。
    2017-07-07
  • Vue項(xiàng)目打包部署到GitHub Pages的實(shí)現(xiàn)步驟

    Vue項(xiàng)目打包部署到GitHub Pages的實(shí)現(xiàn)步驟

    本文主要介紹了Vue項(xiàng)目打包部署到GitHub Pages的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • vue.js this.$router.push獲取不到params參數(shù)問題

    vue.js this.$router.push獲取不到params參數(shù)問題

    這篇文章主要介紹了vue.js this.$router.push獲取不到params參數(shù)問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • Vue中使用防抖與節(jié)流的方法

    Vue中使用防抖與節(jié)流的方法

    這篇文章主要為大家介紹了Vue中使用防抖與節(jié)流的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-01-01
  • Vue filter介紹及其使用詳解

    Vue filter介紹及其使用詳解

    本篇文章主要介紹了Vue filter介紹及其使用詳解,VueJs 提供了強(qiáng)大的過濾器API,能夠?qū)?shù)據(jù)進(jìn)行各種過濾處理。一起跟隨小編過來看看吧
    2017-10-10

最新評論