这两种类型object,并Record<any, any>在我看来,包括相同的一组有效的对象,这是任何事情上typeof x === "object。两者之间有什么区别吗?
我有一个嵌套在几层组件内的组件。作用域槽可以向下传递给该组件。我想检测什么时候不是。
例子:
Vue.component("parent", {
template: `
<child>
<template slot="expand" slot-scope="{data}" v-if="$scopedSlots.expand">
<slot name="expand" :data="data"></slot>
</template>
</child>
`,
});
Vue.component("child", {
template: `
<div>
<slot name="expand" :data="1"></slot>
<div @click="$scopedSlots.expand && log()">Child</div>
</div>
`,
methods: {
log() {
console.log(this.$scopedSlots.expand);
console.log("This shouldn't work");
}
}
});
new Vue({
el: "#app",
template: `<parent></parent>`,
})
Run Code Online (Sandbox Code Playgroud)
在此示例中,作用域槽永远不会从根组件传递到<parent>. 结果,我本以为$scopedSlots.expand和v-ifin<parent>都是假的,所以没有插槽会被传递到<child>,并且单击不会执行任何操作。
然而,这似乎是错误的,原因有两个:
$scopedSlots.expand在内部存储为函数,因此它将始终评估为真,正如您在 CodePen 的控制台中看到的那样。<template>s 总是渲染,即使使用v-if="false". 因此,即使$scopedSlots.expandinside 评估为假<parent> …