vue.js - 在 Vue 中动态导入 fontawesome 图标

标签 vue.js font-awesome

我在动态渲染图标时遇到问题。我使用 v-for 从对象数组中获取所有数据。另外,我还有第二个数组,用于保存我使用的图标的名称。但是,当第一个数组循环时,第二个数组(图标)不会移动。

我尝试创建一种方法来映射第一个和第二个数组中的数据以创建一个新数组。但什么也没发生。

我的代码:

Component.vue

<template>
  <div class="items">
    <div class="item" v-for="(param, index) in params" :key="index">
      <font-awesome-icon :icon="['fab', 'temp']" :temp="getIcon" :key="index" class="fab fa" />
      <h3 class="skills-title">{{ param.name }}.</h3>
      <p style="display: none">{{ param.description }}.</p>
    </div>
  </div>
</template>

<script>
export default {
  name: "PxSkillCard",
  data() {
    return {
      params: [],
      icons: ["laravel", "wordpress-simple"],
    };
  },

  methods: {
    getIcon() {
      let temp = this.params.map((aux, index) => {
        return [aux, this.icons[index]];
      });
    },
  },
};
</script>

我将 fontawesome 文件分离到一个单独的模块中 fontawesome.js

import Vue from "vue";
import { library } from "@fortawesome/fontawesome-svg-core";
import {
  faLaravel,
  faWordpressSimple
} from "@fortawesome/free-brands-svg-icons";

import { faPlus } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/vue-fontawesome";

library.add(
  faLaravel,
  faWordpressSimple
);

Vue.component("font-awesome-icon", FontAwesomeIcon);

最终结果是: enter image description here

我的代码(或我的逻辑)怎么样?

最佳答案

您已经循环了模板中的所有内容,无需在函数中再次循环。

像这样的东西应该有效。

<template>
  <div class="items">
    <div class="item" v-for="(param, index) in params" :key="index">
      <font-awesome-icon :icon="['fab', icons[index]]" :key="index" class="fab fa" />
      <h3 class="skills-title">{{ param.name }}.</h3>
      <p style="display: none">{{ param.description }}.</p>
    </div>
  </div>
</template>

<script>
export default {
  name: "PxSkillCard",
  data() {
    return {
      params: [],
      icons: ["laravel", "wordpress-simple"],
    };
  },
};
</script>

假设两个数组大小相同,并且参数和图标中的数据顺序正确。

关于vue.js - 在 Vue 中动态导入 fontawesome 图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68885368/

相关文章:

html - ERR_NAME_NOT_RESOLVED 使用 fontawesome

javascript - Font Awesome : retrieve css of pseudo element displays a small rectangle

jQuery:更改所有 li 图标的 CSS 类,除了选定的 li 图标

javascript - Vue.js 2 和 Axios 范围问题

html - <b-form-select-option> 在 Bootstrap Vue 中不起作用

javascript - 如何使组件出现并将键盘焦点设置到 iOS Safari 中的输入字段?

javascript - Bootstrap 下拉按钮高度不同

javascript - Vuejs : loading topojson from store and plot with d3

vue.js - 如何增加v-data-table标题中的字体大小

java - FontAwesome 图标不会显示在 Android 应用程序上