Declaring templates and elements inside of templates works great for most scenarios. Sometimes you need a bit more control over what your component will do with the props and children it receives, so Vue provides a render function that allows you complete programmatic control over every argument using JSX.

We have a parent component which renders:

        <Content slot="content" :limit="4">
<div><img src="https://robohash.org/mindy?set=set4" alt=""></div>
<div><img src="https://robohash.org/john?set=set4" alt=""></div>
<div><img src="https://robohash.org/kim?set=set4" alt=""></div>
<div><img src="https://robohash.org/joel?set=set4" alt=""></div>
<div><img src="https://robohash.org/maggie?set=set4" alt=""></div>
</Content>

Content.vue:

<script>
import { shuffle } from "lodash"
export default {
functional: true,
render: (createElement, { children, props, data }) => {
if(props.limit) {
return createElement('div', data, shuffle(children.slice(0, props.limit)));
} else {
return createElement('div', data, shuffle(children));
}
}
}
</script>

You can also using JSX:

[Vue @Component] Control Template Contents with Vue's Render Function的更多相关文章

  1. [Vue @Component] Define Props on a Vue Class with vue-property-decorator

    While traditional Vue components require a data function which returns an object and a method object ...

  2. Vue.js—组件快速入门及Vue路由实例应用

    上次我们学习了Vue.js的基础,并且通过综合的小实例进一步的熟悉了Vue.js的基础应用.今天我们就继续讲讲Vue.js的组件,更加深入的了解Vue,js的使用.首先我们先了解一下什么是Vue.js ...

  3. vue插槽用法(极客时间Vue视频笔记)

    vue插槽 插槽是用来传递复杂的内容,类似方法 <!DOCTYPE html> <html lang="en"> <head> <meta ...

  4. 聊聊Vue.js的template编译

    写在前面 因为对Vue.js很感兴趣,而且平时工作的技术栈也是Vue.js,这几个月花了些时间研究学习了一下Vue.js源码,并做了总结与输出. 文章的原地址:https://github.com/a ...

  5. Vue中使用Vue.component定义两个全局组件,用单标签应用组件时,只显示一个组件的问题和 $emit的使用。

    解决方法: 定义了两个 Vue.component 在 el 中使用的时候要用 双标签, 用单表标签的时候,只会显示第个 组件间 这样写只显示 welcome-button 组件 <welcom ...

  6. Vue学习笔记 template methods,filters,ChromeDriver,安装sass

    ChromeDriver installation failed Error with http(s) request: Error: connect ETIMEDOUT 172.217.160.80 ...

  7. 使用 Vue.component

    引入 vue.js. HTML <div id="app"></div> CSS .greeting { padding: 3rem 1.5rem; bac ...

  8. vue.extend和vue.component的区别

    vue.extend 使用基础 Vue 构造器函数,通过原型继承,(返回)创建一个"子类"(构造器).参数是一个包含组件选项的对象. const Sub = function Vu ...

  9. Vue Component Registration All In One

    Vue Component Registration All In One Vue 注册自定义组件 <template> <div class="back-to-top-c ...

随机推荐

  1. Windows 2008中部署dll到GAC

    两种方法: 1  gacutil.exe 2 直接拖动DLL到GAC (此种方式要关闭UAC,否则提示"Access is Denied")

  2. java用匿名内部类实现多线程堆内存变量共享

    匿名内部类介绍:http://www.cnblogs.com/nerxious/archive/2013/01/25/2876489.html 用Runnable模拟实现共享堆内存变量 import ...

  3. centos服务器/dev/xvda1空间占满的解决方法

    突然线上Centos的机器磁盘空间占满报警,第一反映是日志文件很大,占用了较多的磁盘空间.于是简单的上去看了一下.但是发现线上不是的地址对应的空间占的并不多.用:df -h 命令看了一下,/dev/x ...

  4. LN : leetcode 538 Convert BST to Greater Tree

    lc 538 Convert BST to Greater Tree 538 Convert BST to Greater Tree Given a Binary Search Tree (BST), ...

  5. (求助)对某一颜色,设置透明度 alpha 后,其他使用该颜色的地方 受到影响!!!!原因未知

    对某一颜色,设置透明度 alpha 后,其他使用该颜色的地方 受到影响!!!!原因未知,有谁碰到过这样的问题?????? 测试了以下三款手机,结果如下: 1.android 4.4.2: 不受影响 2 ...

  6. MySQL性能优化必备25条

    1. 为查询缓存优化你的查询 大多数的MySQL服务器都开启了查询缓存.这是提高性最有效的方法之一,而且这是被MySQL的数据库引擎处理的.当有很多相同的查询被执行了多次的时候,这些查询结果会被放到一 ...

  7. [Windows Server 2003] 初识Windows Server 2003

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com ★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频. ★ 本节我们将带领大家:初次见识W ...

  8. Django基础核心技术之Model模型的介绍与设计

    Django基础核心技术之Model模型的介绍与设计原创: Yunbo Shi Python Web与Django开发 2018-05-03Django网络应用开发的5项基础核心技术包括模型(Mode ...

  9. 【转载】appium自动化环境搭建

    1.java开发环境JDK 2.android SDK(platform/platform tools/tools/build tools) 3.python下载安装(pip) 4.appium下载安 ...

  10. Ubuntu安装Foxit PDF阅读器

    最近使用Ubuntu自带的PDF阅读器,发现使用体验较差,打算安装FoxitReader(可能是我习惯了Foxit和Adobe) Foxit官网 对系统平台要求如下:(支持Linux) 继续摸索了一下 ...