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

640?wx_fmt=png

vue

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

// layout.vue
&lt;div class="container"&gt;
  &lt;main&gt;
    &lt;slot&gt;&lt;/slot&gt;
  &lt;/main&gt;
&lt;/div&gt;

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

// hone.vue
&lt;div class="container"&gt;
  &lt;layout&gt;
    &lt;p&gt;A paragraph for the main content.&lt;/p&gt;
    &lt;p&gt;And another one.&lt;/p&gt;
 &lt;/layout&gt;
&lt;/div&gt;

react

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

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

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

function Home (params) {
  return (
    &lt;&gt;
      &lt;Layout&gt;
        &lt;p&gt;A paragraph for the main content.&lt;/p&gt;
        &lt;p&gt;And another one.&lt;/p&gt;
      &lt;/Layout&gt;
    &lt;/&gt;
  )
}

vue

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

&lt;div class="container"&gt;
  &lt;header&gt;
    &lt;slot name="header"&gt;&lt;/slot&gt;
  &lt;/header&gt;
  &lt;main&gt;
    &lt;slot&gt;&lt;/slot&gt;
  &lt;/main&gt;
  &lt;footer&gt;
    &lt;slot name="footer"&gt;&lt;/slot&gt;
  &lt;/footer&gt;
&lt;/div&gt;

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

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

&lt;layout&gt;
  &lt;template v-slot:header&gt;
    &lt;h1&gt;Here might be a page title&lt;/h1&gt;
  &lt;/template&gt;

  &lt;p&gt;A paragraph for the main content.&lt;/p&gt;
  &lt;p&gt;And another one.&lt;/p&gt;

  &lt;template v-slot:footer&gt;
    &lt;p&gt;Here's some contact info&lt;/p&gt;
  &lt;/template&gt;
&lt;/layout&gt;

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

react

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

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

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

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

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

同样是组合代码,Vue和React发挥得淋漓尽致,你更喜欢哪种方式呢?欢迎在评论区留言,说说你们的看法!

640?wx_fmt=png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值