vue.js 你怎么得到字体真棒设置在nuxt3

kokeuurv  于 2023-01-09  发布在  Vue.js
关注(0)|答案(4)|浏览(252)

当我尝试在我的nuxt 3应用程序中呈现这个字体真棒组件时,我得到了下面的错误,遵循了所有的npm安装正确,无法在应用程序中获得图标,没有任何错误。

at WeakMap.set (<anonymous>)  
  at normalizePropsOptions (./node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:3966:11)  
  at createComponentInstance (./node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:6957:23)  
  at renderComponentVNode (./node_modules/@vue/server-renderer/dist/server-renderer.cjs.js:171:22)  
  at Module.ssrRenderComponent (./node_modules/@vue/server-renderer/dist/server-renderer.cjs.js:608:12)  
  at _sfc_ssrRender (./.nuxt/dist/server/server.mjs:2939:33)  
  at renderComponentSubTree (./node_modules/@vue/server-renderer/dist/server-renderer.cjs.js:253:13)  
  at renderComponentVNode (./node_modules/@vue/server-renderer/dist/server-renderer.cjs.js:188:16)  
  at Module.ssrRenderComponent (./node_modules/@vue/server-renderer/dist/server-renderer.cjs.js:608:12)  
  at _sfc_ssrRender (./.nuxt/dist/server/server.mjs:9374:31)

这是中断应用程序的代码行

<font-awesome-icon :icon="['fas', 'user']" />

nuxt.config文件

import { defineNuxtConfig } from "nuxt";

export default defineNuxtConfig({
    css: ["@/assets/css/tailwind.css"],
    build: {
        postcss: {
            postcssOptions: {
                plugins: {
                    tailwindcss: {},
                    autoprefixer: {},
                },
            },
        },
    },
    plugins: ["~/plugins/fontawesome.js"],
    buildModules: [
        ["@pinia/nuxt", { disableVuex: true }],
        ["@nuxtjs/tailwindcss", { config: "@/tailwind.config.js" }],
    ],
    modules: ["@nuxtjs/tailwindcss"],
});

插件文件

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

export default defineNuxtPlugin((nuxtApp) => {
    // nuxtApp.vueApp.component("FontAwesomeIcon", FontAwesomeIcon);
    library.add(fas);
});

我使用这个插件是因为不管什么原因设置npm @nuxt/fortawesome都不起作用
package.json

"dependencies": {
        "@fortawesome/fontawesome-svg-core": "^6.1.1",
        "@fortawesome/free-brands-svg-icons": "^6.1.1",
        "@fortawesome/free-solid-svg-icons": "^6.1.1",
        "@fortawesome/vue-fontawesome": "^2.0.8",
        "@supabase/supabase-js": "^1.35.3",
        "pinia": "^2.0.14"
    }
}
wgmfuz8q

wgmfuz8q1#

使用我的solution here,基于unplugin-icons能够解决OP的问题。
最后,像这样的东西会产生完全相同的结果,但它是通用的,高性能的,而且你只需要对任何类型的未来库做一次结果。

<script setup>
import IconXmark from `~icons/fa6-solid/xmark`
</script>

<template>
  <icon-xmark style="font-size: 2em; color: blue" />
</template>
t40tm48m

t40tm48m2#

安装软件包

pnpm i @fortawesome/fontawesome-svg-core
pnpm i @fortawesome/free-solid-svg-icons
pnpm i @fortawesome/free-regular-svg-icons
pnpm i @fortawesome/free-brands-svg-icons

添加此插件

import { library } from '@fortawesome/fontawesome-svg-core'
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'
import { faSms } from '@fortawesome/free-solid-svg-icons'
import { faFacebook, faTwitter } from '@fortawesome/free-brands-svg-icons'

library.add(faTwitter)
library.add(faFacebook)
library.add(faSms)

export default defineNuxtPlugin((nuxtApp) => {
  nuxtApp.vueApp.component('font-awesome-icon', FontAwesomeIcon)
})

好好享受

<template>
  <font-awesome-icon icon="fa-brand fa-whatsapp" />
</template>
gstyhher

gstyhher3#

我正在使用:

npm i @fortawesome/fontawesome-free

在我的资产文件夹中导入:

//  /assets/main.css
@import "@fortawesome/fontawesome-free/css/all.min.css";

注意:您需要确切的路径。
现在注册:

// nuxt.config.ts

import { defineNuxtConfig } from "nuxt";
export default defineNuxtConfig({
  css: ["~/assets/main.css"],
});

是的,你可以开始了。你可以去fontawesome,搜索一个图标,然后复制粘贴它:

<i class="fa-solid fa-user"></i>

那你应该能看到。

des4xlb0

des4xlb04#

这与最新的更新不一致。下面是你应该如何在Nuxt3上安装font-awesome。
1.* * 安装**

npm i -D @fortawesome/fontawesome-svg-core
npm i -D @fortawesome/free-solid-svg-icons
npm i -D @fortawesome/free-regular-svg-icons
npm i -D @fortawesome/free-brands-svg-icons
npm i -D @fortawesome/vue-fontawesome@latest-3    <=== Extremely Critical

1.* * 新配置**

export default defineNuxtConfig({
  plugins: [
    { src: '~/plugins/font-awesome.ts' },
  ],
})

1.* * 插件配置**

import {library, config} from '@fortawesome/fontawesome-svg-core'
import {FontAwesomeIcon} from '@fortawesome/vue-fontawesome'
import {
  faHospital as farHospital
} from '@fortawesome/free-regular-svg-icons'
import {
  faCircleUser as fasCircleUser,
  faBars as fasBars,
  faHospital as fasHospital
} from '@fortawesome/free-solid-svg-icons'
import {faDiscord as fabDiscord} from '@fortawesome/free-brands-svg-icons'

// This is important, we are going to let Nuxt worry about the CSS
config.autoAddCss = false

// You can add your icons directly in this plugin. See other examples for how you
// can add other styles or just individual icons.
library.add(
  farHospital,
  fasHospital,
  fasBars,
  fabDiscord
)

export default defineNuxtPlugin(({vueApp}) => {
  vueApp.component('FontAwesomeIcon', FontAwesomeIcon)
})

1.* * 测试渲染**

<template>
  <div>
    <font-awesome-icon :icon="['fas', 'hospital']" />
    <font-awesome-icon :icon="['far', 'hospital']" />
    <font-awesome-icon :icon="['fas', 'bars']" />
  </div>
<template>

相关问题