[Vue @Component] Control Template Contents with Vue's Render Function
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的更多相关文章
- [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 ...
- Vue.js—组件快速入门及Vue路由实例应用
上次我们学习了Vue.js的基础,并且通过综合的小实例进一步的熟悉了Vue.js的基础应用.今天我们就继续讲讲Vue.js的组件,更加深入的了解Vue,js的使用.首先我们先了解一下什么是Vue.js ...
- vue插槽用法(极客时间Vue视频笔记)
vue插槽 插槽是用来传递复杂的内容,类似方法 <!DOCTYPE html> <html lang="en"> <head> <meta ...
- 聊聊Vue.js的template编译
写在前面 因为对Vue.js很感兴趣,而且平时工作的技术栈也是Vue.js,这几个月花了些时间研究学习了一下Vue.js源码,并做了总结与输出. 文章的原地址:https://github.com/a ...
- Vue中使用Vue.component定义两个全局组件,用单标签应用组件时,只显示一个组件的问题和 $emit的使用。
解决方法: 定义了两个 Vue.component 在 el 中使用的时候要用 双标签, 用单表标签的时候,只会显示第个 组件间 这样写只显示 welcome-button 组件 <welcom ...
- Vue学习笔记 template methods,filters,ChromeDriver,安装sass
ChromeDriver installation failed Error with http(s) request: Error: connect ETIMEDOUT 172.217.160.80 ...
- 使用 Vue.component
引入 vue.js. HTML <div id="app"></div> CSS .greeting { padding: 3rem 1.5rem; bac ...
- vue.extend和vue.component的区别
vue.extend 使用基础 Vue 构造器函数,通过原型继承,(返回)创建一个"子类"(构造器).参数是一个包含组件选项的对象. const Sub = function Vu ...
- Vue Component Registration All In One
Vue Component Registration All In One Vue 注册自定义组件 <template> <div class="back-to-top-c ...
随机推荐
- 【日常总结】scrollTop、scrollHeight与clientHeight的重要关系
前言 在做一个需求的时候涉及懒加载,百度了一下,发现scrollTop.scrollHeight与clientHeight这三个元素起到了重要作用,以前做过类似demo但是时间过太久忘记了,现在已经完 ...
- 论tab切换的几种实现方法
tab切换在网页中很常见,故最近总结了4种实现方法. 首先,写出tab的框架,加上最简单的样式,代码如下: <!DOCTYPE html> <html> <head> ...
- C#模拟百度登录并到指定网站评论回帖(三)
上次说到怎么获取BAIDUID,这个相信很多人都能够拿到就不多说了,今天一连说两个,获取token和raskey 2.利用以上获得的cookie直接访问页面 https://passport.baid ...
- 漫谈未来的HDFS
前面我们提到的HDFS,了解了HDFS的特性和架构.HDFS能够存储TB甚至PB规模的数据是有前提的,首先数据要以大文件为主,其次NameNode的内存要足够大.对HDFS有所了解的同学肯定都知道,N ...
- 全志A33平台编译linux(分色排版)sina33
全志A33平台编译linux 大文实验室/大文哥 壹捌陆捌零陆捌捌陆捌贰 21504965 AT qq.com 完成时间:2017/12/12 17:36 版本:V1.0 Xshell 5 (Buil ...
- 重构27-Remove God Classes(去掉神类)
在传统的代码库中,我们常常会看到一些违反了SRP原则的类.这些类通常以Utils或Manager结尾,有时也没有这么明显的特征而仅仅是普通的包含多个功能的类.这种God类还有一个特征,使用语句或注释将 ...
- sql server nullif的使用技巧,除数为零的处理技巧
在sql server中做除法处理的时候,我们经常需要处理除数为零的情况,因为如果遇到这种情况的时候,sqlserver会抛出遇到以零作除数错误的异常,我们总不希望把这个异常显示给用户吧. 做个会报这 ...
- (转) 淘淘商城系列——Redis集群的搭建
http://blog.csdn.net/yerenyuan_pku/article/details/72860432 本文我将带领大家如何搭建Redis集群.首先说一下,为何要搭建Redis集群.R ...
- 大数除法(除数在int范围内)
#include<iostream> #include<cstdio> #include<cstring> #define N 1000 using namespa ...
- swift VS NSObject
Any class that does not inherit from another class is known as a base class. Swift classes do not in ...