vue-插槽作用域实用场景

vue-插槽作用域实用场景

  • 1.插槽
    • 1.1 自定义列表渲染
    • 1.2 数据表格组件
    • 1.3 树形组件
    • 1.4 表单验证组件
    • 1.5 无限滚动组件

1.插槽

插槽感觉知道有这个东西,但是挺少用过的,每次看到基本都会再去看一遍用法和概念。但是在项目里,自己还是没有用到过。总结下一些可能用到的场景,下次直接这样写了。

1.1 自定义列表渲染

<script setup>
import HelloWorld from './components/HelloWorld.vue'
import { reactive } from 'vue';
const myItems = reactive(
  [
  {
    name: 'A',
    age: 18
  },
  {
    name: 'B',
    age: 19
  },
  {
    name: 'C',
    age: 20
  }
]
)
</script>
<template>
  <HelloWorld :items="myItems">
    <template v-slot:default="slotProps">
    <span>{{ slotProps.item.name }}</span>
    <button @click="doSomething(slotProps.item)">操作</button>
  </template>
  </HelloWorld>
</template>

<script setup>
import { defineProps } from 'vue';
 defineProps({
  items: {
    type: Array,
    default: () => []
  }
})
</script>
<template>
  <ul>
    <li v-for="item in items" :key="item.id">
      <slot :item="item"></slot>
    </li>
  </ul>
</template>

效果:
在这里插入图片描述
拓展性很强。

1.2 数据表格组件

<script setup>
import HelloWorld from './components/HelloWorld.vue'
import { reactive } from 'vue';
const tableData = reactive(
  [
    {
      name: 'A',
      age: 18
    },
    {
      name: 'B',
      age: 19
    },
    {
      name: 'C',
      age: 20
    }
  ]
)

const columns = reactive(
  [
    {
      label: '姓名',
      key: 'name'
    },
    {
      label: '年龄',
      key: 'age'
    },
    {
      label: '性别',
      key: 'sex'
    },
    { key: 'actions', label: '操作' }
  ]
)
</script>
<template>
  <HelloWorld :columns="columns" :data="tableData">
    <template v-slot:actions="{ row }">
      <button @click="edit(row)">编辑</button>
      <button @click="del(row)">删除</button>
    </template>
  </HelloWorld>
</template>

<template>
  <table>
    <thead>
      <tr>
        <th v-for="column in columns" :key="column.key">
          {{ column.label }}
        </th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="row in data" :key="row.id">
        <td v-for="column in columns" :key="column.key">
          <slot :name="column.key" :row="row">
            {{ row[column.key] }}
          </slot>
        </td>
      </tr>
    </tbody>
  </table>
</template>

<script setup>
import { defineProps } from 'vue';
defineProps({
  columns: Array,
  data: Array,
});
</script>

效果:
在这里插入图片描述

1.3 树形组件

<template>
  <div class="tree-node">
    <slot :node="node" :toggle="toggle" :expandTree="expandTree" name="trangle">
      <span v-if="node.children.length > 0" @click="toggle">{{ expanded ? '▼' : '▶' }}</span>
    </slot>
    <slot :node="node" :toggle="toggle" :expandTree="expandTree">
      <div>
        {{ node.label }}
      </div>
    </slot>
    <div v-if="expanded" class="tree-node-children">
      <HelloWorld v-for="child in node.children" :key="child.id" :node="child">
        <template v-slot="childSlotProps">
          <slot v-bind="childSlotProps"></slot>
        </template>
      </HelloWorld>
    </div>
  </div>
</template>
<script setup>
import HelloWorld from './HelloWorld.vue'
import { defineProps, ref } from 'vue';
const props = defineProps({
  node: {
    type: Object,
    required: true
  }
})
const expanded = ref(false)
const toggle = () => {
  console.log(999)
  expanded.value = !expanded.value
}
const expandTree = () => {
  expanded.value = true
}
</script>
<style>
.tree-node {
  position: relative;
  cursor: pointer;
}

.tree-node-children {
  position: relative;
  padding-left: 20px;
  /* 这个值决定了每一层的缩进量 */
}

