admin管理员组文章数量:1355096
Please see this minimum example
Test.vue
<template>
<div>
<slot name="this_is_not_scoped_slots"/>
</div>
</template>
<script >
import Vue from "vue";
export default Vue.extend({
mounted() {
console.log(Object.keys(this.$scopedSlots));
}
});
</script>
App.vue
<template>
<Test>
<template #this_is_not_scoped_slots>But it shows in this.$scopedSlots</template>
</Test>
</template>
<script>
import Test from "./Test.vue";
export default {
ponents: {
Test
}
};
</script>
In the above example, this console will log out ["this_is_not_scoped_slots"]
.
Why is this happening?
There are two properties in Vue instance
this.$slots
this.$scopedSlots
These two act really differently:
- If you use
v-slot:my_scope_name="{ someValue }"
, thenmy_scope_name
won't show up inthis.$slots
- However, no matter what you define, your named slots will always show up in
this.$scopedSlots
Why is this happening?
I'm building a library, I want to conditional rendering if the user has provided named slots or not, should I always use this.$scopedSlots
to detect those things?
Please see this minimum example
Test.vue
<template>
<div>
<slot name="this_is_not_scoped_slots"/>
</div>
</template>
<script >
import Vue from "vue";
export default Vue.extend({
mounted() {
console.log(Object.keys(this.$scopedSlots));
}
});
</script>
App.vue
<template>
<Test>
<template #this_is_not_scoped_slots>But it shows in this.$scopedSlots</template>
</Test>
</template>
<script>
import Test from "./Test.vue";
export default {
ponents: {
Test
}
};
</script>
In the above example, this console will log out ["this_is_not_scoped_slots"]
.
Why is this happening?
There are two properties in Vue instance
this.$slots
this.$scopedSlots
These two act really differently:
- If you use
v-slot:my_scope_name="{ someValue }"
, thenmy_scope_name
won't show up inthis.$slots
- However, no matter what you define, your named slots will always show up in
this.$scopedSlots
Why is this happening?
I'm building a library, I want to conditional rendering if the user has provided named slots or not, should I always use this.$scopedSlots
to detect those things?
- 1 I believe the new slot syntax was implemented this way to allow for easier migration to Vue 3. See github./vuejs/rfcs/blob/master/active-rfcs/…. – skirtle Commented Sep 5, 2020 at 17:33
1 Answer
Reset to default 8According to the official API :
- ....
- All
$slots
are now also exposed on$scopedSlots
as functions. If you work with render functions, it is now remended to always access slots via$scopedSlots
, whether they currently use a scope or not. This will not only make future refactors to add a scope simpler, but also ease your eventual migration to Vue 3, where all slots will be functions.
本文标签: javascriptWhy Vue regular slots also available in thisscopedSlotsStack Overflow
版权声明:本文标题:javascript - Why Vue regular slots also available in this.$scopedSlots? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744036482a2579865.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论