您好,欢迎访问一九零五行业门户网

详解Vue中的无渲染行为插槽

在本文中我们讨论 vue 中的无渲染插槽模式能够帮助解决哪些问题。
在 vue.js 2.3.0 中引入的作用域插槽显著提高了组件的可重用性。无渲染组件模式应运而生,解决了提供可重用行为和可插入表示的问题。
在这里,我们将会看到如何解决相反的问题:怎样提供可重用的外观和可插入的行为。
无渲染组件这种模式适用于实现复杂行为且具有可自定义表示的组件。
它满足以下功能:
该组件实现所有行为作用域的插槽负责渲染后备内容能够确保组件可以直接使用。举个例子:一个执行 ajax 请求并显示结果的插槽的组件。组件处理 ajax 请求并加载状态,而默认插槽提供演示。
这是一个简化版的实现:
<template> <div> <slot v-if="loading" name="loading"> <div>loading ...</div> </slot> <slot v-else v-bind={data}> </slot> </div></template><script>export default { props: ["url"], data: () => ({ loading: true, data: null }), async created() { this.data = await fetch(this.url); this.loading = false; }};</script>
用法:
<lazy-loading url="https://server/api/data"> <template #default="{ data }"> <div>{{ data }}</div> </template></lazy-loading>
有关这种模式的原始文章,请在这里查看。
一个相反的问题如果问题反过来该怎么办:想象一下,如果一个组件的主要特征就是它的表示形式,另外它的行为应是可自定义的。
假设你想要基于 svg 创建一个树组件,如下所示:
你想要提供 svg 的显示和行为,例如在单击时收回节点和突出显示文本。
当你打算不对这些行为进行硬编码,并且让组件的用户自由覆盖它们时,就会出现问题。
暴露这些行为的简单解决方案是向组件添加方法和事件。
你可能会这样去实现:
<script>export default { mounted() { // pseudo code nodes.on('click',(node) => this.$emit('click', node)); }, methods: { expandnode(node) { //... }, retractnode(node) { //... }, highlighttext(node) { //... }, }};</script>
如果组件的使用者要向组件添加行为,需要在父组件中使用 ref,例如:
<template> <tree ref="tree" @click="onclick"></tree></template><script>export default { methods: { onclick(node) { this.$refs.tree.retractnode(node); } }};</script>
这种方法有几个缺点:
无法再提供默认行为行为代码最终会被频繁的复制粘贴行为不可重用让我们看看无渲染插槽如何解决这些问题。
无渲染插槽行为基本上包括证明对事件的反应。所以让我们创建一个插槽,用来接收对事件和组件方法的访问:
<template> <div> <slot name="behavior" :on="on" :actions="actions"> </slot> </div></template><script>export default { methods: { expandnode(node) { }, retractnode(node) { }, //... }, computed:{ actions() { const {expandnode, retractnode} = this; return {expandnode, retractnode}; }, on() { return this.$on.bind(this); } }};</script>
on 属性是父组件的 $on 方法,因此可以监听所有事件。
可以将行为实现为无渲染组件。接下来编写点击扩展组件:
export default { props: ['on','action'] render: () => null, created() { this.on("click", (node) => { this.actions.expandnode(node); }); }};
用法:
<tree> <template #behavior="{ on, actions }"> <expand-on-click v-bind="{ on, actions }"/> </template></tree>
该解决方案的主要优点是:
通过备用内容来提供默认行为的可能性:例如,通过将图形组件声明为:
<template> <div> <slot name="behavior" :on="on" :actions="actions"> <expand-on-click v-bind="{ on, actions }"/> </slot> </div></template>
能够创建可重用的组件,并可以实现使用这个组件的用户能够选择的标准行为考虑一个悬停突出显示组件:
export default { props: ['on','action'] render: () => null, created() { this.on("hover", (node) => { this.actions.highlight(node); }); }};
覆盖标准行为:
<tree> <template #behavior="{ on, actions }"> <highlight-on-hover v-bind="{ on, actions }"/> </template></tree>
行为插槽是可组合的添加两个预定义的行为:
<tree> <template #behavior="{ on, actions }"> <expand-on-click v-bind="{ on, actions }"/> <highlight-on-hover v-bind="{ on, actions }"/> </template></tree>
解决方案的可读性作为行为的组件是能够自描述的。
可扩展性on 属性可以访问所有组件事件。默认情况下,该插槽可使用新事件。
总结无渲染插槽提供了一种有趣的解决方案,可以在组件中公开方法和事件。它们提供了更具可读性和可重用性的代码。
可以在 github 上找到实现此模式的树组件的代码:vue.d3.tree
英文地址原文:https://alligator.io/vuejs/renderless-behavior-slots/
作者:david desmaisons
相关推荐:
2020年前端vue面试题大汇总(附答案)
vue教程推荐:2020最新的5个vue.js视频教程精选
更多编程相关知识,请访问:编程入门!!
以上就是详解vue中的无渲染行为插槽的详细内容。
其它类似信息

推荐信息