Vue组件设置child的数据值 [英] Vue component set data value of child
问题描述
我正在使用 vue 轮播
我可以看到当前页面有一个值,我认为将它设置为 0 可以作为重置.然而,这些似乎不是允许我这样做的公开方法.我可以从父母那里设置吗?
如果要在子组件上设置数据属性,可以设置一个 ref
属性,然后通过 this.$refs
访问子实例.
在你的情况下,你可以像这样向轮播标签添加一个引用:
然后在父组件的脚本中,您可以像这样设置子组件的 currentPage
属性:
this.$refs.carousel.currentPage = 0;
I am using vue carousel
https://ssense.github.io/vue-carousel/api/
It woks well but I need to resetthe carousel
I can see there is a value of current page and I think setting this to 0 would work as a reset. However these seems to be no exposed method to allow me to do this. Can I set it from the parent?
If you want to set a data property on a child component, you can set a ref
attribute on that child's tag and then access the child instance via this.$refs
.
In your case, you could add a ref to the carousel tag like so:
<Carousel ref="carousel"/>
And then in you parent component's script, you can set the currentPage
property of the child component like so:
this.$refs.carousel.currentPage = 0;
这篇关于Vue组件设置child的数据值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!