国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

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

2019-11-19 14:42:12
字體:
來源:轉載
供稿:網友

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

看Vue.js文檔中的ref部分,自己總結了下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實例        console.log(this.$refs.outsideComponentRef); // div.childComp vue實例      }    }  });

二、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實例        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實例         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其實還是div.insideFather        console.log(this.$refs.insideDomRefAll); // <input type="text">      }    }  });  var refinsidedomall=new Vue({    el:"#ref-inside-dom-all"  });

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 万全县| 平顶山市| 上饶县| 苍梧县| 开江县| 红桥区| 始兴县| 庆安县| 乌兰察布市| 分宜县| 襄垣县| 久治县| 扶风县| 工布江达县| 绥化市| 麟游县| 健康| 东平县| 齐齐哈尔市| 读书| 平泉县| 萝北县| 石阡县| 乐平市| 松溪县| 亚东县| 迁西县| 石台县| 福清市| 历史| 新沂市| 富蕴县| 青海省| 柞水县| 台北市| 海丰县| 林芝县| 西畴县| 山丹县| 布尔津县| 博湖县|