简体   繁体   English

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

[英]Can't load Expo Vector Icons in Nextjs

I have tried a lot of different ways to do this, with absolutely zero luck, over multiple days.我已经尝试了很多不同的方法来做到这一点,运气绝对为零,在多天的时间里。

I am trying to use Solito Nativebase Universal Typescript repo to do this:我正在尝试使用 Solito Nativebase Universal Typescript repo 来执行此操作:

https://github.com/GeekyAnts/nativebase-templates/tree/master/solito-universal-app-template-nativebase-typescript https://github.com/GeekyAnts/nativebase-templates/tree/master/solito-universal-app-template-nativebase-typescript

I have read, and tried everything on this page at least a dozen times:我已阅读并尝试了此页面上的所有内容至少十几次:

https://github.com/GeekyAnts/nativebase-templates/issues/43 https://github.com/GeekyAnts/nativebase-templates/issues/43

My current next.config.js file looks like this:我当前的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
    },
  },
})

I have also tried using @native-base/icons , again, no luck.我也尝试过使用@native-base/icons ,再次,没有运气。

My end use case is this:我的最终用例是这样的:

export const Cart = (props: IIconStyles) => {
  return (
    <Icon
      as={FontAwesome5}
      name="shopping-cart"
      size={props.size ? props.size : 6}
      color="gray.200"
    />
  )

Theoretically it SHOULD show a shopping cart, but instead, this is what I see:理论上它应该显示一个购物车,但相反,这就是我所看到的:

问号图标

So clearly there's some font issue or other issue that is preventing it from loading in the actual SVG.很明显,有一些字体问题或其他问题阻止它加载到实际的 SVG 中。

I can't figure out what this is - I've tried rewriting my _document.tsx file like this:我不知道这是什么 - 我尝试像这样重写我的 _document.tsx 文件:

https://docs.nativebase.io/nb-icons https://docs.nativebase.io/nb-icons

I've tried adding this to my next.config.js :我尝试将此添加到我的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"),
 });

When I try to do something like this:当我尝试做这样的事情时:

import fontsCSS from '@native-base/icons/FontsCSS';

in my _document.tsx file, I get the following error:在我的_document.tsx文件中,我收到以下错误:

Module not found: Can't resolve '@native-base/icons/lib/FontsCSS'

Despite the fact that I've got @native-base/icons installed in my package.json, as well as having it in my Babel file per the instruction link above.尽管我已经在我的 package.json 中安装了@native-base/icons,并且按照上面的指令链接将它放在我的 Babel 文件中。

How do I get vector icons to work in Next?如何让矢量图标在 Next 中工作?

Note, this is specifically Next/Expo/React Native注意,这是专门的 Next/Expo/React Native

You can read more about setup of next-adapter-icons here .您可以在此处阅读有关设置下next-adapter-icons的更多信息。

I got it working with following approach,我用以下方法让它工作,

  • 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>
  );
}

Using import like this:像这样使用导入:

import MaterialIcons from '@expo/vector-icons/MaterialIcons'

in place of:代替:

import { MaterialIcons } from '@expo/vector-icons'

worked for me.为我工作。 I think this is because of the way babel/webpack handles imports in the template.我认为这是因为 babel/webpack 处理模板中的导入的方式。 I followed the steps here to setup the icons.我按照此处的步骤设置图标。

Here's what that looks like on web:这是 web 上的样子: 截屏

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

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