ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

vu_vue_插槽

2022-07-30 00:02:58  阅读:139  来源: 互联网

标签:slot vue 插槽 item 内容 vu 组件


插槽

该页面假设你已经阅读过了组件基础。如果你对组件还不太了解,推荐你先阅读它。

#插槽内容

Vue 实现了一套内容分发的 API,这套 API 的设计灵感源自 Web Components 规范草案,将 <slot> 元素作为承载分发内容的出口。

父组件

它允许你像这样合成组件:

<todo-button>
  Add todo
</todo-button>

子组件

然后在 <todo-button> 的模板中,你可能有:

<!-- todo-button 组件模板 -->
<button class="btn-primary">
  <slot></slot>
</button>

渲染效果

当组件渲染的时候,<slot></slot> 将会被替换为“Add Todo”。

<!-- 渲染 HTML -->
<button class="btn-primary">
  Add todo
</button>

不过,字符串只是开始!插槽还可以包含任何模板代码,包括 HTML:

<todo-button>
  <!-- 添加一个Font Awesome 图标 -->
  <i class="fas fa-plus"></i>
  Add todo
</todo-button>

或其他组件

<todo-button>
    <!-- 添加一个图标的组件 -->
  <font-awesome-icon name="plus"></font-awesome-icon>
  Add todo
</todo-button>

如果 <todo-button> 的 template 中没有包含一个 <slot> 元素,则该组件起始标签和结束标签之间的任何内容都会被抛弃

<!-- todo-button 组件模板 -->

<button class="btn-primary">
  Create a new item
</button>
<todo-button>
  <!-- 以下文本不会渲染 -->
  Add todo
</todo-button>

#渲染作用域

当你想在一个插槽中使用数据时,例如:

<todo-button>
  Delete a {{ item.name }}
</todo-button>

该插槽可以访问与模板其余部分相同的实例 property (即相同的“作用域”)。

Slot explanation diagram

插槽不能访问 <todo-button> 的作用域。例如,尝试访问 action 将不起作用:

<todo-button action="delete">
  Clicking here will {{ action }} an item
  <!-- `action` 未被定义,因为它的内容是传递*到* <todo-button>,而不是*在* <todo-button>里定义的。  -->
</todo-button>

请记住这条规则:

父级模板里的所有内容都是在父级作用域中编译的;子模板里的所有内容都是在子作用域中编译的。

#备用内容

有时为一个插槽设置具体的备用 (也就是默认的) 内容是很有用的,它只会在没有提供内容的时候被渲染。例如在一个 <submit-button> 组件中:

<button type="submit">
  <slot></slot>
</button>

我们可能希望这个 <button> 内绝大多数情况下都渲染文本“Submit”。为了将“Submit”作为备用内容,我们可以将它放在 <slot> 标签内:

<button type="submit">
  <slot>Submit</slot>
</button>

现在当我们在一个父级组件中使用 <submit-button> 并且不提供任何插槽内容时:

<submit-button></submit-button>

备用内容“Submit”将会被渲染:

<button type="submit">
  Submit
</button>

但是如果我们提供内容:

<submit-button>
  Save
</submit-button>

则这个提供的内容将会被渲染从而取代备用内容:

<button type="submit">
  Save
</button>

#具名插槽

有时我们需要多个插槽。例如对于一个带有如下模板的 <base-layout> 组件:

<div class="container">
  <header>
    <!-- 我们希望把页头放这里 -->
  </header>
  <main>
    <!-- 我们希望把主要内容放这里 -->
  </main>
  <footer>
    <!-- 我们希望把页脚放这里 -->
  </footer>
</div>

对于这样的情况,<slot> 元素有一个特殊的 attribute:name。这个 attribute 可以用来定义额外的插槽:

<div class="container">
  <header>
    <slot name="header"></slot>
  </header>
  <main>
    <slot></slot>
  </main>
  <footer>
    <slot name="footer"></slot>
  </footer>
</div>

一个不带 name<slot> 出口会带有隐含的名字“default”。


在向具名插槽提供内容的时候,我们可以在一个 <template> 元素上使用 v-slot 指令,并以 v-slot 的参数的形式提供其名称:

<base-layout>
  <template v-slot:header>
    <h1>Here might be a page title</h1>
  </template>

  <template v-slot:default>
    <p>A paragraph for the main content.</p>
    <p>And another one.</p>
  </template>

  <template v-slot:footer>
    <p>Here's some contact info</p>
  </template>
</base-layout>

现在 <template> 元素中的所有内容都将会被传入相应的插槽。

渲染的 HTML 将会是:

<div class="container">
  <header>
    <h1>Here might be a page title</h1>
  </header>
  <main>
    <p>A paragraph for the main content.</p>
    <p>And another one.</p>
  </main>
  <footer>
    <p>Here's some contact info</p>
  </footer>
