【发布时间】:2020-05-13 13:28:48
【问题描述】:
我正在尝试通过运行以下命令使用 vue cli 3 创建 Web 组件
vue-cli-service build --target wc --name wc-demo 'src/App.vue'
我在网页组件中加载字体和材质图标时遇到问题。
我在 App.vue 的 style 标签内导入 roboto-font.scss 和 material-icon.scss 这是创建的入口网络组件。
<style lang='scss'>
// @import url('https://fonts.googleapis.com/css?family=Roboto:400,700&display=swap');
@import "~@/assets/fonts/roboto-font/roboto-font.scss";
@import "~@/assets/fonts/material-icons/material-icons.scss";
</style>
Material-icon.scss
$font-dir: "assets/fonts/material-icons/web-font/";
@font-face {
font-family: 'Material Icons';
font-style: normal;
font-weight: 400;
// src: url('https://fonts.gstatic.com/s/materialicons/v48/flUhRq6tzZclQEJ-Vdg-IuiaDsNcIhQ8tQ.woff2') format("woff2");
src: url("#{$font-dir}flUhRq6tzZclQEJ-Vdg-IuiaDsNcIhQ8tQ.woff2") format("woff2"),
url("#{$font-dir}flUhRq6tzZclQEJ-Vdg-IuiaDsNa.woff") format("woff");
}
.material-icons {
font-family: 'Material Icons';
font-weight: normal;
font-style: normal;
display: inline-block;
line-height: 1;
text-transform: none;
letter-spacing: normal;
word-wrap: normal;
white-space: nowrap;
direction: ltr;
}
Roboto-font.scss
$font-dir: "assets/fonts/roboto-font/web-font/";
@font-face {
font-family: 'Roboto';
font-style: normal;
font-weight: 100;
src: url("#{$font-dir}KFOkCnqEu92Fr1MmgVxIIzQ.woff") format("woff");
}
使用
运行项目时一切正常纱线发球
。字体在网络选项卡中加载正常,但在构建 Web 组件并使用“live server”将其加载到浏览器中后,字体不会在网络选项卡中加载。
让字体和图标工作的唯一方法是向 index.html 文件添加链接标签。但不在 web 组件内(在 shadow Dom 内)
<!DOCTYPE html>
<html lang="en">
<head>
<title>wc-demo</title>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<link
href="https://fonts.googleapis.com/icon?family=Material+Icons"
rel="stylesheet"
/>
<link
href="https://fonts.googleapis.com/css?family=Roboto&display=swap"
rel="stylesheet"
/>
<title>Document</title>
</head>
<body>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src=“./wc-demo.js"></script>
<wc-demo></wc-demo>
</body>
</html>
谁能帮我理解如何在 web 组件中包含字体或在 html 文件头部分中使用它是唯一的选择吗?
我还链接了一些类似的问题,可能会帮助你帮助像我这样的人:-
https://bugs.chromium.org/p/chromium/issues/detail?id=336876
@font-face not working in polymer/web-components
https://robdodson.me/at-font-face-doesnt-work-in-shadow-dom/
【问题讨论】:
标签: fonts font-face web-component