<template>
<div id="app">
<h1 class="custom-font">这是一个使用自定义字体的标题</h1>
</div>
</template>
<script>
export default {
name: 'App',
// 其他组件选项...
};
</script>
<style>
/* 引入自定义字体 */
@font-face {
font-family: 'MyCustomFont';
src: url('./assets/MyCustomFont.woff2') format('woff2'), /* 最优先级 */
url('./assets/MyCustomFont.woff') format('woff'), /* 其次优先级 */
url('./assets/MyCustomFont.ttf') format('truetype'); /* 最后优先级 */
font-weight: normal;
font-style: normal;
font-display: swap; /* 字体加载时显示系统字体,提升用户体验 */
}
/* 应用自定义字体 */
.custom-font {
font-family: 'MyCustomFont', sans-serif;
}
</style>
这个代码实例展示了如何在Vue组件中通过@font-face
在网页中引入自定义字体,并将其应用到指定的CSS类上。font-display: swap;
是一个提升字体加载性能的选项,它会在自定义字体加载过程中使用系统字体,从而避免文本不可见的情况。