在element-ui的el-tree組件中用render函數(shù)生成el-button的實(shí)例代碼
本文主要介紹怎么在el-tree組件中通過(guò)render函數(shù)來(lái)生成el-button。
這是element-ui中el-tree樹:

這是需要實(shí)現(xiàn)的效果:

tree.vue文件中,具體實(shí)現(xiàn)的代碼如下:
<template>
<el-tree
:data="treeData"
:props="defaultProps"
show-checkbox
node-key="id"
default-expand-all
:expand-on-click-node="false"
:render-content="renderContent">
</el-tree>
</template>
<script>
export default {
name: 'tree',
data:function(){
return {
treeData: [{
id: 1,
label: '一級(jí)',
code:'1',
children: [{
id: 4,
label: '二級(jí)',
code:'1-1',
children: [{
id: 9,
label: '三級(jí)',
code:'1-1-1',
}, {
id: 10,
label: '三級(jí)',
code: '1-1-2',
}]
}]
}],
defaultProps: {
children: 'children',
label: 'label'
}
}
},
methods:{
renderContent:function(h,data){
return h('span',{},[
h('span',data.data.label+":"),
h("span",{
class:'leftSpan',
domProps:{
innerHTML:data.data.code
}
}),
h("el-button",{
class:'floatSpan',
props:{
type:'danger'
}
},'刪除')
])
}
}
}
</script>
<style>
.leftSpan{
color: dodgerblue;
margin-left: 15px;
}
.floatSpan{
float: right;
margin-top: 8px;
margin-right: 10px;
padding: 5px;
}
.el-tree {
width: 33%;
margin: 92px auto;
}
</style>
主要說(shuō)下這段代碼:
renderContent:function(h,data){
return h('span',{},[
h('span',data.data.label+":"),
h("span",{
class:'leftSpan',
domProps:{
innerHTML:data.data.code
}
}),
h("el-button",{
class:'floatSpan',
props:{
type:'danger'
}
},'刪除')
])
}
文檔中有詳細(xì)的說(shuō)明:https://cn.vuejs.org/v2/guide/render-function.html
h(
//參數(shù)1:{String | Object | Function},一個(gè)HTML標(biāo)簽字符串,組件選項(xiàng)對(duì)象,或解析任何一種的一個(gè)async異步函數(shù),必需參數(shù)。
'el-button',
//參數(shù)2:{Object} 一個(gè)包含模板相關(guān)屬性的數(shù)據(jù)對(duì)象,可以在template中使用這些特性,可選參數(shù)
{},
//參數(shù)3: {String | Array},如果直接是字符串則會(huì)生成“文本虛擬節(jié)點(diǎn);如果是數(shù)組,則可以在數(shù)組中,則可以生成子虛擬節(jié)點(diǎn)
'刪除'
)
參數(shù)2中的對(duì)象在文檔中有詳細(xì)的介紹:
{
// 和`v-bind:class`一樣的 API
// 接收一個(gè)字符串、對(duì)象或字符串和對(duì)象組成的數(shù)組
'class': {
foo: true,
bar: false
},
// 和`v-bind:style`一樣的 API
// 接收一個(gè)字符串、對(duì)象或?qū)ο蠼M成的數(shù)組
style: {
color: 'red',
fontSize: '14px'
},
// 普通的 HTML 特性
attrs: {
id: 'foo'
},
// 組件 props
props: {
myProp: 'bar'
},
// DOM 屬性
domProps: {
innerHTML: 'baz'
},
// 事件監(jiān)聽(tīng)器基于 `on`
// 所以不再支持如 `v-on:keyup.enter` 修飾器
// 需要手動(dòng)匹配 keyCode。
on: {
click: this.clickHandler
},
// 僅用于組件,用于監(jiān)聽(tīng)原生事件,而不是組件內(nèi)部使用
// `vm.$emit` 觸發(fā)的事件。
nativeOn: {
click: this.nativeClickHandler
},
// 自定義指令。注意,你無(wú)法對(duì) `binding` 中的 `oldValue`
// 賦值,因?yàn)?Vue 已經(jīng)自動(dòng)為你進(jìn)行了同步。
directives: [
{
name: 'my-custom-directive',
value: '2',
expression: '1 + 1',
arg: 'foo',
modifiers: {
bar: true
}
}
],
// 作用域插槽格式
// { name: props => VNode | Array<VNode> }
scopedSlots: {
default: props => createElement('span', props.text)
},
// 如果組件是其他組件的子組件,需為插槽指定名稱
slot: 'name-of-slot',
// 其他特殊頂層屬性
key: 'myKey',
ref: 'myRef',
// 如果你在渲染函數(shù)中向多個(gè)元素都應(yīng)用了相同的 ref 名,
// 那么 `$refs.myRef` 會(huì)變成一個(gè)數(shù)組。
refInFor: true
}
總結(jié)
以上所述是小編給大家介紹的在element-ui的el-tree組件中用render函數(shù)生成el-button的實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
前端開(kāi)發(fā)必須知道的JS之閉包及應(yīng)用
本文講的是函數(shù)閉包,不涉及對(duì)象閉包(如用with實(shí)現(xiàn))。如果你覺(jué)得我說(shuō)的有偏差,歡迎拍磚,歡迎指教。2010-07-07
JS代碼實(shí)現(xiàn)table數(shù)據(jù)分頁(yè)效果
這篇文章主要介紹了JS代碼實(shí)現(xiàn)table數(shù)據(jù)分頁(yè)效果的相關(guān)資料,非常不錯(cuò),代碼簡(jiǎn)答易懂,非常實(shí)用,需要的朋友可以參考下2016-05-05
javascript實(shí)現(xiàn)獲取指定精度的上傳文件的大小簡(jiǎn)單實(shí)例
下面小編就為大家?guī)?lái)一篇javascript實(shí)現(xiàn)獲取指定精度的上傳文件的大小簡(jiǎn)單實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-10-10
使用uniapp打包微信小程序時(shí)主包和vendor.js過(guò)大解決(uniCloud的插件分包)
每個(gè)使用分包小程序必定含有一個(gè)主包,所謂的主包,即放置默認(rèn)啟動(dòng)頁(yè)面/TabBar頁(yè)面,以及一些所有分包都需用到公共資源/JS 腳本,下面這篇文章主要給大家介紹了關(guān)于使用uniapp打包微信小程序時(shí)主包和vendor.js過(guò)大解決的相關(guān)資料,,需要的朋友可以參考下2023-02-02

