Jos*_*ang 4 javascript vue.js vue-component vuejs2 vuejs3
请参阅这个最小示例
<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>
Run Code Online (Sandbox Code Playgroud)
<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 {
components: {
Test
}
};
</script>
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,该控制台将注销["this_is_not_scoped_slots"]。
为什么会发生这种情况?
Vue实例中有两个属性
this.$slotsthis.$scopedSlots这两者的行为确实不同:
v-slot:my_scope_name="{ someValue }",则my_scope_name不会出现在this.$slotsthis.$scopedSlots为什么会发生这种情况?
我正在构建一个库,如果用户是否提供了命名插槽,我想要条件渲染,我是否应该始终使用它this.$scopedSlots来检测这些东西?