0

我正在使用 Vuejs。我创建了两个组件:FirstSecond.

  1. First呈现Second.
  2. Second有一个命名槽。
  3. First包含一个<template>, 进入Second的命名槽。
  4. First's <template>- 还有另一个命名的插槽。
  5. Second's内部<slot>- 有一个<template>, 进入First's<template>的命名槽。

我得到的是First'在' 插槽<template>内渲染。Second

这是正确的,但我也希望看到Second'在of中<template>呈现。但我没有。<slot>First<template>

这是代码:

var First = Vue.extend({
  template: `
    <div>
      In first<br/>
      <second>
        <template slot="slot1">
          In template of First
          <slot name="slot2">
          </slot>
        </template>
      </second>
    </div>
  `
})
Vue.component('first', First)

var Second = Vue.extend({
  template: `
    <div>
      In second<br/>
      <slot name="slot1">
        <template slot="slot2">
          In template of Second
        </template>
      </slot>
    </div>
  `
})
Vue.component('second', Second)

这是输出:

In first
In second
In template of First

这是 jsfiddle:https ://jsfiddle.net/obeobe/f98kr4ye/

我想得到这个输出:

In first
In second
In template of First
In template of Second

我可以通过插槽实现这一点吗?如果没有,是否可以通过另一种方式实现,但不创建第三个组件?

编辑:一个可能的现实用例:一个 List 组件,它允许每个项目的 HTML 内容由其主机定义,并且可以在主机指定的位置将其自己的一些内容注入到主机的内容中。

例如,像这样:

主机组件:

<div class="myList">
    <list v-bind:items="usersArray">
        <template slot="itemSlot" slotScope="item">
            <div>{{ item.name }}</div>
            <div>{{ item.country }}</div>
            <div>{{ item.phone }}</div>
            <slot name="actions"> <--------- the spot for the List component to inject generic actions
            </slot>
            <div>{{ item.age }}</div>
        </template>
    </list>
</div>

“列表”组件:

<div>
    <div v-for="(anItem, idx) in items">
        <div>{{ idx }}</div>
        <slot name="itemSlot" v-bind:item="anItem">
            <template slot="actions">
                <a v-on:click="duplicateItem(anItem)">Duplicate</a> <---------- the "duplicateItem" method would be implemented inside this List component
                <a>Edit</a>
                <a>Delete</a>
            </template>
        </slot>
    </div>
</div>
4

1 回答 1

1

AFAIK,Vue.js 中没有双向插槽

<div id="app">
  <first>
         ─────────────────────┐
  </first>                    │
</div>                        │
                              │
<div>                         │
  In first<br/>               │
  <second>                    │
    <template slot="slot1">  ─────┐
      In template of First    │   │
      <slot name="slot2">  <──┘ ┄┄│┄┄&gt; This slot will replaced by slot2
      </slot>                     │    template child of first component.
    </template>                   │
  </second>                       │
</div>                            │
                                  │
<div>                             │
  In second<br/>                  │
  <slot name="slot1">  <──────────┘
    <template slot="slot2">  ┄┄┄┄┄┄┄┄> This is fallback content it will 
      In template of Second            shows up if no content provided.
    </template>                        But slot template is used so
                                       this will go to replace slot2
                                       inside slot.
  </slot>
</div>

由于组件内部没有slot2或任何插槽定义,slot因此您In template of Second永远不会出现。

在你的例子中,我认为没有办法做到这一点。实现这一目标的另一种方法是duplicateItem通过插槽范围绑定。

<div class="myList">
  <list v-bind:items="usersArray">
    <template slot="itemSlot" slotScope="{ item, duplicateItem }">
      <div>{{ item.name }}</div>
      <div>{{ item.country }}</div>
      <div>{{ item.phone }}</div>
      <div>
        <a v-on:click="duplicateItem(item)">Duplicate</a>
        <a>Edit</a>
        <a>Delete</a>
      </div>
      <div>{{ item.age }}</div>
    </template>
  </list>
</div>

<div>
  <div v-for="(anItem, idx) in items">
    <div>{{ idx }}</div>
      <slot name="itemSlot"
        v-bind:item="anItem"
        v-bind:duplicateItem="duplicateItem">
      </slot>
    </div>
  </div>
</div>
于 2020-02-04T08:31:50.067 回答