前面的话
有时候需要父组件访问子组件,子组件访问父组件,或者是子组件访问根组件。 在组件实例中,Vue提供了相应的属性,包括$parent、$children、$refs和$root,这些属性都挂载在组件的this上。本文将详细介绍Vue组件实例间的直接访问
$parent
$parent表示父组件的实例,该属性只读
下面是一个简易实例
<div id="example">
<parent-component></parent-component>
</div>
<template id="parent-component">
<div class="parent">
<h3>我是父组件</h3>
<input v-model="parentMsg">
<p>{{parentMsg}}</p>
<child-component></child-component>
</div>
</template>
<template id="child-component">
<div class="child">
<h3>我是子组件</h3>
<p>{{msg}}</p>
<button v-on:click="showData">显示父组件数据</button>
</div>
</template>
<script>
// 注册
Vue.component('parent-component', {
template: '#parent-component',
data(){
return{
parentMsg:'我是父组件的数据'
}
},
components:{
'child-component':{
template:'#child-component',
data(){
return{
msg:''
}
},
methods:{
showData(){
this.msg = this.$parent.parentMsg;
}
}
}
}
})
// 创建根实例
new Vue({
el: '#example'
})
</script>
$root
$root表示当前组件树的根 Vue 实例。如果当前实例没有父实例,此实例将会是其自己。该属性只读
<div id="example">
<h3>我是根组件</h3>
<input v-model="rootMsg">
<p>{{rootMsg}}</p>
<parent-component></parent-component>
</div>
<template id="parent-component">
<div class="parent">
<h3>我是父组件</h3>
<input v-model="parentMsg">
<p>{{parentMsg}}</p>
<child-component></child-component>
</div>
</template>
<template id="child-component">
<div class="child">
<h3>我是子组件</h3>
<p>
<button v-on:click="showRootData">显示根组件数据</button><span>{{rootMsg}}</span>
</p>
<p>
<button v-on:click="showParentData">显示父组件数据</button><span>{{parentMsg}}</span>
</p>
</div>
</template>
<script>
// 注册
Vue.component('parent-component', {
template: '#parent-component',
data(){
return{
parentMsg:'我是父组件的数据'
}
},
components:{
'child-component':{
template:'#child-component',
data(){
return{
parentMsg:'',
rootMsg:''
}
},
methods:{
showParentData(){
this.parentMsg = this.$parent.parentMsg;
},
showRootData(){
this.rootMsg = this.$root.rootMsg;
},
}
}
}
})
// 创建根实例
new Vue({
el: '#example',
data:{
rootMsg:'我是根组件数据'
}
})
</script>
$children
$children表示当前实例的直接子组件。需要注意$children并不保证顺序,也不是响应式的。如果正在尝试使用$children来进行数据绑定,考虑使用一个数组配合v-for来生成子组件,并且使用Array作为真正的来源
<div id="example">
<parent-component></parent-component>
</div>
<template id="parent-component">
<div class="parent">
<h3>我是父组件</h3>
<button @click="getData">获取子组件数据</button>
<br>
<div v-html="msg"></div>
<child-component1></child-component1>
<child-component2></child-component2>
</div>
</template>
<template id="child-component1">
<div class="child">
<h3>我是子组件1</h3>
<input v-model="msg">
<p>{{msg}}</p>
</div>
</template>
<template id="child-component2">
<div class="child">
<h3>我是子组件2</h3>
<input v-model="msg">
<p>{{msg}}</p>
</div>
</template>
<script>
// 注册
Vue.component('parent-component', {
template: '#parent-component',
data(){
return{
msg:'',
}
},
methods:{
getData(){
let html = '';
let children = this.$children;
for(var i = 0; i < children.length;i++){
html+= '<div>' + children[i].msg + '</div>';
}
this.msg = html;
}
},
components:{
'child-component1':{
template:'#child-component1',
data(){
return{
msg:'',
}
},
},
'child-component2':{
template:'#child-component2',
data(){
return{
msg:'',
}
},
},
}
})
// 创建根实例
new Vue({
el: '#example',
})
</script>
$refs
组件个数较多时,难以记住各个组件的顺序和位置,通过序号访问子组件不是很方便
在子组件上使用ref属性,可以给子组件指定一个索引ID:
<child-component1 ref="c1"></child-component1> <child-component2 ref="c2"></child-component2>
在父组件中,则通过$refs.索引ID访问子组件的实例
this.$refs.c1
this.$refs.c2
<div id="example">
<parent-component></parent-component>
</div>
<template id="parent-component">
<div class="parent">
<h3>我是父组件</h3>
<div>
<button @click="getData1">获取子组件c1的数据</button>
<p>{{msg1}}</p>
</div>
<div>
<button @click="getData2">获取子组件c2的数据</button>
<p>{{msg2}}</p>
</div>
<child-component1 ref="c1"></child-component1>
<child-component2 ref="c2"></child-component2>
</div>
</template>
<template id="child-component1">
<div class="child">
<h3>我是子组件1</h3>
<input v-model="msg">
<p>{{msg}}</p>
</div>
</template>
<template id="child-component2">
<div class="child">
<h3>我是子组件2</h3>
<input v-model="msg">
<p>{{msg}}</p>
</div>
</template>
<script>
// 注册
Vue.component('parent-component', {
template: '#parent-component',
data(){
return{
msg1:'',
msg2:'',
}
},
methods:{
getData1(){
this.msg1 = this.$refs.c1.msg;
},
getData2(){
this.msg2 = this.$refs.c2.msg;
},
},
components:{
'child-component1':{
template:'#child-component1',
data(){
return{
msg:'',
}
},
},
'child-component2':{
template:'#child-component2',
data(){
return{
msg:'',
}
},
},
}
})
// 创建根实例
new Vue({
el: '#example',
})
</script>
总结
虽然vue提供了以上方式对组件实例进行直接访问,但并不推荐这么做。这会导致组件间紧密耦合,且自身状态难以理解,所以尽量使用props、自定义事件以及内容分发slot来传递数据。
Vue组件实例,vue,组件访问
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]