.tree-node>span {
  margin-left: 0;
}
</style>

<script setup>
import HelloWorld from './components/HelloWorld.vue'
import { reactive } from 'vue';

const rootNode = reactive({
  id: 1,
  label: '根节点',
  children: [
    {
      id: 2, label: '子节点1', children: [
        { id: 3, label: '子节点1-1', children: [] }
      ]
    },
    { id: 4, label: '子节点2', children: [] }
  ]
})

const addChild = (node, expandTree) => {
  const newId = Date.now()
  expandTree()
  node.children.push({
    id: newId,
    label: `新子节点${newId}`,
    children: []
  })
}
</script>
<template>
  <HelloWorld :node="rootNode">
    <template v-slot="{ node, toggle, expandTree }">
      <span @click="toggle">{{ node.label }}</span>
      <button @click="addChild(node, expandTree)">添加子节点</button>
    </template>
  </HelloWorld>
</template>

效果:
在这里插入图片描述

1.4 表单验证组件

<script setup>
import { ref, watch, defineProps, defineEmits } from 'vue'

const props = defineProps({
  rules: {
    type: Array,
    default: () => []
  },
  modelValue: {
    type: String,
    default: ''
  }
})

const emit = defineEmits(['update:modelValue'])

const value = ref(props.modelValue)
const error = ref('')

const validate = () => {
  for (const rule of props.rules) {
    if (!rule.validate(value.value)) {
      error.value = rule.message
      return false
    }
  }
  error.value = ''
  return true
}

watch(() => props.modelValue, (newValue) => {
  value.value = newValue
})

watch(value, (newValue) => {
  emit('update:modelValue', newValue)
})
</script>

<template>
  <div class="a">
    <slot :value="value" :error="error" :validate="validate"></slot>
    <span v-if="error">{{ error }}</span>
  </div>
</template>
<style lang="scss" scoped>
.a{
  position: relative;
  span{
    color: red;
    position: absolute;
    left: 0;
    bottom: -24px;
    font-size: 14px;
  }
}</style>

//使用
<script setup>
import FormField from './components/HelloWorld.vue'
import { ref } from 'vue';


const email = ref('')

const emailRules = [
  {
    validate: (value) => /.+@.+\..+/.test(value),
    message: '请输入有效的电子邮件地址'
  }
]
</script>
<template>
  <FormField :rules="emailRules" v-model="email">
    <template v-slot="{ value, error, validate }">
      邮箱
      <input :value="value" @input="$event => { email = $event.target.value; validate(); }"
        :class="{ 'is-invalid': error }" />
    </template>
  </FormField>
</template>

<style >
input{
  outline: none;
}
.is-invalid:focus {
  border-color: red;
}
</style>

效果:
在这里插入图片描述

1.5 无限滚动组件


<script setup>
import { ref, onMounted, defineProps } from 'vue'

const props = defineProps({
  fetchItems: {
    type: Function,
    required: true
  }
})

const visibleItems = ref([])
const loading = ref(false)

const handleScroll = async (event) => {
  const { scrollTop, scrollHeight, clientHeight } = event.target
  if (scrollTop + clientHeight >= scrollHeight - 20 && !loading.value) {
    loading.value = true
    const newItems = await props.fetchItems()
    visibleItems.value = [...visibleItems.value, ...newItems]
    loading.value = false
  }
}

onMounted(async () => {
  visibleItems.value = await props.fetchItems()
})
</script>

<template>
  <div @scroll="handleScroll" style="height: 200px; overflow-y: auto;">
    <slot :items="visibleItems"></slot>
    <div v-if="loading">加载中...</div>
  </div>
</template>

//使用
<script setup>
import InfiniteScroll from './components/HelloWorld.vue'
import { ref } from 'vue';



let page = 0
const fetchMoreItems = async () => {
  // 模拟API调用
  await new Promise(resolve => setTimeout(resolve, 1000))
  page++
  return Array.from({ length: 10 }, (_, i) => ({
    id: page * 10 + i,
    content: `Item ${page * 10 + i}`
  }))
}
</script>
<template>
  <InfiniteScroll :fetch-items="fetchMoreItems">
    <template #default="{ items }">
      <div v-for="item in items" :key="item.id">
        {{ item.content }}
      </div>
    </template>
  </InfiniteScroll>
