【问题标题】:Font awesome icons in buefybuefy 中的字体真棒图标
【发布时间】:2019-02-28 06:18:51
【问题描述】:

我正在尝试将我的项目从 bulma + jQuery 切换到 buefy。我从 cdn 加载 buefyvuefont awesome(buefy@0.6.7, vue@2.5.17, font awesome 5.2.0)。我对图标的主要问题。我的项目使用了很棒的字体图标。并且默认的 buefy iconPack 是材料设计。它必须支持字体真棒。我尝试更改默认图标包,但没有任何效果:

Vue.use(Buefy.default, {
    defaultIconPack: 'fas',
});

什么都没有:

Vue.use(Buefy, {
    defaultIconPack: 'fas',
});

所以我需要为每个图标明确指出图标包。

第二个问题是,即使在这种情况下,buefy 也会添加我根本不需要的fa-lg。例如对于 b-tab-item 组件

<b-tab-item label="Similarity" icon="search" icon-pack="fas"></b-tab-item>

它呈现:

<i class="fas fa fa-search fa-lg"></i>

是否有可能改变这种 buefy 行为?

【问题讨论】:

    标签: vue.js font-awesome buefy


    【解决方案1】:

    致任何可能仍在为此苦苦挣扎的人。在我的main.js 中使用它解决了我的问题:

    import Buefy from 'buefy'
    import 'buefy/dist/buefy.css'
    
    import { library } from '@fortawesome/fontawesome-svg-core';
    // internal icons
    import { fas } from "@fortawesome/free-solid-svg-icons";
    import { FontAwesomeIcon } from "@fortawesome/vue-fontawesome";
    
    library.add(fas);
    Vue.component('vue-fontawesome', FontAwesomeIcon);
    
    
    Vue.use(Buefy, {
      defaultIconComponent: "vue-fontawesome",
      defaultIconPack: "fas",
      customIconPacks: {
        fas: {
          sizes: {
            default: "lg",
            "is-small": "1x",
            "is-medium": "2x",
            "is-large": "3x"
          },
          iconPrefix: ""
        }
      }
    });
    

    确保使用 npm 安装所有依赖项:

    $ npm i --save @fortawesome/fontawesome-svg-core
    $ npm i --save @fortawesome/free-solid-svg-icons
    $ npm i --save @fortawesome/vue-fontawesome
    

    那么你可以在你的组件中使用它,如下所示:

    <b-icon
      pack="fas"
      icon="user"
      size="is-large"
      type="is-success"></b-icon>
    

    【讨论】:

      【解决方案2】:

      这是我在 buefy 中的工作代码

      在 main.js 中

      import { library } from '@fortawesome/fontawesome-svg-core'
      import { fas } from '@fortawesome/free-solid-svg-icons'
      import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'
      
      library.add(fas)
      Vue.component('font-awesome-icon', FontAwesomeIcon)
      Vue.use(Buefy, { defaultIconPack: 'fas' })
      
      

      index.html

      放在head标签中

      <link
            rel="stylesheet"
            href="https://use.fontawesome.com/releases/v5.6.3/css/all.css"
            integrity="sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/"
            crossorigin="anonymous"
          />
      

      确保首先添加 fortawesome npm 包

      【讨论】:

        【解决方案3】:

        如果你跑:

        yarn add @fortawesome/fontawesome-free
        

        然后导入:

        import '../node_modules/@fortawesome/fontawesome-free/js/all.js'
        

        它应该可以工作。从 CDN 导入似乎不起作用。

        【讨论】:

        • 这应该是被接受的答案,因为它不会强迫用户为 fontawesome 图标创建额外的抽象层,例如创建单独/专用的 font-awesome-icon 组件!
        【解决方案4】:

        进一步原始答案。这是我使用 CDN 的工作代码:

        main.js

        import Vue from 'vue'
        import App from './App.vue'
        import Buefy from 'buefy';
        import 'buefy/dist/buefy.css'
        
        Vue.use(Buefy, {
          defaultIconPack: 'fas'
        });
        
        Vue.config.productionTip = false
        
        new Vue({
          render: h => h(App),
        }).$mount('#app')
        

        模板

        <template>
        
          <div class="container">
            <b-tabs
              is-boxed>
                <b-tab-item label="Search" icon="search"></b-tab-item>
                <b-tab-item label="Music" icon="music"></b-tab-item>
                <b-tab-item label="Videos" icon="video"></b-tab-item>
            </b-tabs>
          </div>
        </template>
        

        index.html

        <!DOCTYPE html>
        <html lang="en">
          <head>
            <meta charset="utf-8">
            <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <meta name="viewport" content="width=device-width,initial-scale=1.0">
            <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" integrity="sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/" crossorigin="anonymous">
            <title>buefy-test</title>
          </head>
          <body>
            <noscript>
              <strong>We're sorry but buefy-test doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
            </noscript>
            <div id="app"></div>
            <!-- built files will be auto injected -->
          </body>
        </html>
        

        【讨论】:

          猜你喜欢
          • 2014-01-16
          • 2015-06-01
          • 2013-12-07
          • 2017-05-02
          • 1970-01-01
          • 2018-09-08
          • 2015-12-13
          • 2013-06-18
          • 2015-07-16
          相关资源
          最近更新 更多