国产探花免费观看_亚洲丰满少妇自慰呻吟_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,丟棄了一些東西。。。。、//m.survivalescaperooms.com/article/93467.htm

總結一下:

1)組件只能一個根節點

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

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

 4)你需要注意this的指向

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 灵川县| 根河市| 商都县| 武宁县| 米易县| 潼南县| 建昌县| 华池县| 忻州市| 五寨县| 星座| 耿马| 靖州| 博客| 松潘县| 通城县| 富裕县| 育儿| 桐柏县| 黎城县| 郴州市| 手机| 张家川| 河西区| 延川县| 民丰县| 芮城县| 乌苏市| 临猗县| 天镇县| 三河市| 湾仔区| 城固县| 修水县| 镇原县| 沽源县| 沽源县| 三江| 遂平县| 安阳县| 惠安县|