美文网首页前端架构Web前端之路让前端飞
vue使用slot分发内容与react使用prop分发内容

vue使用slot分发内容与react使用prop分发内容

作者: 前端精髓 | 来源:发表于2019-08-16 16:46 被阅读1次

vue

<slot> 元素作为承载分发内容的出口

// layout.vue
<div class="container">
  <main>
    <slot></slot>
  </main>
</div>

当组件渲染的时候,<slot></slot> 将会被替换该组件起始标签和结束标签之间的任何内容

// hone.vue
<div class="container">
  <layout>
    <p>A paragraph for the main content.</p>
    <p>And another one.</p>
 </layout>
</div>

react

每个组件都可以获取到 props.children。它包含组件的开始标签和结束标签之间的内容。

class Layout extends Component {
  render() {
    return (
      <div className="container">
        <main>
          {this.props.children}
        </main>
      </div>
    );
  }
}

props 是 React 组件的输入。它们是从父组件向下传递给子组件的数据。

function Home (params) {
  return (
    <>
      <Layout>
        <p>A paragraph for the main content.</p>
        <p>And another one.</p>
      </Layout>
    </>
  )
}

vue

有时我们需要多个插槽。对于这样的情况,<slot> 元素有一个特殊的特性:name。这个特性可以用来定义额外的插槽:

<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 的参数的形式提供其名称:

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

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

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

现在 <template> 元素中的所有内容都将会被传入相应的插槽。任何没有被包裹在带有 v-slot<template> 中的内容都会被视为默认插槽的内容。

react

注意:组件可以接受任意 props,包括基本数据类型,React 元素以及函数。

class Layout extends Component {
  render() {
    return (
      <div className="container">
        <header>
          {this.props.header}
        </header>
        <main>
          {this.props.children}
        </main>
        <footer>
          {this.props.footer}
        </footer>
      </div>
    );
  }
}

少数情况下,你可能需要在一个组件中预留出几个“洞”。这种情况下,我们可以不使用 children,而是自行约定:将所需内容传入 props,并使用相应的 prop

function Home (params) {
  return (
    <>
      <Layout
        header={
          <h1>Here might be a page title</h1>
        }
        footer={
          <p>Here's some contact info</p>
        } >
        <p>A paragraph for the main content.</p>
        <p>And another one.</p>
      </Layout>
    </>
  )
}

React 元素本质就是对象(object),所以你可以把它们当作 props,像其他数据一样传递。这种方法类似Vue“槽”(slot)的概念,但在 React 中没有“槽”这一概念的限制,你可以将任何东西作为 props 进行传递。

相关文章

  • vue使用slot分发内容与react使用prop分发内容

    vue 将 元素作为承载分发内容的出口 当组件渲染的时候, 将会被替换该组件起始标签和结束标签之间的任何内...

  • 27.Vue slot内容分发-解构

    Vue slot的使用参考:26.Vue slot内容分发 什么是slot分发解构:官方定义:如果一个 JavaS...

  • slot是什么?有什么作用?原理是什么?

    slot又名插槽,是Vue的内容分发机制,组件内部的模板引擎使用slot元素作为承载分发内容的出口。插槽slot是...

  • slot(插槽)

    slot又称插槽,是Vue的内容分发机制,组件内部的模板引擎使用slot元素作为承载分发内容的出口。插槽slot是...

  • Vue中使用Slot分发内容

    使用slot分发内容 使用一种方式混合父组件的内容与子组件自己的模版,这个过程被称为“内容分发”。在子组建中使用特...

  • Vue内容分发的使用

    官方文档对于这一块写的比较不好理解,也有点绕弯子.在这写一点个人的理解,和使用姿势..... 个人对内容分发 的理...

  • 2020-07-23 一次性讲明白vue插槽slot

    vue插槽slot 一、前言 vue官方文档中在"组件基础"内容中提到组件可以通过插槽分发内容,那插槽是怎么使用的...

  • vue插槽slot

    vue插槽slot 一、前言 vue官方文档中在"组件基础"内容中提到组件可以通过插槽分发内容,那插槽是怎么使用的...

  • 插槽

    一、前言 vue中可以使用插槽slot分发内容给子组件,slot可以避免html被转义(不需要使用v-html)。...

  • 使用slot(插槽)分发内容

    什么是slot(插槽) 混合父组件的内容与自己的模板将父组件的内容与子组件的内容像融合,从而弥补视图的不足 单个插...

网友评论

    本文标题:vue使用slot分发内容与react使用prop分发内容

    本文链接:https://www.haomeiwen.com/subject/mmeqsctx.html