Javascript Vue.js组件数据依赖于其他组件数据
我有一个组件如下Javascript Vue.js组件数据依赖于其他组件数据,javascript,vue.js,vue-component,Javascript,Vue.js,Vue Component,我有一个组件如下 export default { name: "ImagesGallery", data () { return { activeImageIndex: 0, }; }, } 此组件是一个图像库。 现在我有了另一个组件,我需要在其中引用activeImageIndex <template> <div> <images-gallery ref="
export default {
name: "ImagesGallery",
data () {
return {
activeImageIndex: 0,
};
},
}
此组件是一个图像库。
现在我有了另一个组件,我需要在其中引用activeImageIndex
<template>
<div>
<images-gallery ref="gallery">
</div>
</template>
export default {
name: "AnotherComponent",
components: {
ImagesGallery,
},
data () {
return {
imageIndex: null, // I want this to refer to the images gallery index.
};
},
}
导出默认值{
名称:“另一个组件”,
组成部分:{
ImagesGallery,
},
数据(){
返回{
imageIndex:null,//我希望它引用图像库索引。
};
},
}
我想要达到的目标:
我希望另一个组件的“imageIndex”是对ImageGallery组件的“activeImageIndex”的引用。如果修改“imageIndex”,库索引的值也应该更改
我试图在数据函数中分配imageIndex:this.$refs.gallery.activeImageIndex,但显然它在这种情况下不起作用。您需要将索引作为道具传递给子组件: **母公司
<images-gallery :activeImageIndex="imageIndex" ref="gallery">
在子组件中,您可以使用与以前相同的方式访问
activeImageIndex
。您需要将索引作为道具传递给子组件:
**母公司
<images-gallery :activeImageIndex="imageIndex" ref="gallery">
在子组件中,您可以使用与以前相同的方式访问
activeImageIndex
。您应该了解VueJs中的反应性、道具和v-model指令。
您应该使用v-model将imageIndex作为道具传递给子组件。
例如:
export default {
name: "ImagesGallery",
props: {
value: Number // this prop represents **imageIndex** from a parent component.
}
}
和父母:
<template>
<div>
<images-gallery ref="gallery" v-model="imageIndex">
</div>
</template>
...
要更改父组件中的imageIndex,只需指定它:
this.imageIndex = newIndex
您应该了解VueJs中的反应性、道具和v-model指令。 您应该使用v-model将imageIndex作为道具传递给子组件。 例如:
export default {
name: "ImagesGallery",
props: {
value: Number // this prop represents **imageIndex** from a parent component.
}
}
和父母:
<template>
<div>
<images-gallery ref="gallery" v-model="imageIndex">
</div>
</template>
...
要更改父组件中的imageIndex,只需指定它:
this.imageIndex = newIndex