react 插槽

问题开发当中会经常出现组件十分相似的组件,只有一部分是不同的

解决: 父组件:在引用的时候

html 复制代码
import { Component } from "react";
import Me from "../me";

const name = <div>名称</div>
class  Shoop extends Component {
   

    render(){
         return <div style={{display: "flex"}}>
                {name}
              <Me>
                 <div slot="img">
                  <img src="https://tse2-mm.cn.bing.net/th/id/OIP-C.GwJCNqHxfwmnsMUpKG6tmAHaHa?w=208&h=207&c=7&r=0&o=5&pid=1.7"></img>
                 </div>
                 <div slot="voide">
                 视频
                 </div>
              </Me>
              <Me>
              <div slot="voide">
                 视频
                 </div>
              </Me>
              <Me>
                 <div></div>
              </Me>
         </div>
    }
}
export default Shoop

子组件

复制代码
 const Me = (props)=> {
   const newChildren =  {}
  
  if(props.children  && props.children.length > 1) {
    console.log(props.children,"props.childrenprops.childrenprops.children")
      props.children.map((Item) => {
      newChildren[Item.props.slot] = {}
      newChildren[Item.props.slot]["children"] =   Item.props.children
      
    })
  }
  return (<div style={{height:"100px",width:"200px",background:"red"}}>
           <div>名称</div>
           <div> 图片自定义区域  {newChildren?.img  ? newChildren?.img?.children : ""} </div>
           <div> {newChildren?.voide  ? newChildren?.voide?.children : ""}</div>
           
  </div>)
}
export default Me

父组件设置 solt 其实设置什么都可以,只是vue 通常以solt 为定义出于习惯

当父组件设置过数据以后,自组件可以通过props.childen 拿到想要的数据进行处理

这里我没有写单插槽定义

如果写多个插槽的时候要考虑单个的问题,因为单个插槽传递过来是对象形式的

相关推荐
XiaoYu20026 分钟前
第1章 核心竞争力和职业规划
前端·面试·程序员
excel11 分钟前
🧩 深入浅出讲解:analyzeScriptBindings —— Vue 如何分析 <script> 里的变量绑定
前端
蓝瑟14 分钟前
AI时代程序员如何高效提问与开发工作?
前端·ai编程
林晓lx44 分钟前
使用Git钩子+ husky + lint语法检查提高前端项目代码质量
前端·git·gitlab·源代码管理
王同学要变强1 小时前
【深入学习Vue丨第二篇】构建动态Web应用的基础
前端·vue.js·学习
社恐的下水道蟑螂1 小时前
从字符串到像素:深度解析 HTML/CSS/JS 的页面渲染全过程
javascript·css·html
程序定小飞1 小时前
基于springboot的web的音乐网站开发与设计
java·前端·数据库·vue.js·spring boot·后端·spring
Hello_WOAIAI1 小时前
2.4 python装饰器在 Web 框架和测试中的实战应用
开发语言·前端·python
FinClip2 小时前
凡泰极客亮相香港金融科技周,AI助力全球企业构建超级应用
前端
阿四2 小时前
【Nextjs】为什么server action中在try/catch内写redirect操作会跳转失败?
前端·next.js