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. RS485通信和Modbus协议(转)

    转自:http://www.51hei.com/bbs/dpj-23230-1.html 在工业控制.电力通讯.智能仪表等领域,通常情况下是采用串口通信的方式进行数据交换.最初采用的方式是RS232接 ...

  2. mybatis使用中类属性名和数据库表字段名问题

    起初我以为上述二者必须一致,后来发现也是可以像Hibernate那样在xml文件中进行映射的. <mapper namespace="com.tenghu.mybatis.model. ...

  3. Android开发——Snackbar使用详解

    http://blog.csdn.net/qq_19431333/article/details/52862348

  4. HDU_2544_最短路

    题意:第一个路口是起点,第n个(最后一个)路口是终点,问最短路径. 总结:第一个dijkstra. 代码: #include<iostream> #include<cstdio> ...

  5. win10系统杀毒功能

    最近很多客户的电脑都是win10的,部署web服务时用到的破解软件(exe文件),经常被当成病毒杀死,今天发现可以在杀毒记录里找到刚刚被杀掉的文件,然后点击操作,点击允许,再运行时就可以畅通无阻了

  6. SpringBoot开源项目学习总结

    一.实现日期格式数据类型的转换 首先,定义DateConverter实现Converter<String, Date>接口: package com.stevlu.common; impo ...

  7. 17Web应用乱码问题

    Web应用乱码问题 Web应用乱码问题 简介 每个国家(或区域)都规定了本国家(或地区)计算机信息交换用的字符编码集,如美国的扩展ASCII码, 中国的GB2312-80,日本的JIS 等,作为该国家 ...

  8. A6. JVM 垃圾回收算法(GC 算法)

    [概述] 常见的垃圾回收算法有:标记-清除算法.复制算法.标记-整理算法.分代收集算法. [标记-清除算法] 标记-清除算法是最基础的收集算法,如同它的名字一样,算法分为 “标记” 和 “清除” 两个 ...

  9. [angular1.6]Error: "transition superseded" ui-router 在angular1.6 报错误问题解决

    在angular1.6版本里,使用ui-router如果报这个错误,可以将ui-router升级到最近版本即可.ui-router version v0.4.2

  10. new实现

    前言 本篇来分析new是怎么实现的, 使用c++进行在申请对象的时候用到new, 但是为什么申请对象要用到new, 而不能用malloc, 而有时申请数组的用new或者malloc似乎又都可以, 这里 ...