做网站需要准备资料,广告设计与制作专业怎么样,成都网站建设制作公司,中山电子商务网站建设Vue中的Render渲染函数 VUE一般使用template来创建HTML#xff0c;然后在有的时候#xff0c;我们需要使用javascript来创建html#xff0c;这时候我们需要使用render函数。比如如下我想要实现如下html#xff1a; div idcontainerh1a hre… Vue中的Render渲染函数 VUE一般使用template来创建HTML然后在有的时候我们需要使用javascript来创建html这时候我们需要使用render函数。比如如下我想要实现如下html div idcontainerh1a href#Hello world!/a/h1
/div我们会如下使用 !DOCTYPE html
htmlheadtitle演示Vue/titlestyle/style/headbodydiv idcontainertb-heading :level1a href#Hello world!/a/tb-heading/div/bodyscript src./vue.js/scriptscript typetext/x-template idtemplateIdh1 v-iflevel 1slot/slot/h1h2 v-else-iflevel 2slot/slot/h2/scriptscriptVue.component(tb-heading, {template: #templateId,props: {level: {type: Number,required: true}}});new Vue({el: #container});/script
/html如上代码是根据参数 :level来显示不同级别的标题中插入锚点元素我们需要重复的使用 slot/slot. 下面我们来尝试使用 render函数重写上面的demo如下代码 !DOCTYPE html
htmlheadtitle演示Vue/titlestyle/style/headbodydiv idcontainertb-heading :level2a href#Hello world!/a/tb-heading/div/bodyscript src./vue.js/scriptscriptVue.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 中。 理解createElementVue通过建立一个虚拟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一样的 APIclass: {foo: true,bar: false},// 和v-bind:style一样的 APIstyle: {color: red,fontSize: 14px},// 正常的 HTML 特性attrs: {id: foo},// 组件 propsprops: {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 | ArrayVNode }scopedSlots: {default: props createElement(span, props.text)},// 如果组件是其他组件的子组件需为插槽指定名称slot: name-of-slot,// 其他特殊顶层属性key: myKey,ref: myRef
}上面的data数据可能不太好理解我们来看一个demo就知道它是如何使用的了如下代码 !DOCTYPE html
htmlheadtitle演示Vue/titlestyle/style/headbodydiv idcontainertb-heading :level2Hello world!/tb-heading/div/bodyscript src./vue.js/scriptscriptvar 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},// 组件propsprops: {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
htmlheadtitle演示Vue/titlestyle/style/headbodydiv idcontainertb-heading :level2Hello world!/tb-heading/div/bodyscript src./vue.js/scriptscriptVue.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-fortemplate 中有 v-if 和 v-for 但是vue中的render函数没有提供专用的API。比如如下 ul v-ifitems.lengthli v-foritem in items{{ item.name }}/li
/ul
p v-elseNo item found./p在render函数中会被javascript的 if/else 和map重新实现。如下代码 !DOCTYPE html
htmlheadtitle演示Vue/titlestyle/style/headbodydiv idcontainertb-headingHello world!/tb-heading/div/bodyscript src./vue.js/scriptscriptVue.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
/htmlv-model render函数中没有 与 v-model相应的api我们必须自己来实现相应的逻辑。如下代码可以实现 !DOCTYPE html
htmlheadtitle演示Vue/titlestyle/style/headbodydiv idcontainertb-heading inputinputFuncHello world!/tb-heading/div/bodyscript src./vue.js/scriptscriptVue.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
htmlheadtitle演示Vue/titlestyle/style/headbodydiv idcontainertb-heading :level2a href#Hello world!/a/tb-heading/div/bodyscript src./vue.js/scriptscriptVue.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
htmlheadtitle演示Vue/titlestyle/style/headbodydiv idcontainer{{msg}}choiceitem value1test/item/choice/div/bodyscript src./vue.js/scriptscriptVue.component(choice, {template: divulslot/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
/htmlhttps://www.zhihu.com/question/54217073 转载于:https://www.cnblogs.com/smzd/p/8868759.html