</template>


本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/889712.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

基于SpringBoot+Vue的疫情物资管理系统(带1w+文档)

基于SpringBootVue的疫情物资管理系统(带1w文档) 基于SpringBootVue的疫情物资管理系统(带1w文档) 本课题研究和开发疫情物资管理系统管理系统&#xff0c;让安装在计算机上的该系统变成管理人员的小帮手&#xff0c;提高疫情物资管理系统信息处理速度&#xff0c;规范疫情物资…

C++入门基础知识107—【关于C++continue 语句】

成长路上不孤单&#x1f60a;&#x1f60a;&#x1f60a;&#x1f60a;&#x1f60a;&#x1f60a; 【14后&#x1f60a;///C爱好者&#x1f60a;///持续分享所学&#x1f60a;///如有需要欢迎收藏转发///&#x1f60a;】 今日分享关于C continue 语句的相关内容&#xff01;…

关于Java部署项目,文件上传路径问题 、Windows是\ linux是/

Windows是\ linux是/ &#xff0c;踩坑。报错如下&#xff1a;

SpringBootWeb快速入门!详解如何创建一个简单的SpringBoot项目?

在现代Web开发中&#xff0c;SpringBoot以其简化的配置和快速的开发效率而受到广大开发者的青睐。本篇文章将带领你从零开始&#xff0c;搭建一个基于SpringBoot的简单Web应用~ 一、前提准备 想要创建一个SpringBoot项目&#xff0c;需要做如下准备&#xff1a; idea集成开发…

信息安全工程师(28)机房安全分析与防护

前言 机房安全分析与防护是一个复杂而细致的过程&#xff0c;涉及到物理安全、环境控制、电力供应、数据安全、设备管理、人员管理以及紧急预案等多个方面。 一、机房安全分析 1. 物理安全威胁 非法入侵&#xff1a;未经授权的人员可能通过门窗、通风口等进入机房&#xff0c;…

《大规模语言模型从理论到实践》第一轮学习笔记

第一章 绪论 本章主要介绍大规模语言模型基本概念、发展历程和构建流程。 大规模语言模型&#xff08;Large Language Models&#xff0c;LLM&#xff09;&#xff0c;也称大语言模型 或大型语言模型。 1.1 大规模语言模型基本概念 1.语言模型&#xff08;Language Model&a…

LeetCode 3310. 移除可疑的方法

LeetCode 3310. 移除可疑的方法 你正在维护一个项目&#xff0c;该项目有 n 个方法&#xff0c;编号从 0 到 n - 1。 给你两个整数 n 和 k&#xff0c;以及一个二维整数数组 invocations&#xff0c;其中 invocations[i] [ai, bi] 表示方法 ai 调用了方法 bi。 已知如果方法 k…

Leetcode 37. 解数独

1.题目基本信息 1.1.题目描述 编写一个程序&#xff0c;通过填充空格来解决数独问题。 数独的解法需 遵循如下规则&#xff1a; 数字 1-9 在每一行只能出现一次。数字 1-9 在每一列只能出现一次。数字 1-9 在每一个以粗实线分隔的 33 宫内只能出现一次。&#xff08;请参考…

文件IO及目录操作

一、文件IO 1.1 close函数&#xff08;关闭文件&#xff09; #include <unistd.h>---所需头文件 int close(int fd); 功能&#xff1a;关闭文件 参数&#xff1a;fd&#xff1a;文件描述符 返回值&#xff1a;成功返回0&#xff0c;失败返回-1&#xff0c;置位错误码 …

主机加固的关键要素:服务器防病毒

在数字化浪潮中&#xff0c;网络安全已成为企业不可忽视的一环。尤其是安全运维人员&#xff0c;他们肩负着保护企业数据不受侵害的重任。MCK主机加固解决方案&#xff0c;正是为了应对这一挑战而生。 网络安全的严峻现实 不久前&#xff0c;一家知名企业因勒索病毒攻击而被迫…

