


What should I do if the font icon is not displayed after uniapp is packaged?
In recent years, with the continuous development of front-end technology, font icons, as a lightweight icon solution, have been widely used in web and mobile design. When using uniapp to develop mobile applications, we can also easily use font icons, but there is often a problem that the font icons cannot be displayed properly after packaging. Today, we will discuss the reasons and solutions for why the font icon does not display after uniapp is packaged.
Cause analysis
1. The font file was not loaded successfully
First of all, we need to understand that uniapp will generate a dist directory after packaging, and the content in this directory will be our final Published applications include various resource files and HTML files. When we use font icons, we actually use CSS styles in the HTML file and set the data source to the font file of the font icon, so the problem may occur in the HTML file or font file.
The most common situation is that the font file is not loaded successfully, resulting in the font icon not being displayed. You can view the console output through the F12 developer tools. If there is a 404 or network error and the font file is in a failed request status, it can be determined that the font file was not loaded successfully.
2. Address error
Another possibility is that the address we introduced the font is wrong. Because uniapp uses relative paths to introduce resource files, you need to ensure that the HTML file and the font file are in the same file. Clip it down. If a file path error occurs, the font icon will not be displayed properly. Similarly, you can determine whether there is a path error through the console output.
Solution
1. Add local font resources
If the font file is not loaded successfully, we can try to add the font file to the local resource and then add it to the HTML file Use local relative paths for reference.
- Create a new
fonts
folder in the project root directory and extract the downloaded font files into the folder. -
Enter the
uni.scss
file and introduce the font file, as shown below:@font-face{ font-family:'iconfont'; src:url(../fonts/iconfont.ttf) format('truetype'); }
You need to pay attention here,
@font-face
The name offont-family
needs to be consistent with the font name used in HTML. -
Use the font icon in the HTML file as follows:
<i></i>
Here the
uni-icon-wode
is what we have in the font file Customized icon class name, anduni-icon
is the base class defined in uni.scss.
If this method solves the problem that the font icon cannot be displayed, then congratulations, the problem has been solved. However, sometimes even if we have followed the above methods, the font icon still cannot be displayed normally. In this case, you can try the following method.
2. Use cdn
Sometimes we find that the local reference of the font file fails, then we can consider using cdn to solve the problem. However, when using CDN, we need to ensure that the CDN address we introduce is correct, and since the CDN address may be affected by the network, it is best to provide multiple alternatives. The modification method is as follows:
-
In the
manifest.json
file, add the resource address of the font file, as follows:"networkTimeout": { "request": 5000, "downloadFile": 10000 }, "onDemandResourceRules": [ { "host": "xxxxx.com", "files": [ "/fonts/iconfont.ttf" ] } ], "preloadRule": { "async": [ {"path": "xxxxx.com/fonts/iconfont.ttf"} ], "sync": [ ] }
where
xxxxx.com
Specify the domain name of the cdn for us. -
Use the cdn address in the
uni.scss
file as follows:@font-face{ font-family:'iconfont'; src:url(//xxxxx.com/fonts/iconfont.ttf) format('truetype'); }
The
//
here means The relative path with the same protocol is used, suitable for http, https, etc. -
Use the font icon in the HTML file as follows:
<i></i>
Similarly, the
uni-icon-wode
here is what we have in the font The customized icon class name in the file.
It should be noted that using CDN may also encounter other problems, such as unstable connections, files exceeding cache limits, etc., so more testing and backup plans are required.
In general, font icons are a good and lightweight icon solution when using uniapp to develop mobile applications. However, due to issues such as the complexity of packaging and the way resources are handled, it may There may be situations where the font icon cannot be displayed properly. The above two methods can help us solve these problems and make our applications more beautiful and practical.
The above is the detailed content of What should I do if the font icon is not displayed after uniapp is packaged?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 English version
Recommended: Win version, supports code prompts!

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver CS6
Visual web development tools

Atom editor mac version download
The most popular open source editor
