vue组件 $children,$refs,$parent的使用详解
本文介绍了vue组件 $children,$refs,$parent的使用,分享给大家,也自己留个笔记 如果项目很大,组件很多,怎么样才能准确的、快速的寻找到我们想要的组件了?? 1)$refs首先你的给子组件做标记。demo : 然后在父组件中,通过this.$refs.one就可以访问了这个自组件了,包括访问自组件的data里面的数据,调用它的函数 2)$children他返回的是一个组件集合,如果你能清楚的知道子组件的顺序,你也可以使用下标来操作; 接下来就给一个长一点的deno首先定义一个父组件:parentcomponent, 在父组件中我又是使用了两个自组件(假如有一百个自组件)[明确一点,组件只能有一个根节点],根节点是啥,我不知道。。。。。。 this is a parent-component 分别给出两个字组件的定义:(第2个使用的是template,第1个是script) this is first child
<template id="childSec"> this is second child 组件模板定义好了,就是用: 1)挂在元素:
new Vue({
el:"#app",data:{},components:{
"parent-component":{
template:'#parentcomponent',data(){
return{msg:'这是父组件中的内容'}
},methods:{
show_child_of_parents(){
//children方式访问自组件
for(let i=0;i
}
},'secondchild':{
template:'#childSec',data(){
return {msg:"这是第二个组件"};
}
}
}
}
}
}); 2)使用父组件了 可以通过$refs访问父组件的子组件 |