简体   繁体   English

插槽未显示 - Vue.js

[英]Slots are not showing - Vue.js

I'm learning vue right now and have problems with understanding the slots.我现在正在学习 vue,但在理解插槽方面存在问题。

I got two components:我有两个组件:

  1. BaseIcon基本图标
<template>
     ...
     <slot name="test"></slot> 
     ...
<template/>
  1. EventCard事件卡
<template>
  <router-link class="event-link" :to="{ name: 'event-show', params: {id: '1'}}">
      ..
      <BaseIcon name="users" slot="test">{{ event.attendees.length }} attending</BaseIcon>
      ..
  </router-link>
</template>

But the the "slot" ain't replaced with the content in the BaseIcon component tags in EventCard .但是“插槽”并没有替换为 EventCard 中BaseIcon组件标签中的EventCard

You could use it like v-slot because slot syntax is deprecated as mentionedhere :您可以像v-slot一样使用它,因为不推荐使用slot语法,如此所述:

 <BaseIcon name="users">
   <template v-slot:test>
    {{ event.attendees.length }} attending
   </template>
</BaseIcon>

or a shorthand :或速记:

 <BaseIcon name="users" >
    <template #test>
    {{ event.attendees.length }} attending
   </template>
</BaseIcon>

The named v-slot can be used only in the template.命名的 v-slot 只能在模板中使用。 Default can be used in the component too.默认值也可以在组件中使用。 See the docs: https://v2.vuejs.org/v2/guide/components-slots.html#Abbreviated-Syntax-for-Lone-Default-Slots请参阅文档: https ://v2.vuejs.org/v2/guide/components-slots.html#Abbreviated-Syntax-for-Lone-Default-Slots

Also:还:

Note that v-slot can only be added to a (with one exception), unlike the deprecated slot attribute.请注意,v-slot 只能添加到 a(有一个例外),这与已弃用的 slot 属性不同。 https://v2.vuejs.org/v2/guide/components-slots.html#Named-Slots https://v2.vuejs.org/v2/guide/components-slots.html#Named-Slots

I had a very similar problem recently.我最近有一个非常相似的问题。 Turned out there was a tiny glitch in my HTML markup (I hadn't closed a bold tag).结果发现我的 HTML 标记中有一个小故障(我没有关闭粗体标记)。 Presumably, the Vue routine that converts template HTML into Javascript script is super sensitive to this kind of glitch (unlike browsers which have been brilliant at coping with them for years) - so the template was silently failing.据推测,将模板 HTML 转换为 Javascript 脚本的 Vue 例程对这种故障非常敏感(不像多年来一直擅长应对这些故障的浏览器)——所以模板默默地失败了。

I had to use a laborious "divide and conquer" process to track it down - chop everything out, then paste it back bit by bit.我不得不使用费力的“分而治之”的过程来追踪它——把所有东西都剪掉,然后一点一点地粘贴回去。 But there may be syntax checkers out there that would analyse your template markup thoroughly.但是可能有语法检查器可以彻底分析您的模板标记。

Worth checking.值得检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM