理解Vue中的Render渲染函数
理解Vue中的Render渲染函数
VUE一般使用template来创建HTML,然后在有的时候,我们需要使用javascript来创建html,这时候我们需要使用render函数。
比如如下我想要实现如下html:
<div id="container">
<h1>
<a href="#">
Hello world!
</a>
</h1>
</div>
我们会如下使用:
<!DOCTYPE html>
<html>
<head>
<title>演示Vue</title>
<style> </style>
</head>
<body>
<div id="container">
<tb-heading :level="1">
<a href="#">Hello world!</a>
</tb-heading>
</div>
</body>
<script src="./vue.js"></script> <script type="text/x-template" id="templateId">
<h1 v-if="level === 1">
<slot></slot>
</h1>
<h2 v-else-if="level === 2">
<slot></slot>
</h2>
</script> <script>
Vue.component('tb-heading', {
template: '#templateId',
props: {
level: {
type: Number,
required: true
}
}
});
new Vue({
el: '#container'
});
</script>
</html>
如上代码是根据参数 :level来显示不同级别的标题中插入锚点元素,我们需要重复的使用 <slot></slot>.
下面我们来尝试使用 render函数重写上面的demo;如下代码:
<!DOCTYPE html>
<html>
<head>
<title>演示Vue</title>
<style> </style>
</head>
<body>
<div id="container">
<tb-heading :level="2">
<a href="#">Hello world!</a>
</tb-heading>
</div>
</body>
<script src="./vue.js"></script> <script>
Vue.component('tb-heading', {
render: function(createElement) {
return createElement(
'h' + this.level, // tag name 标签名称
this.$slots.default // 组件的子元素
)
},
props: {
level: {
type: Number,
required: true
}
}
});
new Vue({
el: '#container'
});
</script>
</html>
如上 render函数代码看起来非常简单就实现了,组件中的子元素存储在组件实列中 $slots.default 中。
理解createElement
Vue通过建立一个虚拟DOM对真实的DOM发生变化保存追踪,如下代码:
return createElement('h1', this.title);
createElement返回的是包含的信息会告诉VUE页面上需要渲染什么样的节点及其子节点。我们称这样的节点为虚拟DOM,可以简写为VNode,
createElement 参数 // @return {VNode}
createElement(
// {String | Object | Function}
// 一个HTML标签字符串,组件选项对象,或者一个返回值类型为String/Object的函数。该参数是必须的
'div', // {Object}
// 一个包含模板相关属性的数据对象,这样我们可以在template中使用这些属性,该参数是可选的。
{ }, // {String | Array}
// 子节点(VNodes)由 createElement() 构建而成。可选参数
// 或简单的使用字符串来生成的 "文本节点"。
[
'xxxx',
createElement('h1', '一则头条'),
createElement(MyComponent, {
props: {
someProp: 'xxx'
}
})
]
)
理解深入data对象。
在模板语法中,我们可以使用 v-bind:class 和 v-bind:style 来绑定属性,在VNode数据对象中,下面的属性名的字段级别是最高的。
该对象允许我们绑定普通的html特性,就像DOM属性一样。如下:
{
// 和`v-bind:class`一样的 API
'class': {
foo: true,
bar: false
},
// 和`v-bind:style`一样的 API
style: {
color: 'red',
fontSize: '14px'
},
// 正常的 HTML 特性
attrs: {
id: 'foo'
},
// 组件 props
props: {
myProp: 'bar'
},
// DOM 属性
domProps: {
innerHTML: 'baz'
},
// 事件监听器基于 `on`
// 所以不再支持如 `v-on:keyup.enter` 修饰器
// 需要手动匹配 keyCode。
on: {
click: this.clickHandler
},
// 仅对于组件,用于监听原生事件,而不是组件内部使用 `vm.$emit` 触发的事件。
nativeOn: {
click: this.nativeClickHandler
},
// 自定义指令。注意事项:不能对绑定的旧值设值
// Vue 会为您持续追踪
directives: [
{
name: 'my-custom-directive',
value: '2',
expression: '1 + 1',
arg: 'foo',
modifiers: {
bar: true
}
}
],
// Scoped slots in the form of
// { name: props => VNode | Array<VNode> }
scopedSlots: {
default: props => createElement('span', props.text)
},
// 如果组件是其他组件的子组件,需为插槽指定名称
slot: 'name-of-slot',
// 其他特殊顶层属性
key: 'myKey',
ref: 'myRef'
}
上面的data数据可能不太好理解,我们来看一个demo,就知道它是如何使用的了,如下代码:
<!DOCTYPE html>
<html>
<head>
<title>演示Vue</title>
<style> </style>
</head>
<body>
<div id="container">
<tb-heading :level="2">
Hello world!
</tb-heading>
</div>
</body>
<script src="./vue.js"></script> <script>
var getChildrenTextContent = function(children) {
return children.map(function(node) {
return node.children ? getChildrenTextContent(node.children) : node.text
}).join('')
};
Vue.component('tb-heading', {
render: function(createElement) {
var headingId = getChildrenTextContent(this.$slots.default)
.toLowerCase()
.replace(/\W+/g, '-')
.replace(/(^\-|\-$)/g, '')
return createElement(
'h' + this.level,
[
createElement('a', {
attrs: {
name: headingId,
href: '#' + headingId
},
style: {
color: 'red',
fontSize: '20px'
},
'class': {
foo: true,
bar: false
},
// DOM属性
domProps: {
innerHTML: 'baz'
},
// 组件props
props: {
myProp: 'bar'
},
// 事件监听基于 'on'
// 所以不再支持如 'v-on:keyup.enter' 修饰语
// 需要手动匹配 KeyCode
on: {
click: function(event) {
event.preventDefault();
console.log(111);
}
}
}, this.$slots.default)
]
)
},
props: {
level: {
type: Number,
required: true
}
}
});
new Vue({
el: '#container'
});
</script>
</html>
对应的属性使用方法和上面一样既可以了,我们可以打开页面查看下效果也是可以的。如下
VNodes 不一定必须唯一 (文档中说要唯一)
文档中说 VNode必须唯一;说 下面的 render function 是无效的:
但是我通过测试时可以的,如下代码:
<!DOCTYPE html>
<html>
<head>
<title>演示Vue</title>
<style> </style>
</head>
<body>
<div id="container">
<tb-heading :level="2">
Hello world!
</tb-heading>
</div>
</body>
<script src="./vue.js"></script> <script>
Vue.component('tb-heading', {
render: function(createElement) {
var pElem = createElement('p', 'hello world');
return createElement('div', [
pElem, pElem
])
},
props: {
level: {
type: Number,
required: true
}
}
});
new Vue({
el: '#container'
});
</script>
</html>
使用Javascript代替模板功能
v-if 和 v-for
template 中有 v-if 和 v-for, 但是vue中的render函数没有提供专用的API。
比如如下:
<ul v-if="items.length">
<li v-for="item in items">{{ item.name }}</li>
</ul>
<p v-else>No item found.</p>
在render函数中会被javascript的 if/else 和map重新实现。如下代码:
<!DOCTYPE html>
<html>
<head>
<title>演示Vue</title>
<style> </style>
</head>
<body>
<div id="container">
<tb-heading>
Hello world!
</tb-heading>
</div>
</body>
<script src="./vue.js"></script> <script>
Vue.component('tb-heading', {
render: function(createElement) {
console.log(this)
if (this.items.length) {
return createElement('ul', this.items.map(function(item){
return createElement('li', item.name);
}))
} else {
return createElement('p', 'No items found.');
}
}, props: {
items: {
type: Array,
default: function() {
return [
{
name: 'kongzhi1'
},
{
name: 'kongzhi2'
}
]
}
}
}
});
new Vue({
el: '#container'
});
</script>
</html>
v-model
render函数中没有 与 v-model相应的api,我们必须自己来实现相应的逻辑。如下代码可以实现:
<!DOCTYPE html>
<html>
<head>
<title>演示Vue</title>
<style> </style>
</head>
<body>
<div id="container">
<tb-heading @input="inputFunc">
Hello world!
</tb-heading>
</div>
</body>
<script src="./vue.js"></script> <script>
Vue.component('tb-heading', {
render: function(createElement) {
var self = this;
return createElement('input', {
domProps: {
value: '11'
},
on: {
input: function(event) {
self.value = event.target.value;
self.$emit('input', self.value);
}
}
})
},
props: { }
});
new Vue({
el: '#container',
methods: {
inputFunc: function(value) {
console.log(value)
}
}
});
</script>
</html>
理解插槽
可以从 this.$slots 获取VNodes列表中的静态内容:如下代码:
<!DOCTYPE html>
<html>
<head>
<title>演示Vue</title>
<style> </style>
</head>
<body>
<div id="container">
<tb-heading :level="2">
<a href="#">Hello world!</a>
</tb-heading>
</div>
</body>
<script src="./vue.js"></script> <script>
Vue.component('tb-heading', {
render: function(createElement) {
return createElement(
'h' + this.level, // tag name 标签名称
this.$slots.default // 子组件
)
},
props: {
level: {
type: Number,
required: true
}
}
});
new Vue({
el: '#container'
});
</script>
</html>
理解函数式组件
函数式组件我们标记组件为 functional, 意味着它无状态(没有data), 无实列(没有this上下文)。
一个函数式组件像下面这样的:
Vue.component('my-component', {
functional: true,
// 为了弥补缺少的实列
// 提供第二个参数作为上下文
render: function(createElement, context) { },
// Props 可选
props: { }
})
组件需要的一切通过上下文传递,包括如下:
props: 提供props对象
children: VNode子节点的数组
slots: slots对象
data: 传递给组件的data对象
parent: 对父组件的引用
listeners: (2.3.0+) 一个包含了组件上所注册的 v-on 侦听器的对象。这只是一个指向 data.on 的别名。
injections: (2.3.0+) 如果使用了 inject 选项,则该对象包含了应当被注入的属性。
在添加 functional: true 之后,组件的 render 函数之间简单更新增加 context 参数,this.$slots.default 更新为 context.children,之后this.level 更新为 context.props.level。
如下代码演示:
<!DOCTYPE html>
<html>
<head>
<title>演示Vue</title>
<style> </style>
</head>
<body> <div id="container">
{{msg}}
<choice>
<item value="1">test</item>
</choice>
</div> </body>
<script src="./vue.js"></script> <script>
Vue.component('choice', {
template: '<div><ul><slot></slot></ul></div>'
}); Vue.component('item', {
functional: true,
render: function(h, context) {
return h('li', {
on: {
click: function() {
console.log(context);
console.log(context.parent);
console.log(context.props)
}
}
}, context.children)
},
props: ['value']
}) new Vue({
el: '#container',
data: {
msg: 'hello'
}
});
</script>
</html>
理解Vue中的Render渲染函数的更多相关文章
- Vue.js 2.x render 渲染函数 & JSX
Vue.js 2.x render 渲染函数 & JSX Vue绝大多数情况下使用template创建 HTML.但是比如一些重复性比较高的场景,需要运用 JavaScript 的完全编程能力 ...
- vue render 渲染函数
vue render 渲染函数 经常看到使用render渲染函数的示例,而且在一些特殊情况下,确实更好使用,可以更加有效地细分组件,因而借助vue-element-admin来学习一波 render函 ...
- Render渲染函数和JSX
1.Render函数:render是用来替换temlate的,需要更灵活的模板的写法的时候,用render. 官网API地址:https://cn.vuejs.org/v2/guide/render- ...
- vue系列---理解Vue中的computed,watch,methods的区别及源码实现(六)
_ 阅读目录 一. 理解Vue中的computed用法 二:computed 和 methods的区别? 三:Vue中的watch的用法 四:computed的基本原理及源码实现 回到顶部 一. 理解 ...
- 理解vue中的scope的使用
理解vue中的scope的使用 我们都知道vue slot插槽可以传递任何属性或html元素,但是在调用组件的页面中我们可以使用 template scope="props"来获取 ...
- 深入理解javascript中的立即执行函数(function(){…})()
投稿:junjie 字体:[增加 减小] 类型:转载 时间:2014-06-12 我要评论 这篇文章主要介绍了深入理解javascript中的立即执行函数,立即执行函数也叫立即调用函数,通常它的写法是 ...
- 深入理解javascript中的立即执行函数
这篇文章主要介绍了深入理解javascript中的立即执行函数,立即执行函数也叫立即调用函数,通常它的写法是用(function(){…})()包住业务代码,使用jquery时比较常见,需要的朋友可以 ...
- 基于VueJS的render渲染函数结合自定义组件打造一款非常强大的IView 的Table
基于VueJS的render渲染函数结合自定义组件打造一款非常强大的IView 的Table https://segmentfault.com/a/1190000015970367
- Vue之render渲染函数和JSX的应用
一.模板缺陷 模板的最大特点是扩展难度大,不易扩展.可能会造成逻辑冗余 <Level :type="1">哈哈</Level> <Level :typ ...
随机推荐
- Python学习一:序列基础详解
作者:NiceCui 本文谢绝转载,如需转载需征得作者本人同意,谢谢. 本文链接:http://www.cnblogs.com/NiceCui/p/7858473.html 邮箱:moyi@moyib ...
- HTML5浏览器端图片预览&生成Base64
本文主要介绍如何通过拖拽方式在浏览器端实现图片预览,并生成图片的Base64编码. 工具链接:图片转Base64. 首先介绍一下FileReader, FileReader对象允许浏览器使用File或 ...
- 教你3分钟读懂HTML5语言的特点
HTML5的跨平台技术 HTML5技术跨平台,适配多终端.传统移动终端上的Native App,开发者的研发工作必须针对不同的操作系统进行,成本相对较高.Native App对于用户还存在着管理成本. ...
- iis7 部署网站 403错误
C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -i 403 - 禁止访问: 访问被拒绝. 您无权使用所提供的凭据查看此 ...
- docker-compose v3版本命令详解参考
参考和指南 这些主题描述了Compose文件格式的第3版.这是最新的版本. Compose and Docker 兼容性矩阵 有几个版本的Compose文件格式 - 1,2,2.x和3.x.下表是快速 ...
- 找到链表的倒数第K位
#include<iostream> using namespace std; class node{ public: node():value(),next(NULL){} ~node( ...
- codeforces 897B Chtholly's request 偶数长度回文数
codeforces 897B Chtholly's request 题目链接: http://codeforces.com/problemset/problem/897/B 思路: 暴力求出这100 ...
- 51Nod--1049最大子段和
1049 最大子段和 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 N个整数组成的序列a[1],a[2],a[3],-,a[n],求该序列如a[i]+a ...
- (高级篇)php结合redis实现高并发下的抢购、秒杀功能
抢购.秒杀是如今很常见的一个应用场景,主要需要解决的问题有两个:1 高并发对数据库产生的压力2 竞争状态下如何解决库存的正确减少("超卖"问题)对于第一个问题,已经很容易想到用缓存 ...
- 深港澳大湾区第三次.NET技术交流会圆满成功
2017年12月10日,一场以云.devops.微服务.容器是现在这个发展阶段的软件形态, 本次活动我们围绕这些话题介绍.NET生态下的发展本地社区活动,这次活动还得到如鹏网杨中科老师的大力支持开通网 ...