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

首頁 > 編程 > JavaScript > 正文

vue組件 $children,$refs,$parent的使用詳解

2019-11-19 15:56:46
字體:
來源:轉載
供稿:網友

本文介紹了vue組件 $children,$refs,$parent的使用,分享給大家,也自己留個筆記

如果項目很大,組件很多,怎么樣才能準確的、快速的尋找到我們想要的組件了??

1)$refs

首先你的給子組件做標記。demo :<firstchild ref="one"></firstchild>

然后在父組件中,通過this.$refs.one就可以訪問了這個自組件了,包括訪問自組件的data里面的數據,調用它的函數

2)$children

他返回的是一個組件集合,如果你能清楚的知道子組件的順序,你也可以使用下標來操作;

for(let i=0;i<this.$children.length;i++){    console.log(this.$children[i].msg);輸出子組件的msg數據; }

接下來就給一個長一點的deno

首先定義一個父組件:parentcomponent,

在父組件中我又是使用了兩個自組件(假如有一百個自組件)[明確一點,組件只能有一個根節點],根節點是啥,我不知道。。。。。。

<template id="parentcomponent">  <div >    <p>this is a parent-component</p>    <firstchild ref="f1"></firstchild>    <secondchild ref="f2"></secondchild>    <button @click='show_child_of_parents'>show child msg</button>  </div></template>

 分別給出兩個字組件的定義:(第2個使用的是template,第1個是script) 

<script type="text/x-template" id="childOne">  <div>    <p>this is first child</p>       //使用stop阻止默認事件(vue的事件處理機制)    <button @click.stop='getParent'>get parent msg</button>  </div></script><template id="childSec">  <div>    <p>this is second child</p>  </div></template>

組件模板定義好了,就是用:

1)掛在元素: 

<script>  new Vue({    el:"#app",    data:{},    components:{      "parent-component":{        template:'#parentcomponent',         data(){          return{msg:'這是父組件中的內容'}                  },        methods:{          show_child_of_parents(){            //children方式訪問自組件               for(let i=0;i<this.$children.length;i++){                console.log(this.$children[i].msg);            }               //通過$ref打標記,訪問子組件             console.log(this.$refs.f1.msg);               this.$refs.f1.getParent();          },                          },                       components:{          'firstchild':{            template:'#childOne',            data(){              return {msg:'這是第一個子組件'};            },            methods:{              getParent(){                let a=1;                console.log(a);                alert(this.$parent.msg);                              }            },          },                    'secondchild':{            template:'#childSec',            data(){              return {msg:"這是第二個組件"};            }          }                  }                      }    }      });</script>

 2)使用父組件了

  <body>    <p><strong>可以通過$refs訪問父組件的子組件</strong></p>    <div id="app">      <parent-component></parent-component>    </div>  </body>

值得注意的是vue2,相比vue1,丟棄了一些東西。。。。、//www.survivalescaperooms.com/article/93467.htm

總結一下:

1)組件只能一個根節點

2)可以在自組件中使用this.$parent.屬性值,或者函數

3)在父組件中可以使用this.$refs.組件的標記 訪問子組件,或者this.$children[i].屬性,,訪問子組件的

 4)你需要注意this的指向

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 滦南县| 平谷区| 中阳县| 理塘县| 盐源县| 登封市| 开化县| 天全县| 成安县| 建湖县| 西乡县| 乌拉特中旗| 四川省| 淳化县| 韩城市| 醴陵市| 蒙城县| 盘锦市| 灵丘县| 刚察县| 上蔡县| 耒阳市| 大方县| 城口县| 北票市| 商水县| 清水县| 左权县| 朔州市| 庄河市| 民权县| 长宁县| 甘谷县| 抚州市| 安阳市| 左云县| 浦东新区| 弥渡县| 嘉善县| 崇阳县| 田阳县|