- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Vue 对象有一个非常有用的成员,叫做 $attrs
。什么$attrs does 包含所有未被识别为当前组件 props 的属性。 $attrs
的一个很好的例子是 here .
我想知道 $scopedSlots
是否有 $attrs
的等效项。我目前使用的方法类似于 https://stackoverflow.com/a/50892881/6100005 中的第一个建议。 . $scopedSlots
的问题在于它还传递了已定义的插槽。要在此处使用该示例:
<template>
<wrapper>
<b-table :foo="foo" v-bind="$attrs" v-on="$listeners">
<template v-for="(_, slot) of $scopedSlots" v-slot:[slot]="scope"><slot :name="slot" v-bind="scope"/></template>
</b-table>
<slot name="mySlot"></slot>
</wrapper>
</template>
<script>
export default {
props: {
// let's pretend that `b-table` has a prop `foo` that is default `false`
foo: {
type: boolean,
default: true,
}
}
}
</script>
现在,由于 $attrs
的行为,foo
不会被绑定(bind)两次,但是 mySlot
将被发送到两个 wrapper
和 b-table
。
那么我如何才能将所有插槽传递给除了我正在定义自己的插槽?
我的一个想法是拥有
computed: {
bTableSlots() {
Object.keys(this.$scopedSlots)
.filter( key => key!=='mySlot' )
.reduce( (res, key) => (res[key] = this.$scopedSlots[key], res), {} );
}
}
然后
<template v-for="(_, slot) of bTableSlots" v-slot:[slot]="scope"><slot :name="slot" v-bind="scope"/></template>
想知道是否有更好的方法来做到这一点。谢谢!
最佳答案
那么除了我自己定义的插槽之外,如何传递所有插槽?
为什么这甚至是一个问题?如果<b-table>
没有名称为 mySlot
的命名插槽, 它只会忽略它 - 最后它只是其中的一个条目 $scopedSlots
组件永远不会访问的属性。而且由于作用域插槽作为函数传递,您无需支付任何额外的费用。
当 Vue 编译器看到插槽中的内容 ( <template #slotName>
) 时,它会将内容编译成 Array
。的 VNode
s 或函数返回 Array
的 VNode
s 并将其传递给子组件。事实上,您的子组件将其进一步向下传递不会产生任何额外成本。它是对现有数组的引用或对现有函数的引用(对于作用域插槽),在这两种情况下,如果更下方的组件不知 Prop 有完全相同名称的插槽,则没有额外费用,除了在 $scopedSlots
中还有一项...
如果您觉得这是一个问题,恐怕过滤是唯一的方法 - 您可以按照自己的方式或通过一些辅助数组 [ 'slot1', 'slot2' ]
定义所有现有的 b-table
插槽并过滤其他所有内容(恕我直言,每次 b-table
添加新插槽时都需要更新组件)...
我理解这个想法并看到与 $attrs
的相似之处- 可能有类似 $unknownSlots
的东西在组件上保存当前组件未定义的插槽,但现在 Vue 公共(public) API 中没有类似的东西......
关于javascript - 视觉 : wrapper for slots ignoring already-defined slots,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64213179/
我假设 jls 中描述的转换是根据优先级排序的。首先具有更高的优先级。 jls 因此我解决了 Boxing 比 Unboxing 具有更高的优先级。我决定检验这个假设。 研究以下代码: public
谁能解释一下我的 html 设计出了什么问题 Profile 问题是 div 标签(#profile wrapper)正在增加它的 margin top,如果我增加 #intro w
下面的完整代码示例(已成功编译)是我的问题的简化且稍微做作的示例。 NatPair 是一对 Nat ,我想使用函数 Num 将二进制 NatPair 操作逐点“提升”到 lift_binary_op_
给定以下函数: public void convertToWrapper(long[] longsToConvert) { } 和 public void convertToPrimitiv
我有这样的代码: class Base { } class Derived : Base { } class Wrapper { public T Value { get; } pub
我正在安装 SonarQube v5.0。 我正在运行 Windows Server 2012 64 位(虚拟操作系统)、Java 1.8 64 位和 SonarQube windows-x86-64
我正在为我的一个组件编写测试用例,该组件具有路由器(使用 withrouter)。我收到错误 wrapper.find is not a function。基本要求是需要检查我的渲染中是否存在标签,还
如何制作 在不改变结构的情况下,内部包装器比包装器本身更大? HTML CSS .page { width: 100%; height: 400px; bor
为了引导 Gradle-Wrapper,我需要从需要 HTTP Basic-Auth 的 Artifactory 中提取 Gradle 发行版。我的构建环境无法访问外部世界 - 这被公司代理阻止。我的
我正在构建一个Spring-Boot应用程序(2.1.4.RELEASE),并且正在使用它创建一个Gradle Wrapper gradle clean wrapper 我的gradle版本是5.4
以下代码取自这篇文章:How to create Scala swing wrapper classes with SuperMixin? import scala.swing._ import ja
我想同时使用 Boost.Asio 的 strand 和 prioritized wrapper。 在我编写代码之前,我阅读了以下信息: Boost asio priority and strand
Android Studio 的 Gradle 选项到底有什么区别: Android Studio->Preferences->Gradle 使用默认的gradle wrapper(推荐)和使用可定制
目前我有一个设置宽度为 1240 像素的包装器。可以想象,这意味着我页面中心的 1240 像素始终被包装器覆盖。 我还有一张宽度为 3160 像素的图像(1920 像素的图像大小,在中间切割并由 12
这只是一个练习,但我无法弄清楚其中的歧义: private static void flipFlop(String str, int i, Integer iRef) { System.out.pri
所以我现在正在编写我的网站,我正在使用两个 div 在背景中创建某种渐变,其中 div#bg1 为灰色,div#bg2 为深灰色。但是当我添加包装器 div 时,bg1 和 bg2 仍然遵循包装器的顶
已解决:必须 float #main div,并对结构进行许多其他重大更改。但致命一击是 float 的。 可以在 http://thepremium.ca/amodestblog 查看有问题的网站
操作系统:Windows 8.1 Visual Studio 高级版 2013 我有一个复杂的 MVC 应用程序,我已经运行了多年。我能够毫无问题地在调试中运行应用程序。然而,就在今天下午,当我尝试发
在带有 python 3.6.8 的 Ubuntu 18.04 上,尝试安装 Airflow。当我运行airflow initdb命令时,抛出以下错误 Traceback (most recent c
我将我的 Android Studio 升级到 2.3 版,然后开始出现此错误: Error:org.gradle.wrapper.WrapperExecutor.forProjectDirector
我是一名优秀的程序员,十分优秀!