</div>

注意,v-slot 只能添加在 <template> (只有一种例外情况)

#作用域插槽

自己的理解

感觉文档写的不够清晰,于是自己写了一篇: 父组件如何通过插槽拿到子组件中的数据,

所以下面的内容可以不看.


有时让插槽内容能够访问子组件中才有的数据是很有用的。当一个组件被用来渲染一个项目数组时,这是一个常见的情况,我们希望能够自定义每个项目的渲染方式。

例如,我们有一个组件,包含 todo-items 的列表。

app.component('todo-list', {
  data() {
    return {
      items: ['Feed a cat', 'Buy milk']
    }
  },
  template: `
    <ul>
      <li v-for="(item, index) in items">
        {{ item }}
      </li>
    </ul>
  `
})

我们可能会想把 {{ item }} 替换为 <slot>,以便在父组件上自定义。

<todo-list>
  <i class="fas fa-check"></i>
  <span class="green">{{ item }}</span>
</todo-list>

但是,这是行不通的,因为只有 <todo-list> 组件可以访问 item,我们将从其父组件提供插槽内容。

要使 item 可用于父级提供的插槽内容,我们可以添加一个 <slot> 元素并将其作为一个 attribute 绑定:

<ul>
  <li v-for="( item, index ) in items">
    <slot :item="item"></slot>
  </li>
</ul>

可以根据自己的需要将很多的 attribute 绑定到 slot 上。

<ul>
  <li v-for="( item, index ) in items">
    <slot :item="item" :index="index" :another-attribute="anotherAttribute"></slot>
  </li>
</ul>

绑定在 <slot> 元素上的 attribute 被称为插槽 prop。现在在父级作用域中,我们可以使用带值的 v-slot 来定义我们提供的插槽 prop 的名字:

<todo-list>
  <template v-slot:default="slotProps">
    <i class="fas fa-check"></i>
    <span class="green">{{ slotProps.item }}</span>
  </template>
</todo-list>

Scoped slot diagram

在这个例子中,我们选择将包含所有插槽 prop 的对象命名为 slotProps,但你也可以使用任意你喜欢的名字。

#独占默认插槽的缩写语法

在上述情况下,当被提供的内容只有默认插槽时,组件的标签才可以被当作插槽的模板来使用。这样我们就可以把 v-slot 直接用在组件上:

和原来的对比

image-20210925140106806

<todo-list v-slot:default="slotProps">
  <i class="fas fa-check"></i>
  <span class="green">{{ slotProps.item }}</span>
</todo-list>

这种写法还可以更简单。就像假定未指明的内容对应默认插槽一样,不带参数的 v-slot 被假定对应默认插槽:

和上面的对比

image-20210925140158937

<todo-list v-slot="slotProps">
  <i class="fas fa-check"></i>
  <span class="green">{{ slotProps.item }}</span>
</todo-list>

注意默认插槽的缩写语法不能和具名插槽混用,因为它会导致作用域不明确:

<!-- 无效,会导致警告 -->
<todo-list v-slot="slotProps">
  <i class="fas fa-check"></i>
  <span class="green">{{ slotProps.item }}</span>
  
  <template v-slot:other="otherSlotProps">
    slotProps is NOT available here
  </template>
</todo-list>

只要出现多个插槽,请始终为所有的插槽使用完整的基于 <template> 的语法:

<todo-list>
  <template v-slot:default="slotProps">
    <i class="fas fa-check"></i>
    <span class="green">{{ slotProps.item }}</span>
  </template>

  <template v-slot:other="otherSlotProps">
    ...
  </template>
</todo-list>

#解构插槽 Prop

作用域插槽的内部工作原理是将你的插槽内容包括在一个传入单个参数的函数里

function (slotProps) {
  // ... 插槽内容 ...
}

这意味着 v-slot 的值实际上可以是任何能够作为函数定义中的参数的 JavaScript 表达式。你也可以使用 ES2015 解构来传入具体的插槽 prop,如下:

和没有使用解构插槽的对比

image-20210925140428703

<todo-list v-slot="{ item }">
  <i class="fas fa-check"></i>
  <span class="green">{{ item }}</span>
</todo-list>

这样可以使模板更简洁,尤其是在该插槽提供了多个 prop 的时候。它同样开启了 prop 重命名等其它可能,例如将 item 重命名为 todo

没有使用prop重用名的对比

image-20210925140537207

<todo-list v-slot="{ item: todo }">
  <i class="fas fa-check"></i>
  <span class="green">{{ todo }}</span>
</todo-list>

你甚至可以定义备用内容,用于插槽 prop 是 undefined 的情形

