在Vue 3中使用令人敬畏的字体
我正在尝试对Vue 3使用Font Awesom。
我的package.json
"dependencies": {
"@fortawesome/fontawesome-svg-core": "^1.2.34",
"@fortawesome/free-brands-svg-icons": "^5.15.2",
"@fortawesome/free-solid-svg-icons": "^5.15.2",
"@fortawesome/vue-fontawesome": "^3.0.0-3",
}
导入main.js
中的FontAwese
import { createApp } from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import { library } from "@fortawesome/fontawesome-svg-core";
import { faPhone } from "@fortawesome/free-solid-svg-icons/faAddressBook";
import { FontAwesomeIcon } from "@fortawesome/vue-fontawesome";
library.add(faPhone);
/* eslint-disable */
createApp(App)
.use(store)
.use(router)
.component("font-awesome-icon", FontAwesomeIcon)
.mount("#app")
和<template>
中的此内部组件
<font-awesome-icon :icon="['fas', 'faPhone']" />
但是当我在组件中使用它时,没有发生任何事情.in元素是doent的呈现内容,它只显示HTML注释<!---->
如何解决此问题并开始在任何组件中使用FontAwese?
解决方案
这些步骤让它为我工作:
安装
prelease
(3.0.0-4
),vue-fontawesome
兼容Vue 3,图标依赖:npm i --save @fortawesome/vue-fontawesome@prerelease npm i --save @fortawesome/fontawesome-svg-core npm i --save @fortawesome/free-solid-svg-icons
在
main.js
中,从@fortawesome/free-solid-svg-icons
中选择要加载的图标:import { library } from "@fortawesome/fontawesome-svg-core"; import { faPhone } from "@fortawesome/free-solid-svg-icons"; library.add(faPhone);
全局注册
font-awesome-icon
组件:import { FontAwesomeIcon } from "@fortawesome/vue-fontawesome"; createApp(App) .component("font-awesome-icon", FontAwesomeIcon) .mount("#app");
在
src/App.vue
中,使用如下组件(注意图标名称是phone
,而不是faPhone
):<!-- explicit style --> <font-awesome-icon :icon="['fas', 'phone']" /> <!-- implicit style (fas is assumed) --> <font-awesome-icon icon="phone" />
demo
相关文章