Vue组件设置child的数据值 [英] Vue component set data value of child

查看:66
本文介绍了Vue组件设置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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