Vue render与$slot $slotScoped应用

默认插槽

<script>
import { Component, Vue } from 'vue-property-decorator';
@Component()
export default class Page extends Vue {
  render() {
    return (
      <div>
        <p>Uxx智能家居</p>
        <customNode>
          <p>欢迎回家!</p>
        </customNode>
      </div>
    );
  }
}
@Component()
class customNode extends Vue {
  render() {
    return (
      <div>
        <p>尊敬的用户</p>
        <p>{this.$slots.default}</p>
      </div>
    );
  }
}
</script>

请注意,在render里使用插槽的时候,不能用template做包裹(因为template在JSX里面解析会被赋予display:none的属性)

 

具名插槽

<script>
import { Component, Vue } from 'vue-property-decorator';
@Component()
export default class Page extends Vue {
  render() {
    return (
      <div>
        <p>Uxx智能家居</p>
        <customNode>
          <p slot="name">欢迎回家!</p>
        </customNode>
      </div>
    );
  }
}
@Component()
class customNode extends Vue {
  render() {
    return (
      <div>
        <p>尊敬的用户</p>
        <p>{this.$slots.name}</p>
      </div>
    );
  }
}
</script>

作用域插槽

<script>
import { Component, Vue } from 'vue-property-decorator';
@Component()
export default class Page extends Vue {
  render() {
    return (
      <div>
        <p>Uxx智能家居</p>
        <customNode
          {...{ scopedSlots: { name: postData => <p>{postData.name}</p> } }}
        ></customNode>
      </div>
    );
  }
}
@Component()
class customNode extends Vue {
  postData = {
    name: '大傻蛋',
  };
  render() {
    return (
      <div>
        <p>尊敬的用户</p>
        <p>{this.$scopedSlots.name(this.postData)}</p>
      </div>
    );
  }
}
</script>

注意,这里用$scopedSlots来代替作用域插槽($slot表示普通插槽),取消了v-bnd对作用域的绑定,改用传参来传递

scopedSlots属性绑定插槽内容,替代了往customNode里面写插槽内容的写法

 

JSX函数式写法

render函数:

该函数接受三个参数:

render(h) {
'div', // 参数一
{ attrs: { ...this.$attrs, title: this.title } }, // 参数二
[h('h1', '这是标题'), h('p', this.$scopedSlots.content)] // 参数三
}

参数一类型:

① String类型:HTML标签 

render(h) {
    return h('div',....)
}

② Object类型: 一个含有数据选项的对象,可以理解为组件

import customMessage from './customMessage .vue';
...
render(h) {
   return h(customMessage,{..},[...])
}

③ Function类型: 返回一个含有数据选项的对象 简单理解为再套娃一个render函数(?存疑)

render(h) {
   return h(h('div',{...}),{..},[...])
}

参数二类型:

Object类型,接收虚拟DOM上的特有属性。如class、style、attrs、domProps等。

render(h, context) {
    const {listeners,staticClass, className} = context;
     return h(Component, {
      props: {
        field,
        updateFields,
      },
      attrs: {
        placeholder: field.widget.placeholder,
        view: view || field.widget.view,
      },
      on: {
        input: () => {},
        ...listeners,
      },
      style:{color:'red'},
      class: className,
      staticClass,
    });
}

参数三类型:

String|Array:代表子节点

render(h) {
    return this.$createElement(
      this.component,
      { attrs: { ...this.$attrs, title: this.title } },
      [h('h1', '这是标题'), h('p', this.$scopedSlots.content)],
    );
  }
render(h) {
    return this.$createElement(
      this.component,
      { attrs: { ...this.$attrs, title: this.title } },
      this.$scopedSlots.content
    );
  }

用render函数复写上面的例子

默认插槽:

<script>
import { Component, Vue } from 'vue-property-decorator';
@Component()
export default class Page extends Vue {
  render(h) {
    return h('div', [
      h('p', 'Uxx智能家居'),
      h(customNode, [h('p', '欢迎回家!')]),
    ]);
  }
}
@Component()
class customNode extends Vue {
  render(h) {
    return h('div', {}, [
      h('p', '尊敬的用户'),
      h('p', {}, this.$slots.default),
    ]);
  }
}
</script>

具名插槽:

<script>
import { Component, Vue } from 'vue-property-decorator';
@Component()
export default class Page extends Vue {
  render(h) {
    return h('div', [
      h('p', 'Uxx智能家居'),
      h(customNode, [h('p', { slot: 'name' }, '欢迎回家!')]),
    ]);
  }
}
@Component()
class customNode extends Vue {
  render(h) {
    return h('div', {}, [h('p', '尊敬的用户'), h('p', {}, this.$slots.name)]);
  }
}
</script>

 作用域插槽:

<script>
import { Component, Vue } from 'vue-property-decorator';
@Component()
export default class Page extends Vue {
  render(h) {
    return h('div', [
      h('p', 'Uxx智能家居'),
      h(customNode, {
        scopedSlots: {
          name: postData => <p>{postData.name}</p>,
        },
      }),
    ]);
  }
}
@Component()
class customNode extends Vue {
  postData = {
    name: '大傻蛋',
  };
  render(h) {
    return h('div', {}, [
      h('p', '尊敬的用户'),
      h('p', {}, this.$scopedSlots.name(this.postData)),
    ]);
  }
}
</script>

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值