【web前端】element-ui框架在线上环境偶尔出现iconfont图标乱码的问题,解决办法

使用element-UI的项目,打包部署以后,偶尔会出现iconfont显示乱码


iconfont显示为乱码

但是刷新以后就变成正常了。

经过研究以后发现,图标字体的content经过scss编译以后变成了这样:


content变成问号

不过正常情况下图标还是会显示的,但是偶尔进入页面后,所有的iconfont图标会变成乱码。

我解决这个问题的办法就是直接引入iconfont的css文件,不经过scss的编译,图标的content的就会是原来的内容,一个转义字符加上数字字母。像这样:


content正常

具体做法就是
1.找到项目里自定义element-ui主题的文件element-variables.scss
在这个文件里本来有一个引入所有element组件scss文件的一行

@import '~element-ui/packages/theme-chalk/src/index';

把这一行去掉,然后直接element-variables.scss在引入组件的scss文件,当然要去掉iconfont的scss
这里是所有组件,你也可以按需引入:

@import '~element-ui/packages/theme-chalk/src/pagination.scss';
@import '~element-ui/packages/theme-chalk/src/dialog.scss';
@import '~element-ui/packages/theme-chalk/src/autocomplete.scss';
@import '~element-ui/packages/theme-chalk/src/dropdown.scss';
@import '~element-ui/packages/theme-chalk/src/dropdown-menu.scss';
@import '~element-ui/packages/theme-chalk/src/dropdown-item.scss';
@import '~element-ui/packages/theme-chalk/src/menu.scss';
@import '~element-ui/packages/theme-chalk/src/submenu.scss';
@import '~element-ui/packages/theme-chalk/src/menu-item.scss';
@import '~element-ui/packages/theme-chalk/src/menu-item-group.scss';
@import '~element-ui/packages/theme-chalk/src/input.scss';
@import '~element-ui/packages/theme-chalk/src/input-number.scss';
@import '~element-ui/packages/theme-chalk/src/radio.scss';
@import '~element-ui/packages/theme-chalk/src/radio-group.scss';
@import '~element-ui/packages/theme-chalk/src/radio-button.scss';
@import '~element-ui/packages/theme-chalk/src/checkbox.scss';
@import '~element-ui/packages/theme-chalk/src/checkbox-button.scss';
@import '~element-ui/packages/theme-chalk/src/checkbox-group.scss';
@import '~element-ui/packages/theme-chalk/src/switch.scss';
@import '~element-ui/packages/theme-chalk/src/select.scss';
@import '~element-ui/packages/theme-chalk/src/button.scss';
@import '~element-ui/packages/theme-chalk/src/button-group.scss';
@import '~element-ui/packages/theme-chalk/src/table.scss';
@import '~element-ui/packages/theme-chalk/src/table-column.scss';
@import '~element-ui/packages/theme-chalk/src/date-picker.scss';
@import '~element-ui/packages/theme-chalk/src/time-select.scss';
@import '~element-ui/packages/theme-chalk/src/time-picker.scss';
@import '~element-ui/packages/theme-chalk/src/popover.scss';
@import '~element-ui/packages/theme-chalk/src/tooltip.scss';
@import '~element-ui/packages/theme-chalk/src/message-box.scss';
@import '~element-ui/packages/theme-chalk/src/breadcrumb.scss';
@import '~element-ui/packages/theme-chalk/src/breadcrumb-item.scss';
@import '~element-ui/packages/theme-chalk/src/form.scss';
@import '~element-ui/packages/theme-chalk/src/form-item.scss';
@import '~element-ui/packages/theme-chalk/src/tabs.scss';
@import '~element-ui/packages/theme-chalk/src/tab-pane.scss';
@import '~element-ui/packages/theme-chalk/src/tag.scss';
@import '~element-ui/packages/theme-chalk/src/tree.scss';
@import '~element-ui/packages/theme-chalk/src/alert.scss';
@import '~element-ui/packages/theme-chalk/src/notification.scss';
@import '~element-ui/packages/theme-chalk/src/slider.scss';
@import '~element-ui/packages/theme-chalk/src/loading.scss';
@import '~element-ui/packages/theme-chalk/src/row.scss';
@import '~element-ui/packages/theme-chalk/src/col.scss';
@import '~element-ui/packages/theme-chalk/src/upload.scss';
@import '~element-ui/packages/theme-chalk/src/progress.scss';
@import '~element-ui/packages/theme-chalk/src/spinner.scss';
@import '~element-ui/packages/theme-chalk/src/message.scss';
@import '~element-ui/packages/theme-chalk/src/badge.scss';
@import '~element-ui/packages/theme-chalk/src/card.scss';
@import '~element-ui/packages/theme-chalk/src/rate.scss';
@import '~element-ui/packages/theme-chalk/src/steps.scss';
@import '~element-ui/packages/theme-chalk/src/step.scss';
@import '~element-ui/packages/theme-chalk/src/carousel.scss';
@import '~element-ui/packages/theme-chalk/src/scrollbar.scss';
@import '~element-ui/packages/theme-chalk/src/carousel-item.scss';
@import '~element-ui/packages/theme-chalk/src/collapse.scss';
@import '~element-ui/packages/theme-chalk/src/collapse-item.scss';
@import '~element-ui/packages/theme-chalk/src/cascader.scss';
@import '~element-ui/packages/theme-chalk/src/color-picker.scss';
@import '~element-ui/packages/theme-chalk/src/transfer.scss';
@import '~element-ui/packages/theme-chalk/src/container.scss';
@import '~element-ui/packages/theme-chalk/src/header.scss';
@import '~element-ui/packages/theme-chalk/src/aside.scss';
@import '~element-ui/packages/theme-chalk/src/main.scss';
@import '~element-ui/packages/theme-chalk/src/footer.scss';
@import '~element-ui/packages/theme-chalk/src/timeline.scss';
@import '~element-ui/packages/theme-chalk/src/timeline-item.scss';
@import '~element-ui/packages/theme-chalk/src/link.scss';
@import '~element-ui/packages/theme-chalk/src/divider.scss';
@import '~element-ui/packages/theme-chalk/src/image.scss';
@import '~element-ui/packages/theme-chalk/src/calendar.scss';
@import '~element-ui/packages/theme-chalk/src/backtop.scss';
@import '~element-ui/packages/theme-chalk/src/infinite-scroll.scss';
@import '~element-ui/packages/theme-chalk/src/page-header.scss';
@import '~element-ui/packages/theme-chalk/src/cascader-panel.scss';
@import '~element-ui/packages/theme-chalk/src/avatar.scss';
@import '~element-ui/packages/theme-chalk/src/drawer.scss';
@import '~element-ui/packages/theme-chalk/src/popconfirm.scss';
@import '~element-ui/packages/theme-chalk/src/common/transition.scss';

这些就是对element-variables.scss文件的修改,

2.然后就是重新引入iconfont的css文件,
在element-UI里找到已经编译好的iconfont的文件

.\node_modules\element-ui\lib\theme-chalk

在main.js里引入这个文件夹下的icon.css就行了。

import 'element-ui/lib/theme-chalk/icon.css'

这样content里面就是正常内容了,字体图标也不会乱码了。

你可能感兴趣的:(【web前端】element-ui框架在线上环境偶尔出现iconfont图标乱码的问题,解决办法)