elementUI ツリー ツリー コントロールは、Alibaba アイコンを組み合わせて三角形のアイコン スタイルを変更します。

要件: プロジェクトは、フォルダー j タイプのフォルダー リスト ツリーです。デザインでは、三角形のアイコンを UI デザイン アイコンに置き換える必要があります。UI デザイン アイコンは、Alibaba アイコン ライブラリに配置されています。

// main.js中先引入iconfont的css和js
import '@/assets/iconfont/iconfont.css'
import '@/assets/iconfont/iconfont.js'

解決策 1.

<el-tree
            :data="getPerSrcTree"
            default-expand-all
            :expand-on-click-node="false"
            node-key="id"
            ref="atree"
            highlight-current
            :check-strictly="true"
            :props="defaultProps"
            @node-click="nodeClick"
          >
             <span class="custom-tree-node"  slot-scope="{ node }">
                <span style="display: flex;align-items: center;">
                  <i calss="iconfont icon-wendangdakai" width="20px" height="20px"></i>{
    
    {
    
     node.label  }}
                </span>              
            </span> 
          </el-tree>

この方法で作成したアイコンには色がついていないので、cssで必要な色を設定する必要がありますが、svgを使えば元の色を直接表示することができます。

<el-tree
    :data="getPerSrcTree"
      default-expand-all
      :expand-on-click-node="false"
      node-key="id"
      ref="atree"
      highlight-current
      :check-strictly="true"
      :props="defaultProps"
      @node-click="nodeClick"
    >
      <span class="custom-tree-node"  slot-scope="{ node }">
          <span style="display: flex;align-items: center;">
            <iconSvg name="wendangdakai" width="20px" height="20px"></iconSvg>{
    
    {
    
     node.label  }}
          </span>              
      </span>
    </el-tree>

カプセル化された iconSvg

<template>
  <svg
    :class="getClassName"
    :width="width"
    :height="height"
    aria-hidden="true">
    <use :xlink:href="getName"></use>
  </svg>
</template>

<script>
  export default {
    
    
    name: 'icon-svg',
    props: {
    
    
      name: {
    
    
        type: String,
        required: true
      },
      className: {
    
    
        type: String
      },
      width: {
    
    
        type: String,
        default:"40px"
      },
      height: {
    
    
        type: String,
        default:"40px"
      }
    },
    computed: {
    
    
      getName () {
    
    
        return `#icon-${
      
      this.name}`
      },
      getClassName () {
    
    
        return [
          'icon-svg',
          `icon-svg__${
      
      this.name}`,
          this.className && /\S/.test(this.className) ? `${
      
      this.className}` : ''
        ]
      }
    }
  }
</script>

<style>
  .icon-svg {
    
    
    fill: currentColor;
    overflow: hidden;
  }
</style>

実現レンダリング
ここに画像の説明を挿入します

各第 1 レベルのディレクトリに異なるアイコンを持たせたい場合は、データの各レベルにアイコン フィールドを直接追加します。

getPerSrcTree: [{
    
    
          label: '一级 1',
          icon:'wendangdakai',
          children: [{
    
    
            label: '二级 1-1',
            children: [{
    
    
              label: '三级 1-1-1'
            }]
          }]
        }, {
    
    
          label: '一级 2',
          children: [{
    
    
            label: '二级 2-1',
            children: [{
    
    
              label: '三级 2-1-1'
            }]
          }, {
    
    
            label: '二级 2-2',
            children: [{
    
    
              label: '三级 2-2-1'
            }]
          }]
        }, {
    
    
          label: '一级 3',
          children: [{
    
    
            label: '二级 3-1',
            children: [{
    
    
              label: '三级 3-1-1'
            }]
          }, {
    
    
            label: '二级 3-2',
            children: [{
    
    
              label: '三级 3-2-1'
            }]
          }]
        }],


<el-tree
    :data="getPerSrcTree"
      default-expand-all
      :expand-on-click-node="false"
      node-key="id"
      ref="atree"
      highlight-current
      :check-strictly="true"
      :props="defaultProps"
      @node-click="nodeClick"
    >
      <span class="custom-tree-node"  slot-scope="{ node,data }">
          <span style="display: flex;align-items: center;">
            <iconSvg :name="data.icon" width="20px" height="20px"></iconSvg>{
    
    {
    
     node.label  }}
          </span>              
      </span>
    </el-tree>

ただし、2 つの異なるアイコンを展開したり折りたたんだりする方法はありません。

解決策 2

CSS スタイルによる変更

<el-tree
    :data="getPerSrcTree"
      default-expand-all
      :expand-on-click-node="false"
      node-key="id"
      ref="atree"
      highlight-current
      :check-strictly="true"
      :props="defaultProps"
      @node-click="nodeClick"
    ></el-tree>

<style  scoped>
.el-tree /deep/ .el-icon-caret-right:before{
    
    
  content: "\e85a";  //在引入的iconfont文件夹找到iconfont.css
  font-size: 25px;
  font-family: "iconfont"; //想要显示icon这个必须加
  color:rgb(43, 206, 229)  //想要的颜色
}
.el-tree /deep/ .el-tree-node__expand-icon.expanded.el-icon-caret-right:before
{
    
    
  content: "\e85b";
  font-size: 25px;
  font-family: "iconfont";
  color:rgb(43, 206, 229)
}
.el-tree /deep/ .el-tree-node__expand-icon.expanded
{
    
    
    -webkit-transform: rotate(0deg);
    transform: rotate(0deg);
}
/* 没有子节点 */
.el-tree /deep/.el-tree-node__expand-icon.is-leaf::before
{
    
    
    font-size: 25px;
    content: '\e85d';
    font-family: "iconfont";
    color:rgb(43, 206, 229)
}
</style>

cssの内容は、インポートしたiconfontフォルダのiconfont.cssから該当する内容を探し、Font-family: "iconfont"; とアイコンが表示されるように記述する必要があります。
ここに画像の説明を挿入します
ここに画像の説明を挿入します

最終的な効果は図の通りで、展開する場合と展開しない場合の2つの表示効果があり、ディレクトリ配下にサブセットがない場合はフォルダアイコンの代わりにファイルアイコンが表示されます。
ここに画像の説明を挿入します

おすすめ

転載: blog.csdn.net/qq_32881447/article/details/114967549