gpt4 book ai didi

javascript - 无法在 Nextjs 中加载 Expo 矢量图标

转载 作者:行者123 更新时间:2023-12-05 00:33:20 25 4
gpt4 key购买 nike

我试过 批号多天的不同方法来做到这一点,绝对零运气。
我正在尝试使用 Solito Nativebase Universal Typescript 存储库来执行此操作:
https://github.com/GeekyAnts/nativebase-templates/tree/master/solito-universal-app-template-nativebase-typescript
我已阅读并尝试了此页面上的所有内容至少十几次:
https://github.com/GeekyAnts/nativebase-templates/issues/43
我的当前next.config.js文件如下所示:

/** @type {import('next').NextConfig} */

const { withNativebase } = require('@native-base/next-adapter')
const withImages = require('next-images')
const { withExpo } = require('@expo/next-adapter')
const withFonts = require('next-fonts')

module.exports = withNativebase({
dependencies: [
'@expo/next-adapter',
'next-images',
'react-native-vector-icons',
'react-native-vector-icons-for-web',
'solito',
'app',
],
plugins: [
[withFonts, { projectRoot: __dirname }],
withImages,
[withExpo, { projectRoot: __dirname }],
],
nextConfig: {
images: {
disableStaticImages: true,
},
projectRoot: __dirname,
reactStrictMode: true,
webpack5: true,
webpack: (config, options) => {
config.resolve.alias = {
...(config.resolve.alias || {}),
'react-native$': 'react-native-web',
'@expo/vector-icons': 'react-native-vector-icons',
}
config.resolve.extensions = [
'.web.js',
'.web.ts',
'.web.tsx',
...config.resolve.extensions,
]
return config
},
},
})
我也尝试过使用 @native-base/icons ,再次,没有运气。
我的最终用例是这样的:
export const Cart = (props: IIconStyles) => {
return (
<Icon
as={FontAwesome5}
name="shopping-cart"
size={props.size ? props.size : 6}
color="gray.200"
/>
)
理论上它应该显示一个购物车,但相反,这就是我所看到的:
question mark icon
很明显,有一些字体问题或其他问题阻止它加载到实际的 SVG 中。
我不知道这是什么 - 我尝试像这样重写我的 _document.tsx 文件:
https://docs.nativebase.io/nb-icons
我已尝试将此添加到我的 next.config.js :
 config.module.rules.push({
test: /\.ttf$/,
loader: "url-loader", // or directly file-loader
include: path.resolve(__dirname, "node_modules/@native-base/icons"),
});
当我尝试做这样的事情时:
import fontsCSS from '@native-base/icons/FontsCSS';
在我的 _document.tsx文件,我收到以下错误:
Module not found: Can't resolve '@native-base/icons/lib/FontsCSS'
尽管我已经在我的 package.json 中安装了 @native-base/icons,并且按照上面的说明链接将它放在了我的 Babel 文件中。
如何让矢量图标在 Next 中工作?
请注意,这是 具体 Next/Expo/React Native

最佳答案

您可以阅读有关 next-adapter-icons 设置的更多信息here .
我用以下方法让它工作,

  • next.config.js
  • const { withNativebase } = require("@native-base/next-adapter");
    const path = require("path");

    module.exports = withNativebase({
    dependencies: ["@native-base/icons", "react-native-web-linear-gradient"],
    nextConfig: {
    webpack: (config, options) => {
    config.module.rules.push({
    test: /\.ttf$/,
    loader: "url-loader", // or directly file-loader
    include: path.resolve(__dirname, "node_modules/@native-base/icons"),
    });
    config.resolve.alias = {
    ...(config.resolve.alias || {}),
    "react-native$": "react-native-web",
    "react-native-linear-gradient": "react-native-web-linear-gradient",
    "@expo/vector-icons": "react-native-vector-icons",
    };
    config.resolve.extensions = [
    ".web.js",
    ".web.ts",
    ".web.tsx",
    ...config.resolve.extensions,
    ];
    return config;
    },
    },
    });
  • pages/_document.js
  • import React from 'react';
    import { DocumentContext, DocumentInitialProps } from 'next/document';
    import { default as NativebaseDocument } from '@native-base/next-adapter/document'

    // Icon Font Library Imports
    import MaterialIconsFont from '@native-base/icons/FontsCSS/MaterialIconsFontFaceCSS';
    import EntypoFontFaceCSS from '@native-base/icons/FontsCSS/EntypoFontFaceCSS';
    const fontsCSS = `${MaterialIconsFont} ${EntypoFontFaceCSS}`;

    export default class Document extends NativebaseDocument {

    static async getInitialProps(ctx) {
    const props = await super.getInitialProps(ctx);
    const styles = [
    <style key={'fontsCSS'} dangerouslySetInnerHTML={{ __html: fontsCSS }} />,
    ...props.styles,
    ]
    return { ...props, styles: React.Children.toArray(styles) }
    }
    }
  • pages/index.tsx
  • import React from "react";
    import { Box, Icon } from "native-base";
    import Entypo from "@expo/vector-icons/Entypo";

    export default function App() {
    return (
    <Box>
    <Icon
    as={Entypo}
    name="user"
    color="coolGray.800"
    _dark={{
    color: "warmGray.50",
    }}
    />
    </Box>
    );
    }

    关于javascript - 无法在 Nextjs 中加载 Expo 矢量图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73200647/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com