二分查找一>0~n-1中缺失的数字(点名)

1.题目&#xff1a; 2.解析&#xff1a;方法一&#xff1a;用哈希表&#xff1a;记录存在的数字&#xff0c;找到哈希表为空的数字输出 Set<Integer> set new HashSet<>();for(int x : records) set.add(x);for(int i 0; i < set.size(); i){if(!set.contain…

Linux系列-Linux的常见指令

&#x1f308;个人主页&#xff1a;羽晨同学 &#x1f4ab;个人格言:“成为自己未来的主人~” Linux基本指令 ls指令 语法&#xff1a;ls 【选项】【目录或文件】 功能&#xff1a;对于目录&#xff0c;该命令列出该目录下的所有子目录与文件&#xff0c;对于文件&#xf…

【GO基础学习】环境安装到基础语法(1)

文章目录 环境安装GoLand 安装GO基础GO特点类型和函数Init函数和main函数GO命令下划线变量和常量数组切片Slice 引用 环境安装 下载地址&#xff1a;https://www.golangroadmap.com/ 安装目录文件说明&#xff1a; api&#xff1a;每个版本的 api 变更差异。 bin&#xff1…

基于SpringBoot+Vue的船舶监造系统(带1w+文档)

基于SpringBootVue的船舶监造系统(带1w文档) 基于SpringBootVue的船舶监造系统(带1w文档) 大概在20世纪90年代&#xff0c;我国才开始研发船舶监造系统&#xff0c;与一些发达国家相比&#xff0c;系统研发起步比较晚。当时的计算机技术刚开始发展起来&#xff0c;国家经济力量…

Map的实现类:HashMap

在API获取HsahMap类的全部信息 实例代码&#xff1a;创建一个Student类和Demo02 package com.map;public class Student {private String name;private int stuNo;public Student(String name, int stuNo) {this.name name;this.stuNo stuNo;}public String getName() {retu…

从零开始构建:Python自定义脚本自动化你的日常任务

从零开始构建&#xff1a;Python自定义脚本自动化你的日常任务 Python 作为一种简洁且功能强大的编程语言&#xff0c;被广泛应用于各种自动化任务中。通过编写 Python 脚本&#xff0c;你可以轻松地将日常重复性工作自动化&#xff0c;例如文件操作、数据处理、网络爬虫、系统…

C++ | Leetcode C++题解之第457题环形数组是否存在循环

题目&#xff1a; 题解&#xff1a; class Solution { public:bool circularArrayLoop(vector<int>& nums) {int n nums.size();auto next [&](int cur) {return ((cur nums[cur]) % n n) % n; // 保证返回值在 [0,n) 中};for (int i 0; i < n; i) {if …

STM32 407 RS485通信实现数据收发【我的创作纪念日】

1. 前言 本例中的485驱动&#xff0c;基于标准库编写&#xff0c;不是HAL库&#xff0c;请大家注意。 最近搞嵌入式程序&#xff0c;踩了不少坑&#xff0c;这里统一记录一下。 2. 收获 1.串口通信&#xff0c;数据是一个字节一个字节的发送&#xff0c;对方收到的数据是放在…

github学生认证(Github Copilot)

今天想配置一下Github Copilot&#xff0c;认证学生可以免费使用一年&#xff0c;认证过程中因为各种原因折腾了好久&#xff0c;记录一下解决方法供大家参考。 p.s.本文章只针对Github学生认证部分遇到的问题及解决方法&#xff0c;不包括配置copilot的全部流程~ 1、准备工作…

无图化加速!MemFusionMap提出时序重叠热图策略,在线建图mAP暴涨5.4%!

导读&#xff1a; HDMap对于自动驾驶系统至关重要&#xff0c;因为它可以为规划提供了精细的道路信息。尽管现有的单帧输入方法在在线矢量化高精地图构建方面取得了不错的成绩&#xff0c;但在处理复杂场景和遮挡时仍然存在挑战。为了解决这些问题&#xff0c;作者提出了 MemFu…