如何绑定到具名插槽的属性

huangapple go评论51阅读模式
英文:

How to bind on properties of a named slot

问题

I've translated the provided content:

关于 这个链接,我正在尝试实现与该链接中提供的“Scoped Slots”示例相同的示例。
如下所示的代码片段中,我创建了名为 scopedSlot 的插槽,并指定了一些属性 textcount

<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:

&lt;footer&gt;
	&lt;slot name=&quot;footer&quot;&gt;&lt;/slot&gt;
	&lt;div&gt;
	  &lt;slot name=&quot;scopedSlot&quot; :text=&quot;greetingMessage&quot; :count=&quot;1&quot;&gt;&lt;/slot&gt;
	&lt;/div&gt;
&lt;/footer&gt;

in the App.vue, i am trying to bind on the properties of the slot as shown in the following code snippet:

&lt;template #scopedSlot v-slot=&quot;slotProps&quot;&gt;
  {{slotProps.text}} {{ slotProps.count }}
&lt;/template&gt;

but the latter code generates the following error on v-slot:

An element cannot have multiple &#39;v-slot&#39; directives

please let me know how to correctly bind on a slot properties

App:

&lt;template&gt;
  &lt;img alt=&quot;Vue logo&quot; src=&quot;./assets/logo.png&quot;&gt;
  &lt;HelloWorld msg=&quot;Welcome to Your Vue.js App&quot;/&gt;

  &lt;BaseLayer_0&gt;
	&lt;template #header&gt;
	  &lt;h1&gt;header&lt;/h1&gt;
	&lt;/template&gt;

	&lt;template #default&gt;
	  &lt;p&gt;default&lt;/p&gt;
	&lt;/template&gt;

	&lt;template #footer&gt;
	  &lt;p&gt;footer&lt;/p&gt;
	&lt;/template&gt;

	&lt;template #scopedSlot v-slot=&quot;slotProps&quot;&gt;
	  {{slotProps.text}} {{ slotProps.count }}
	&lt;/template&gt;
	
  &lt;/BaseLayer_0&gt;

&lt;/template&gt;

&lt;script&gt;
import HelloWorld from &#39;./components/HelloWorld.vue&#39;
import BaseLayer_0 from &#39;./components/BaseLayer_0.vue&#39;

export default {
  name: &#39;App&#39;,
  components: {
	HelloWorld,
	BaseLayer_0
  }
}
&lt;/script&gt;

BaseLayer_0

&lt;template&gt;
  &lt;div class=&quot;container&quot;&gt;
	&lt;header&gt;
		&lt;slot name=&quot;header&quot;&gt;&lt;/slot&gt;
	&lt;/header&gt;

	&lt;main&gt;
		&lt;slot&gt;&lt;/slot&gt;
	&lt;/main&gt;

	&lt;footer&gt;
		&lt;slot name=&quot;footer&quot;&gt;&lt;/slot&gt;
		&lt;div&gt;
		  &lt;slot name=&quot;scopedSlot&quot; :text=&quot;greetingMessage&quot; :count=&quot;1&quot;&gt;&lt;/slot&gt;
		&lt;/div&gt;
	&lt;/footer&gt;

  

&lt;/template&gt;

&lt;script&gt;

export default {
name: 'BaseLayer_0',

}
</script>

image:

如何绑定到具名插槽的属性

答案1

得分: 1

首先,在 BaseLayer_0.vue 文件中,在 &lt;slot name=&quot;scopedSlot&quot; :text=&quot;greetingMessage&quot; :count=&quot;1&quot;&gt;&lt;/slot&gt; 部分,你使用了 : 作为 v-bind 的缩写来绑定 text 属性。因此,greetingMessage 被视为一个变量,但你在脚本中并没有声明 greetingMessage 变量。

因此,你需要添加以下内容:

#BaseLayer_0.vue

&lt;script&gt;
export default { 
  name: &#39;BaseLayer_0&#39;,
  data() {
    return {
      greetingMessage: &#39;hello word&#39;
    }
  }
}
&lt;/script&gt;

其次,在下面的代码片段中,也存在一个问题:

#app.vue

&lt;template #scopedSlot v-slot=&quot;slotProps&quot;&gt;
   {{slotProps.text}} {{ slotProps.count }}
&lt;/template&gt;

它应该被更正为:

&lt;template #scopedSlot=&quot;{ text, count }&quot;&gt;
   {{ text }} {{ count }}
&lt;/template&gt;

codesandbox 示例

英文:

Firstly, in BaseLayer_0.vue, in the

&lt;slot name=&quot;scopedSlot&quot; :text=&quot;greetingMessage&quot; :count=&quot;1&quot;&gt;&lt;/slot&gt; 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

&lt;script&gt;
export default { 
  name: &#39;BaseLayer_0&#39;,
  data() {
    return {
      greetingMessage: &#39;hello word&#39;
    }
  }
}
&lt;/script&gt;

Secondly, in the following code snippet, there is also an issue:

#app.vue

&lt;template #scopedSlot v-slot=&quot;slotProps&quot;&gt;
   {{slotProps.text}} {{ slotProps.count }}
&lt;/template&gt;

It should be corrected to:

&lt;template #scopedSlot=&quot;{ text, count }&quot;&gt;
   {{ text }} {{ count }}
&lt;/template&gt;

codesandbox demo

答案2

得分: 0

正确的语法是

&lt;template #scopedSlot=&quot;slotProps&quot;&gt;

或者

&lt;template v-slot:scopedSlot=&quot;slotProps&quot;&gt;

#v-slot: 的缩写,所以在你最初的代码中,你在没有使用 slotProps 的情况下分配给了 scopedSlot,然后又分配给了默认插槽,这次使用了 slotProps,Vue 报错了。

英文:

The correct syntax is

&lt;template #scopedSlot=&quot;slotProps&quot;&gt;

or

&lt;template v-slot:scopedSlot=&quot;slotProps&quot;&gt;

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.

huangapple
  • 本文由 发表于 2023年6月8日 14:13:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76429058.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定