英文:
How to bind on properties of a named slot
问题
I've translated the provided content:
关于 这个链接,我正在尝试实现与该链接中提供的“Scoped Slots”示例相同的示例。
如下所示的代码片段中,我创建了名为 scopedSlot 的插槽,并指定了一些属性 text 和 count:
<footer>
<slot name="footer"></slot>
<div>
<slot name="scopedSlot" :text="greetingMessage" :count="1"></slot>
</div>
</footer>
在 App.vue 中,我试图绑定插槽的属性,如下所示的代码片段:
<template #scopedSlot v-slot="slotProps">
{{ slotProps.text }} {{ slotProps.count }}
</template>
但是后面的代码生成了以下关于 v-slot 的错误:
一个元素不能具有多个 'v-slot' 指令
请告诉我如何正确绑定插槽属性。
App:
<template>
<img alt="Vue logo" src="./assets/logo.png">
<HelloWorld msg="Welcome to Your Vue.js App">
<BaseLayer_0>
<template #header>
<h1>header</h1>
</template>
<template #default>
<p>default</p>
</template>
<template #footer>
<p>footer</p>
</template>
<template #scopedSlot v-slot="slotProps">
{{ slotProps.text }} {{ slotProps.count }}
</template>
</BaseLayer_0>
</template>
<script>
import HelloWorld from './components/HelloWorld.vue'
import BaseLayer_0 from './components/BaseLayer_0.vue'
export default {
name: 'App',
components: {
HelloWorld,
BaseLayer_0
}
}
</script>
BaseLayer_0:
<template>
<div class="container">
<header>
<slot name="header"></slot>
</header>
<main>
<slot></slot>
</main>
<footer>
<slot name="footer"></slot>
<div>
<slot name="scopedSlot" :text="greetingMessage" :count="1"></slot>
</div>
</footer>
</div>
</template>
<script>
export default {
name: 'BaseLayer_0'
}
</script>
图片:
英文:
Referring to this link, i am trying to implement the same example of Scoped Slotsprovided in the url.
as shown in the following code snippet, i created the slot named scopedSlot and specified some properties text and count:
<footer>
<slot name="footer"></slot>
<div>
<slot name="scopedSlot" :text="greetingMessage" :count="1"></slot>
</div>
</footer>
in the App.vue, i am trying to bind on the properties of the slot as shown in the following code snippet:
<template #scopedSlot v-slot="slotProps">
{{slotProps.text}} {{ slotProps.count }}
</template>
but the latter code generates the following error on v-slot:
An element cannot have multiple 'v-slot' directives
please let me know how to correctly bind on a slot properties
App:
<template>
<img alt="Vue logo" src="./assets/logo.png">
<HelloWorld msg="Welcome to Your Vue.js App"/>
<BaseLayer_0>
<template #header>
<h1>header</h1>
</template>
<template #default>
<p>default</p>
</template>
<template #footer>
<p>footer</p>
</template>
<template #scopedSlot v-slot="slotProps">
{{slotProps.text}} {{ slotProps.count }}
</template>
</BaseLayer_0>
</template>
<script>
import HelloWorld from './components/HelloWorld.vue'
import BaseLayer_0 from './components/BaseLayer_0.vue'
export default {
name: 'App',
components: {
HelloWorld,
BaseLayer_0
}
}
</script>
BaseLayer_0
<template>
<div class="container">
<header>
<slot name="header"></slot>
</header>
<main>
<slot></slot>
</main>
<footer>
<slot name="footer"></slot>
<div>
<slot name="scopedSlot" :text="greetingMessage" :count="1"></slot>
</div>
</footer>
</template>
<script>
export default {
name: 'BaseLayer_0',
}
</script>
image:
答案1
得分: 1
首先,在 BaseLayer_0.vue 文件中,在 <slot name="scopedSlot" :text="greetingMessage" :count="1"></slot> 部分,你使用了 : 作为 v-bind 的缩写来绑定 text 属性。因此,greetingMessage 被视为一个变量,但你在脚本中并没有声明 greetingMessage 变量。
因此,你需要添加以下内容:
#BaseLayer_0.vue
<script>
export default {
name: 'BaseLayer_0',
data() {
return {
greetingMessage: 'hello word'
}
}
}
</script>
其次,在下面的代码片段中,也存在一个问题:
#app.vue
<template #scopedSlot v-slot="slotProps">
{{slotProps.text}} {{ slotProps.count }}
</template>
它应该被更正为:
<template #scopedSlot="{ text, count }">
{{ text }} {{ count }}
</template>
英文:
Firstly, in BaseLayer_0.vue, in the
<slot name="scopedSlot" :text="greetingMessage" :count="1"></slot> section,
you used the shorthand : for v-bind to bind the text attribute. Therefore, greetingMessage is treated as a variable, but you haven't declared the greetingMessage variable in the script.
Hence, you need to add the following:
#BaseLayer_0.vue
<script>
export default {
name: 'BaseLayer_0',
data() {
return {
greetingMessage: 'hello word'
}
}
}
</script>
Secondly, in the following code snippet, there is also an issue:
#app.vue
<template #scopedSlot v-slot="slotProps">
{{slotProps.text}} {{ slotProps.count }}
</template>
It should be corrected to:
<template #scopedSlot="{ text, count }">
{{ text }} {{ count }}
</template>
答案2
得分: 0
正确的语法是
<template #scopedSlot="slotProps">
或者
<template v-slot:scopedSlot="slotProps">
# 是 v-slot: 的缩写,所以在你最初的代码中,你在没有使用 slotProps 的情况下分配给了 scopedSlot,然后又分配给了默认插槽,这次使用了 slotProps,Vue 报错了。
英文:
The correct syntax is
<template #scopedSlot="slotProps">
or
<template v-slot:scopedSlot="slotProps">
The # is shorthand for v-slot:, so in your initial code, you were assigning to the scopedSlot without using slotProps, and then again to the default slot, this time using slotProps, and Vue complained.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。



评论