当前位置:Gxlcms > JavaScript > 在element-ui的el-tree组件中用render函数生成el-button的实例代码

在element-ui的el-tree组件中用render函数生成el-button的实例代码

时间:2021-07-01 10:21:17 帮助过:104人阅读

本文主要介绍怎么在el-tree组件中通过render函数来生成el-button。

这是element-ui中el-tree树:

这是需要实现的效果:

tree.vue文件中,具体实现的代码如下:

  1. <template>
  2. <el-tree
  3. :data="treeData"
  4. :props="defaultProps"
  5. show-checkbox
  6. node-key="id"
  7. default-expand-all
  8. :expand-on-click-node="false"
  9. :render-content="renderContent">
  10. </el-tree>
  11. </template>
  12. <script>
  13. export default {
  14. name: 'tree',
  15. data:function(){
  16. return {
  17. treeData: [{
  18. id: 1,
  19. label: '一级',
  20. code:'1',
  21. children: [{
  22. id: 4,
  23. label: '二级',
  24. code:'1-1',
  25. children: [{
  26. id: 9,
  27. label: '三级',
  28. code:'1-1-1',
  29. }, {
  30. id: 10,
  31. label: '三级',
  32. code: '1-1-2',
  33. }]
  34. }]
  35. }],
  36. defaultProps: {
  37. children: 'children',
  38. label: 'label'
  39. }
  40. }
  41. },
  42. methods:{
  43. renderContent:function(h,data){
  44. return h('span',{},[
  45. h('span',data.data.label+":"),
  46. h("span",{
  47. class:'leftSpan',
  48. domProps:{
  49. innerHTML:data.data.code
  50. }
  51. }),
  52. h("el-button",{
  53. class:'floatSpan',
  54. props:{
  55. type:'danger'
  56. }
  57. },'删除')
  58. ])
  59. }
  60. }
  61. }
  62. </script>
  63. <style>
  64. .leftSpan{
  65. color: dodgerblue;
  66. margin-left: 15px;
  67. }
  68. .floatSpan{
  69. float: right;
  70. margin-top: 8px;
  71. margin-right: 10px;
  72. padding: 5px;
  73. }
  74. .el-tree {
  75. width: 33%;
  76. margin: 92px auto;
  77. }
  78. </style>

主要说下这段代码:

  1. renderContent:function(h,data){
  2. return h('span',{},[
  3. h('span',data.data.label+":"),
  4. h("span",{
  5. class:'leftSpan',
  6. domProps:{
  7. innerHTML:data.data.code
  8. }
  9. }),
  10. h("el-button",{
  11. class:'floatSpan',
  12. props:{
  13. type:'danger'
  14. }
  15. },'删除')
  16. ])
  17. }

文档中有详细的说明:https://cn.vuejs.org/v2/guide/render-function.html

  1. h(
  2. //参数1:{String | Object | Function},一个HTML标签字符串,组件选项对象,或解析任何一种的一个async异步函数,必需参数。
  3. 'el-button',
  4. //参数2:{Object} 一个包含模板相关属性的数据对象,可以在template中使用这些特性,可选参数
  5. {},
  6. //参数3: {String | Array},如果直接是字符串则会生成“文本虚拟节点;如果是数组,则可以在数组中,则可以生成子虚拟节点
  7. '删除'
  8. )

参数2中的对象在文档中有详细的介绍:

  1. {
  2. // 和`v-bind:class`一样的 API
  3. // 接收一个字符串、对象或字符串和对象组成的数组
  4. 'class': {
  5. foo: true,
  6. bar: false
  7. },
  8. // 和`v-bind:style`一样的 API
  9. // 接收一个字符串、对象或对象组成的数组
  10. style: {
  11. color: 'red',
  12. fontSize: '14px'
  13. },
  14. // 普通的 HTML 特性
  15. attrs: {
  16. id: 'foo'
  17. },
  18. // 组件 props
  19. props: {
  20. myProp: 'bar'
  21. },
  22. // DOM 属性
  23. domProps: {
  24. innerHTML: 'baz'
  25. },
  26. // 事件监听器基于 `on`
  27. // 所以不再支持如 `v-on:keyup.enter` 修饰器
  28. // 需要手动匹配 keyCode。
  29. on: {
  30. click: this.clickHandler
  31. },
  32. // 仅用于组件,用于监听原生事件,而不是组件内部使用
  33. // `vm.$emit` 触发的事件。
  34. nativeOn: {
  35. click: this.nativeClickHandler
  36. },
  37. // 自定义指令。注意,你无法对 `binding` 中的 `oldValue`
  38. // 赋值,因为 Vue 已经自动为你进行了同步。
  39. directives: [
  40. {
  41. name: 'my-custom-directive',
  42. value: '2',
  43. expression: '1 + 1',
  44. arg: 'foo',
  45. modifiers: {
  46. bar: true
  47. }
  48. }
  49. ],
  50. // 作用域插槽格式
  51. // { name: props => VNode | Array<VNode> }
  52. scopedSlots: {
  53. default: props => createElement('span', props.text)
  54. },
  55. // 如果组件是其他组件的子组件,需为插槽指定名称
  56. slot: 'name-of-slot',
  57. // 其他特殊顶层属性
  58. key: 'myKey',
  59. ref: 'myRef',
  60. // 如果你在渲染函数中向多个元素都应用了相同的 ref 名,
  61. // 那么 `$refs.myRef` 会变成一个数组。
  62. refInFor: true
  63. }

总结

以上所述是小编给大家介绍的在element-ui的el-tree组件中用render函数生成el-button的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

人气教程排行