常常看到使用render
渲染函数的示例,并且在一些特殊状况下,确实更好使用,能够更加有效地细分组件,于是借助vue-element-admin
来学习一波html
针对 Link.vue进行改造vue
Link.vuegit
// https://github.com/vuejs/eslint-plugin-vue/issues/462 <template> <!-- eslint-disable vue/require-component-is --> <component v-bind="linkProps(to)"> <slot /> </component> </template> <script> import { isExternal } from '@/utils/validate' export default { props: { to: { type: String, required: true } }, methods: { linkProps(url) { if (isExternal(url)) { return { is: 'a', href: url, target: '_blank', rel: 'noopener' } } return { is: 'router-link', to: url } } } } </script>
上述方式打开了一个新的使用方式,这样的好处,不须要使用if/else
进行处理,还能够减小一个多余的标签【root element】。可是会有一些语法提示错误,虽然添加了eslint-disable
来禁止,但仍是不行,并且有些不似vue
的用法github
<script> import { isExternal } from '@/utils/validate' export default { name: 'Link', props: { to: { type: String, required: true } }, render(h) { if (isExternal(this.to)) { return h( 'a', { attrs: { target: '_blank', rel: 'noopener', href: this.to } }, this.$slots.default ) } else { return h('router-link', { props: { to: this.to } }, this.$slots.default ) } } } </script>
主要是slot
如何处置比较好,其余都好处理,并且使用slot
有两种方式 插槽ide
方式一函数
this.$slots.default
方式二oop
this.$scopedSlots.default()
<script> import { isExternal } from '@/utils/validate' export default { name: 'Link', functional: true, props: { to: { type: String, required: true } }, render(h, context) { console.log(context) const { props, scopedSlots } = context const { to } = props if (isExternal(to)) { return h( 'a', { attrs: { target: '_blank', rel: 'noopener', href: to } }, scopedSlots.default() ) } else { return h('router-link', { props: { to: to } }, // scopedSlots.default() context.children ) } } } </script>
对于上述两种实现方式,大体相同,有一些细节须要注意学习
functional: true
添加这个后,只能经过 context
来进行上下文关联,而没法调用this
,同时这种方式会快一些,只是在使用slot
时,会有两种形式link
this.$slots.default
更新为 context.children
scopedSlots.default()
这种方式依旧在functional: true
,文件名即可以改成js
为后缀了,若依旧使用vue
时,便须要<script> export default {}</script>
进行包裹了render
函数更可能是,不少细节不能使用语法糖来进行处理,致使使用起来不顺手slot
插槽这块仍是颇有用的,只是文档说明等没有前面的那么详细了root element
怕是就够处理好一下子了