日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > vue >内容正文

vue

vue render函数

發(fā)布時間:2023/12/10 vue 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 vue render函数 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Vue中的Render渲染函數(shù)

VUE一般使用template來創(chuàng)建HTML,然后在有的時候,我們需要使用javascript來創(chuàng)建html,這時候我們需要使用render函數(shù)。
比如如下我想要實(shí)現(xiàn)如下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>

  

如上代碼是根據(jù)參數(shù) :level來顯示不同級別的標(biāo)題中插入錨點(diǎn)元素,我們需要重復(fù)的使用 <slot></slot>.

下面我們來嘗試使用 render函數(shù)重寫上面的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 標(biāo)簽名稱this.$slots.default // 組件的子元素)},props: {level: {type: Number,required: true}}});new Vue({el: '#container'});</script> </html>

  如上 render函數(shù)代碼看起來非常簡單就實(shí)現(xiàn)了,組件中的子元素存儲在組件實(shí)列中 $slots.default 中。

理解createElement
Vue通過建立一個虛擬DOM對真實(shí)的DOM發(fā)生變化保存追蹤,如下代碼:
return createElement('h1', this.title);
createElement返回的是包含的信息會告訴VUE頁面上需要渲染什么樣的節(jié)點(diǎn)及其子節(jié)點(diǎn)。我們稱這樣的節(jié)點(diǎn)為虛擬DOM,可以簡寫為VNode,

createElement 參數(shù)// @return {VNode} createElement(// {String | Object | Function}// 一個HTML標(biāo)簽字符串,組件選項(xiàng)對象,或者一個返回值類型為String/Object的函數(shù)。該參數(shù)是必須的'div',// {Object}// 一個包含模板相關(guān)屬性的數(shù)據(jù)對象,這樣我們可以在template中使用這些屬性,該參數(shù)是可選的。{},// {String | Array}// 子節(jié)點(diǎn)(VNodes)由 createElement() 構(gòu)建而成。可選參數(shù)// 或簡單的使用字符串來生成的 "文本節(jié)點(diǎn)"。['xxxx',createElement('h1', '一則頭條'),createElement(MyComponent, {props: {someProp: 'xxx'}})] )

  理解深入data對象。

在模板語法中,我們可以使用 v-bind:class 和 v-bind:style 來綁定屬性,在VNode數(shù)據(jù)對象中,下面的屬性名的字段級別是最高的。
該對象允許我們綁定普通的html特性,就像DOM屬性一樣。如下:

{// 和`v-bind:class`一樣的 API'class': {foo: true,bar: false},// 和`v-bind:style`一樣的 APIstyle: {color: 'red',fontSize: '14px'},// 正常的 HTML 特性attrs: {id: 'foo'},// 組件 propsprops: {myProp: 'bar'},// DOM 屬性domProps: {innerHTML: 'baz'},// 事件監(jiān)聽器基于 `on`// 所以不再支持如 `v-on:keyup.enter` 修飾器// 需要手動匹配 keyCode。on: {click: this.clickHandler},// 僅對于組件,用于監(jiān)聽原生事件,而不是組件內(nèi)部使用 `vm.$emit` 觸發(fā)的事件。nativeOn: {click: this.nativeClickHandler},// 自定義指令。注意事項(xiàng):不能對綁定的舊值設(shè)值// Vue 會為您持續(xù)追蹤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數(shù)據(jù)可能不太好理解,我們來看一個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'},// 組件propsprops: {myProp: 'bar'},// 事件監(jiān)聽基于 '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>

  對應(yīng)的屬性使用方法和上面一樣既可以了,我們可以打開頁面查看下效果也是可以的。如下

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函數(shù)沒有提供專用的API。
比如如下:

<ul v-if="items.length"><li v-for="item in items">{{ item.name }}</li> </ul> <p v-else>No item found.</p>

  在render函數(shù)中會被javascript的 if/else 和map重新實(shí)現(xiàn)。如下代碼:

<!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函數(shù)中沒有 與 v-model相應(yīng)的api,我們必須自己來實(shí)現(xiàn)相應(yīng)的邏輯。如下代碼可以實(shí)現(xiàn):

<!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列表中的靜態(tài)內(nèi)容:如下代碼:

<!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 標(biāo)簽名稱this.$slots.default // 子組件)},props: {level: {type: Number,required: true}}});new Vue({el: '#container'});</script> </html>

  

理解函數(shù)式組件

函數(shù)式組件我們標(biāo)記組件為 functional, 意味著它無狀態(tài)(沒有data), 無實(shí)列(沒有this上下文)。
一個函數(shù)式組件像下面這樣的:

Vue.component('my-component', {functional: true,// 為了彌補(bǔ)缺少的實(shí)列// 提供第二個參數(shù)作為上下文render: function(createElement, context) {},// Props 可選props: {} })

  

組件需要的一切通過上下文傳遞,包括如下:
props:?提供props對象
children:?VNode子節(jié)點(diǎn)的數(shù)組
slots:?slots對象
data:?傳遞給組件的data對象
parent:?對父組件的引用
listeners: (2.3.0+)?一個包含了組件上所注冊的 v-on 偵聽器的對象。這只是一個指向 data.on 的別名。
injections: (2.3.0+)?如果使用了 inject 選項(xiàng),則該對象包含了應(yīng)當(dāng)被注入的屬性。

在添加 functional: true 之后,組件的 render 函數(shù)之間簡單更新增加 context 參數(shù),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>

  

?https://www.zhihu.com/question/54217073

轉(zhuǎn)載于:https://www.cnblogs.com/smzd/p/8868759.html

總結(jié)

以上是生活随笔為你收集整理的vue render函数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。