定义备用内容和prop重用名的对比

image-20210925140700040

<todo-list v-slot="{ item = 'Placeholder' }">
  <i class="fas fa-check"></i>
  <span class="green">{{ item }}</span>
</todo-list>

#动态插槽名

动态指令参数也可以用在 v-slot 上,来定义动态的插槽名:

和非动态即静态插槽名的对比

image-20210925140951597

<base-layout>
  <template v-slot:[dynamicSlotName]>
    ...
  </template>
</base-layout>

#具名插槽的缩写

v-onv-bind 一样,v-slot 也有缩写,即把参数之前的所有内容 (v-slot:) 替换为字符 #。例如 v-slot:header 可以被重写为 #header

<base-layout>
  <template #header>
    <h1>Here might be a page title</h1>
  </template>

  <template #default>
    <p>A paragraph for the main content.</p>
    <p>And another one.</p>
  </template>

  <template #footer>
    <p>Here's some contact info</p>
  </template>
</base-layout>

然而,和其它指令一样,该缩写只在其有参数的时候才可用。这意味着以下语法是无效的:

<!-- This will trigger a warning -->

<todo-list #="{ item }">
  <i class="fas fa-check"></i>
  <span class="green">{{ item }}</span>
</todo-list>

如果你希望使用缩写的话,你必须始终以明确插槽名取而代之:

和错误写法的对比

image-20210925141145723

<todo-list #default="{ item }">
  <i class="fas fa-check"></i>
  <span class="green">{{ item }}</span>
</todo-list>

父组件如何通过插槽拿到子组件中的数据

输出

image-20210925135704251

图解

image-20210925135515753

跨组件插槽|动态插槽|公共插槽|插槽嵌套

代码示例

[vu_vue跨组件插槽 - 快捷方式.lnk](..\ab_code\vu_vue跨组件插槽 - 快捷方式.lnk)

输出

image-20210929190000052

图解

image-20210929185739809

outlayer.vue

<template>
  <div class="unit">
    <table border="1">
      innerlayer
      <template v-for="item in slotList" :key="item.name">
        <slot :name="item.name">
          <h6>{{ item.name }}</h6>
        </slot>
      </template>
    </table>
  </div>
</template>

<script lang="ts">
import { defineComponent, PropType } from 'vue'
import { SlotList } from './types'

export default defineComponent({
  name: 'unit',
  props: {
    slotList: {
      type: Array as PropType<SlotList[]>,
      required: true
    }
  },
  components: {},
  setup() {
    return {}
  }
})
</script>

<style scoped></style>

middle.vue

<template>
  <div class="middle">
    <table border="1">
      middlelayer
      <unit :slotList="slotList">
        <!-- 固定插槽:一般我们把一些公共的插槽放在这里 -->
        <template #slotA>
          <h6>固定插槽|公共插槽:这是从middle.vue传递过来的插槽内容A</h6>
        </template>
        <template #slotC>
          <h6>固定插槽|公共插槽:这是从middle.vue传递过来的插槽内容C</h6>
        </template>
        <!-- 跨组件插槽 | 插槽中嵌套插槽: 这里的插槽是不同组件的插槽变化 -->
        <template v-for="item in dynamicSlotList" :key="item.name" #[item.name]>
          <slot :name="item.name"></slot>
        </template>
      </unit>
    </table>
  </div>
</template>

<script lang="ts">
import { defineComponent, PropType } from 'vue'
import { SlotList } from './types'
import Unit from './unit.vue'

export default defineComponent({
  name: 'middle',
  props: {
    slotList: {
      type: Array as PropType<SlotList[]>,
      required: true
    }
  },
  components: {
    Unit
  },
  setup(props) {
    const dynamicSlotList: SlotList[] = props.slotList?.filter(
      (item: SlotList) => {
        return item.isFixed ? false : true
      }
    )
    return {
      dynamicSlotList
    }
  }
})
</script>

<style scoped></style>

inner.vue

<template>
  <div class="unit">
    <table border="1">
      innerlayer
      <template v-for="item in slotList" :key="item.name">
        <slot :name="item.name">
          <h6>{{ item.name }}</h6>
        </slot>
      </template>
    </table>
  </div>
</template>

<script lang="ts">
import { defineComponent, PropType } from 'vue'
import { SlotList } from './types'

export default defineComponent({
  name: 'unit',
  props: {
    slotList: {
      type: Array as PropType<SlotList[]>,
      required: true
    }
  },
  components: {},
  setup() {
    return {}
  }
})
</script>

<style scoped></style>

types.ts

export interface SlotList {
  name: string
  isFixed: boolean
}

标签:slot,vue,插槽,item,内容,vu,组件
来源: https://www.cnblogs.com/zhuoss/p/16534039.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有