如何检查$插槽内的值类型?
我将提供一个简单的示例,可以证明我面临的问题。
因此,我有一个包含以下代码的页面:
<Slider>
<Slide>
<p>test 1</p>
</Slide>
<Slide> test 2 </Slide>
<div>test3</div>
</Slider>
问题在滑块的安装部分内。
内部滑块
<template>
<div class="h-48 w-full">
<slot></slot>
</div>
</template>
<script>
export default {
data: () => ({
slides: [],
}),
watch: {
slides() {
console.log(this.slides);
},
},
mounted() {
this.$slots.default().forEach((vNode) => {
console.log(vNode);
// this.slides.push(node.componentInstance)
});
},
};
</script>
如何检查vnode是否是幻灯片组件的实例?
The console log return the following object:
{
"__v_isVNode": true,
"__v_skip": true,
"type": {
"__hmrId": "6e03689e",
"__file": "<route to project>/src/components/CarouselSlide.vue"
},
"props": null,
"key": null,
"ref": null,
"scopeId": null,
"slotScopeIds": null,
"children": {
"_": 1
},
"component": null,
"suspense": null,
"ssContent": null,
"ssFallback": null,
"dirs": null,
"transition": null,
"el": null,
"anchor": null,
"target": null,
"targetAnchor": null,
"staticCount": 0,
"shapeFlag": 36,
"patchFlag": 0,
"dynamicProps": null,
"dynamicChildren": null,
"appContext": null
}
Slide is just the following:
<template>
<div class="slide">
<slot></slot>
</div>
</template>
<script>
export default {};
</script>
Here is a demo: link
I'll provide a simple example that would demonstrate the problem I'm facing.
So I have a page that includes the following code:
<Slider>
<Slide>
<p>test 1</p>
</Slide>
<Slide> test 2 </Slide>
<div>test3</div>
</Slider>
The problem is within the mounted section of the Slider.
Inside Slider
<template>
<div class="h-48 w-full">
<slot></slot>
</div>
</template>
<script>
export default {
data: () => ({
slides: [],
}),
watch: {
slides() {
console.log(this.slides);
},
},
mounted() {
this.$slots.default().forEach((vNode) => {
console.log(vNode);
// this.slides.push(node.componentInstance)
});
},
};
</script>
How do I check if the vNode is an instance of the Slide component?
The console log return the following object:
{
"__v_isVNode": true,
"__v_skip": true,
"type": {
"__hmrId": "6e03689e",
"__file": "<route to project>/src/components/CarouselSlide.vue"
},
"props": null,
"key": null,
"ref": null,
"scopeId": null,
"slotScopeIds": null,
"children": {
"_": 1
},
"component": null,
"suspense": null,
"ssContent": null,
"ssFallback": null,
"dirs": null,
"transition": null,
"el": null,
"anchor": null,
"target": null,
"targetAnchor": null,
"staticCount": 0,
"shapeFlag": 36,
"patchFlag": 0,
"dynamicProps": null,
"dynamicChildren": null,
"appContext": null
}
Slide is just the following:
<template>
<div class="slide">
<slot></slot>
</div>
</template>
<script>
export default {};
</script>
Here is a demo: link
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
与由JSX表示的React元素对象类似,Vue Vnode对象具有
类型
属性,它是DOM元素的字符串,也是VUE组件的组件。应该有一张支票:
Similarly to React element objects represented by JSX, Vue vnode objects have
type
property that is a string for DOM elements, and a component for Vue components.There should be a check: