首页 > 其他分享 >vue3 | slots

vue3 | slots

时间:2023-02-07 20:23:53浏览次数:35  
标签:slot name 插槽 vue3 内容 res 组件 slots

一、什么是插槽

插槽就是子组件中的提供给父组件使用的一个占位符,用<slot></slot>  表示,父组件可以在这个占位符中填充任何模板代码,如 HTML、组件等,填充的内容会替换子组件的<slot></slot>标签,父组件填充的内容称为插槽内容

  • 子组件不提供插槽时,父组件填充失效
  • 父组件无填充时,<slot></slot>中的备用内容会启用生效
  • 父级模板里的所有内容都是在父级作用域中编译的,子模板里的所有内容都是在子作用域中编译的,互不影响

二、匿名插槽(默认插槽)

介绍

在外部组件没有提供任何内容的情况下,可以使用匿名插槽提供默认内容。

使用场景

比如
MyComponent.vue

<n-el>
    <slot>这是一个默认展示的内容</slot>
</n-el>

//组件使用
<MyComponent/>

如下:

image.png

如果外部组件提供了插槽内容,我们提供的内容会覆盖掉默认的内容

如下:

<MyComponent>加入一段文字</MyComponent>

image.png

注: 插槽内容可以是任意合法的模板内容,不局限于文本。

如下:

<MyComponent>
  <n-button>加入一个按钮</n-button>
</MyComponent>

image.png

三、具名插槽

介绍

<slot>元素带有 name 属性的插槽被称为具名插槽。

使用场景

作用于一个组件中拥有多个插槽,而name相当于插槽的标识,用来给各个插槽分配唯一的 ID。

MyComponent.vue

<n-el class="flex flex-col justify-center items-center h-[80vh] w-full">
  <n-el class="text-[pink] text-[18px] mb-2">组件相关内容</n-el>
  <slot name="head"></slot>
  <slot name="main"></slot>
  <slot name="footer"></slot>
</n-el>

外部引用该组件

<MyComponent>
  <template v-slot:head>
    <n-el>这是头部内容</n-el>
  </template>
  <template v-slot:main>
    <n-el>这是主体内容</n-el>
  </template>
  <template v-slot:footer>
    <n-el>这是尾部内容</n-el>
  </template>
  <template v-slot:no>
    <n-el>组件中没有可匹配的插槽name,不显示</n-el>
  </template>
</MyComponent>

v-slot可以简写成#

    <MyComponent>
      <template #head>
        <n-el>这是头部内容</n-el>
      </template>
      <template #main>
        <n-el>这是主体内容</n-el>
      </template>
      <template #footer>
        <n-el>这是尾部内容</n-el>
      </template>
      <template #no>
        <n-el>组件中没有可匹配的插槽name,不显示</n-el>
      </template>
    </MyComponent>

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

image.png

image.png

注意: 匿名插槽也有自己的name,只不过 name 会被隐式地命名为default

image.png

上面的写法等价于:

image.png

image.png

四、动态插槽名

<MyComponent>
  <template v-slot:[slotName]>
    ...
  </template>

  <!-- 缩写为 -->
  <template #[slotName]>
    ...
  </template>
</MyComponent>

如下
MyComponent.vue

<n-el>
  <n-el>组件相关内容</n-el>
  <slot name="head"></slot>
</n-el>
<script setup lang="ts">
  const data = 'head'
</script>
<template>
    ......
    <MyComponent>
      <template #[data]> 头部内容 </template>
    </MyComponent>
    .......
</template>

image.png

五、作用域插槽

介绍

作用域插槽可以让父级外层组件能够访问子组件的数据,子组件向将数据提供给插槽,组件 props 传递数据的方式,子组件向插槽传递一个attributes,父组件通过v-slot带的值(任意命名)来获取子组件的数据。

使用场景

(一)、默认插槽

MyComponent.vue

<n-el class="flex flex-col justify-center items-center h-[80vh] w-full">
  <slot content="螺蛳粉" data="10"></slot>
</n-el>

写法一: v-slot 写在组件上

   <MyComponent v-slot="res">
      <n-el>老板:来 {{ res.data }} 份 {{ res.content }} ~</n-el>
    </MyComponent>

