在Vue 3中使用令人敬畏的字体

2022-02-24 00:00:00 javascript vue.js vue-component vuejs3

我正在尝试对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?


解决方案

这些步骤让它为我工作:

  1. 安装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
    
  2. 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);
    
  3. 全局注册font-awesome-icon组件:

    import { FontAwesomeIcon } from "@fortawesome/vue-fontawesome";
    
    createApp(App)
      .component("font-awesome-icon", FontAwesomeIcon)
      .mount("#app");
    
  4. src/App.vue中,使用如下组件(注意图标名称是phone,而不是faPhone):

    <!-- explicit style -->
    <font-awesome-icon :icon="['fas', 'phone']" />
    
    <!-- implicit style (fas is assumed) -->
    <font-awesome-icon icon="phone" />
    

demo

相关文章