vue Render scopedSlots

render 中 slot 的一般默认使用方式如下: this.$slots.default 对用 template的<slot>的使用没有name 。 想使用多个slot 的话。需要对slot命名唯一。

在render函数中动态使用多个slot,并且给slot传值

一、我的业务逻辑:

      使用了三个组件,

       组件A调用组件B,组件B调用组件C,组件C是自己封装的render渲染组件。

       组件A希望将自己自定义的插槽插到C组件,C组件渲染出自定义的内容,并且将C组件的值传递给B组件和A组件,B组件是对C组件进行更大一层的封装

     A组件调用B组件

<index-grid>
          <div
            slot="name"
            slot-scope="field"
            class="check-link"
            @click="rowLinkClick"
          >
            <span>{{ field.field.rowData.name }}</span>
          </div>
</index-grid>

A组件引用B组件,slot-scope接收从B组件中传出来solt的值,slot=“name”,是为插槽具名;

B组件中调用C组件的render函数

<sub-grid ref="indexGridSub">
      <span
        v-for="(item, index) in fields"
        :key="index"
        slot="name"
        slot-scope="field"
      >
        <slot name="name" :field="field"></slot>
      </span>

    </sub-grid>

B组件span中 slot是动态的值,和A组件中的slot同一个值,才能接受来自A组件自定义的插槽,

field是来自于C组件中传递的值

C组件是render函数

h(
          "td",
          {
            style: { width: field.width + "px" },
            class: { borderRight },
            // 作用域插槽格式
            // { name: props => VNode | Array<VNode> }
            scopedSlots: this.$scopedSlots.name,
            // 如果组件是其他组件的子组件,需为插槽指定名称
            slot: 'name'
          },
          this.$scopedSlots.name({
            field: field,
            rowData: rowData,
          })
        );

C组件往上传递的值就是{field:'', rowData: ''}的对象

欢迎关注我的个人技术公众号!javascript艺术

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Antd 表格组件的 `customRender` 和 `scopedSlots` 是用于自定义表格单元格内容的两种不同方式。 `customRender` 是一个函数,用于自定义表格单元格的渲染内容。你可以在函数中根据传入的参数来返回自定义的渲染结果。 `scopedSlots` 是一个插槽,用于自定义表格单元格的内容。你可以在插槽中编写自定义的 HTML 或组件,并将其作为表格单元格的内容。 当 `customRender` 和 `scopedSlots` 同时存在时,Antd 表格组件会优先使用 `customRender` 来渲染单元格内容。这意味着,如果你同时使用了 `customRender` 和 `scopedSlots`,`scopedSlots` 中的插槽将会失效。 如果你想要使用 `scopedSlots` 来自定义表格单元格的内容,可以将 `customRender` 的值设为 `undefined` 或 null,这样 Antd 表格组件就会使用 `scopedSlots` 来渲染单元格内容。 以下是一个示例代码: ```vue <template> <a-table :columns="columns" :data-source="data"> <template v-slot:title> Custom Table </template> <template v-slot:name="{ text }"> <span style="color: red">{{ text }}</span> </template> </a-table> </template> <script> export default { data() { return { columns: [ { title: 'Name', dataIndex: 'name', customRender: undefined, // or null scopedSlots: { customRender: 'name' } }, { title: 'Age', dataIndex: 'age' }, { title: 'Address', dataIndex: 'address' } ], data: [ { name: 'John Doe', age: 30, address: 'New York' }, // ... ] }; } }; </script> ``` 在上面的示例中,我们将 `customRender` 的值设为 `undefined`,并在 `scopedSlots` 中使用 `name` 插槽来自定义 `Name` 列的内容。这样,`scopedSlots` 中的插槽就会生效,并且 `Name` 列的单元格内容会渲染为红色字体。 希望以上解答能对你有帮助!如有任何疑问,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值