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

首頁 > 語言 > JavaScript > 正文

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

2024-05-06 15:23:30
字體:
來源:轉載
供稿:網友

本文介紹了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"  });            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 灵山县| 陕西省| 新沂市| 图木舒克市| 桂东县| 思茅市| 沾益县| 淳安县| 新疆| 铜鼓县| 渭南市| 南涧| 依兰县| 三门峡市| 丘北县| 辽中县| 华宁县| 东兰县| 郁南县| 嘉禾县| 阳曲县| 于田县| 鱼台县| 灵武市| 白山市| 阳山县| 赤水市| 循化| 禄丰县| 大竹县| 南开区| 临海市| 平度市| 青铜峡市| 乡城县| 泰州市| 宁波市| 乌审旗| 阿克苏市| 金寨县| 双鸭山市|