Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
761 views
in Technique[技术] by (71.8m points)

vue3没有$children了,那怎么获取子组件的实例。或者有什么代替方法

vue3没有$children了,那怎么获取子组件的实例。或者有什么代替方法

场景:在Swiper父组件里对SwiperItem前后各添加一张图片,还有动态修改SwiperItem的样式

因为swiper渲染swiper-item用的是slot,所以没办法用ref


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

目前来看,vue3把this.$children给干掉了,但有个替代办法是子组件mounted()的时候,通过this.$parent调用父组件方法,把this传给父组件,这样父组件就能操作子组件了。

我今天试图把一个组件库迁到vue3,第一个组件就被这个问题给卡住了,翻了些vue3的组件库代码,没其它太好的办法。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...