简体   繁体   English

如何在Vue.js中添加不同的字体真棒图标

[英]How to add different font awesome icons in Vue.js

I am modeling an app section to display three different tabs with three different Font Awesome icons. 我正在建模一个应用程序部分,以显示带有三个不同的Font Awesome图标的三个不同的选项卡。 So far, the tabs are set in the following way and the application shows only one icon in tab: 到目前为止,选项卡的设置方式如下,应用程序在选项卡中仅显示一个图标:

   <div class="tab">

    <ul class="nav nav-tabs" role="tablist">
           <li v-for="(tab,index) in tabs" :class="{active : curentTab===index}" 
            @click="curentTab=index">
         <a href="#">
       <i class="fa fa-bullhorn" style="width:auto" aria-hidden="true"></i> {{tab}}
         </a></li>
      </ul>

 </div>

Here follows the Vue application snippet to model the tabs. 下面是Vue应用程序片段以对选项卡进行建模。

   <script>
   data() {
    return {
      curentTab:0,
      tabs:['Tab1','Tab2 ','Tab3']
    }
  }
  </script>

How can I show different icons for different tabs? 如何为不同的标签显示不同的图标?

Thanks in advance! 提前致谢!

To render each tab with its specific icon you could modify your data model by introducing a different icon for each tab. 要使用其特定图标呈现每个选项卡,您可以通过为每个选项卡引入一个不同的图标来修改数据模型。 For example, like this: 例如,像这样:

tabs:[
  {label: 'Tab1', icon:'smile'},
  {label: 'Tab2', icon:'bullhorn'},
  {label: 'Tab3', icon:'heart'}
]

Also, you have to modify your template by binding the i CSS class accordingly. 另外,您必须通过相应地绑定i CSS类来修改template To do so, you can: 为此,您可以:

  1. introduce a Vue method returning the proper CSS class corresponding to the FontAwesome one, eg: 引入一种Vue方法,该方法返回与FontAwesome对应的正确CSS类,例如:
    methods: {
         faClass(icon) {
            return `fa fa-${icon}`;
          }
        }
  1. invoke this new method in your template , like this: 在您的template调用此新方法,如下所示:
<i :class="[faClass(tab.icon)]" style="width:auto" aria-hidden="true">

Here you can see how class is now bound through Vue itself. 在这里,您可以看到现在如何通过Vue本身绑定class


So, going back to your example, you will have: 因此,回到您的示例,您将获得:

HTML template HTML模板

<div id='app'>
  <ul class="nav nav-tabs" role="tablist">
           <li v-for="(tab,index) in tabs" :class="{active : curentTab===index}" 
            @click="curentTab=index">
         <a href="#">
       <i :class="[faClass(tab.icon)]" 
           style="width:auto" 
           aria-hidden="true"></i> {{tab.label}}
         </a></li>
      </ul>
</div>

Vue instance Vue实例

    ...
    data: function() {
       return {
        curentTab:0,
        tabs:[
         {label: 'Tab1', icon:'smile'},
         {label: 'Tab2', icon:'bullhorn'},
         {label: 'Tab3', icon:'heart'}
       ]
      }
     },
      methods: {
        faClass(icon) {
          return 'fa fa-'+icon;
        }
      }
    ...

Check this CodePen to see it in action. 检查此CodePen,以查看其作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM