本文介绍了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,丢弃了一些东西。。。。、https://www.jb51.net/article/93467.htm
总结一下:
1)组件只能一个根节点
2)可以在自组件中使用this.$parent.属性值,或者函数
3)在父组件中可以使用this.$refs.组件的标记 访问子组件,或者this.$children[i].属性,,访问子组件的
4)你需要注意this的指向
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新动态
- 丁当《日与夜,跟自己说晚安》[320K/MP3][98.51MB]
- 何炅.2005-漫游【中唱】【WAV+CUE】
- 何嘉莉.1998-MYDREAMS(EP)【飞图】【WAV+CUE】
- 群星.2019-天坑鷹猎电视剧原声带(EP)【智慧大狗】【FLAC分轨】
- 【雨果唱片】《春江花月夜》2020【WAV+CUE】
- 【正原唱片】女子十二乐坊《发烧国乐精粹-十二乐坊》WAV+CUE
- [黄翠珊]SusanWong-TheBestofSusanWong[WAV+CUE]
- 杜德伟.1995-BEST.LOVE(英)【滚石】【WAV+CUE】
- 杜德伟.1996-BEST.LOVE.2(英)【滚石】【WAV+CUE】
- 陈柏宇.2008-Change【SONY】【FLAC分轨】
- 丁当《日与夜,跟自己说晚安》[FLAC/分轨][285.42MB]
- 早安《Bubble Vision》[320KMP3][36.83MB]
- 叶振棠《叶振棠歌曲合集》[合集][MP3][145.9MB]
- [黄翠珊]SusanWong-ClosetoMe[SACD]dsf
- 中唱唱片群星《好歌珍藏-民歌精粹》2CD[WAV.]