image.png

注意: v-slot="res"  可以类比这里的函数签名,和函数的参数类似,我们也可以在  v-slot  中使用解构:

<MyComponent v-slot="{ data, content }">
  <n-el>
    老板:来 {{ data }} 份 {{ content }} ~
  </n-el>
</MyComponent>

写法二: v-slot 写在 template 上

   <MyComponent>
      <template v-slot:default="res">
        <n-el>老板:来 {{ res.data }} 份 {{ res.content }} ~</n-el>
      </template>

      //或者
      <template #default="res">
        <n-el>老板:来 {{ res.data }} 份 {{ res.content }} ~</n-el>
      </template>

    </MyComponent>

(二)、具名插槽

具名作用域插槽的工作方式也是类似的,插槽 props 可以作为  v-slot  指令的值被访问到:v-slot:name="slotProps"。当使用缩写时是这样:

MyComponent.vue

<n-el class="flex flex-col justify-center items-center h-[80vh] w-full">
  <slot content="北京烤鸭" data="5" name="food1"></slot>
  <slot content="长沙臭豆腐" data="15" name="food2"></slot>
</n-el>
   <MyComponent>
      <template #food1="res">
        <n-el>老板:来 {{ res.data }} 份 {{ res.content }} ~</n-el>
      </template>
      <template #food2="res">
        <n-el>老板:来 {{ res.data }} 份 {{ res.content }} ~</n-el>
      </template>
    </MyComponent>

image.png

标签:slot,name,插槽,vue3,内容,res,组件,slots
From: https://www.cnblogs.com/yangyukeke/p/17099694.html

相关文章

  • vue3 | shallowReactive 、shallowRef、triggerRef
    shallowReactive使用reactive声明的变量为递归监听,使用shallowReactive声明的变量为非递归监听(通俗的讲就是reactive创建的对象将会被vue内部进行递归监听,可以监......
  • Vite+Vue3+TS 自定义全局组件,无法不能高亮解析的解决办法
    检查package.json中的devDependencies是否安装@vue/runtime-core依赖,没有的话,安装后重启VSCode一、安装依赖$yarnadd@vue/runtime-core-D$yarnaddunplugin-vue-......
  • vue3+ts基础使用
    详见:vue官方文档响应式数据<scriptsetuplang="ts">import{ref,reactive,computed}from"vue";importtype{Ref}from"vue";//ref//可通过Ref或......
  • vue3新增特性合集
    封装公共方法到原型上再vue2中全局挂载方法使用的是Vue.prototype.xxx的形式来挂载的,但是在vue3中这个方法将不能使用,取而代之的是app.config.globalProperties......
  • vue3 中好用的插件
    1.Api自动导入unplugin-auto-import自动引入compositionapi,不需要再手动引入。(npm地址)下载npmi-Dunplugin-auto-import配置vite.config.tsimportAutoIm......
  • vue3.0 同一项目中调用多个域名的请求
    1.简单粗暴形式:复制多个request.js文件,设置不同的baseUrl,根据需要引用不同的request.js文件。可以解决问题,但不推荐使用2.参数配置形式:利用参数配置,可灵活的调用多个不......
  • vue3 手写dropdown
    <template><divclass="drp_component":class="classname"><p@click="openDrp"class="ws-n"ref="drpDef"title=""><slot></slot></p></div......
  • 面试官:vue2和vue3的区别有哪些?
    一、Vue3与Vue2区别详述1.生命周期对于生命周期来说,整体上变化不大,只是大部分生命周期钩子名称上+“on”,功能上是类似的。不过有一点需要注意,Vue3在组合式API(Comp......
  • vue3 + ts 封装树形控件
    vue3+ts封装树形控件父组件调用 <TreeFilter label="name" title="部门列表(单选)" :requestApi="getUserDepartment" :defaultValue="treeFilterValue.depar......
  • vue3中使用pinia
    包管理器安装yarnaddpinia#或者使用npmnpminstallpinia在目录下创建store文件夹,并创建index.js文件import{createPinia}from'pinia'constpinia=cre......