簡體   English   中英

如何在 react-native-web 中使用 react-native-vector-icons?

[英]How to use react-native-vector-icons with react-native-web?

我已經使用react-nativereact-native-web設置了 monorepo 項目。 我正在為 Android、iOS 和 Web 共享相同的代碼庫。 安裝 react-native-vector-icons 后,我在所有三個平台上都運行了代碼,它在 Android 和 iOS 中運行良好,但在 Web 中運行良好。 在網絡上它看起來像下面:

在此處輸入圖片說明

我已經按照這里的描述設置了Webpack

下面是我的Webpack配置config-overrides.js

const fs = require('fs');
const path = require('path');
const webpack = require('webpack');

const appDirectory = fs.realpathSync(process.cwd());
const resolveApp = relativePath => path.resolve(appDirectory, relativePath);

// our packages that will now be included in the CRA build step
const appIncludes = [
    resolveApp('src'),
    resolveApp('../components/src'),
];
//below is the config for react-native-vector-icons
const ttf = {
    test: /\.ttf$/,
    loader: "file-loader",
    include: path.resolve(__dirname, "../../node_modules/react-native-vector-icons"),
};
module.exports = function override(config, env) {
    // allow importing from outside of src folder
    config.resolve.plugins = config.resolve.plugins.filter(
        plugin => plugin.constructor.name !== 'ModuleScopePlugin'
    );
    config.module.rules[0].include = appIncludes;
    config.module.rules[1] = ttf; //add the react-native-vector-icons here
    config.module.rules[2].oneOf[1].include = appIncludes;
    config.module.rules[2].oneOf[1].options.plugins = [
        require.resolve('babel-plugin-react-native-web'),
    ].concat(config.module.rules[2].oneOf[1].options.plugins);
    config.module.rules = config.module.rules.filter(Boolean);
    config.plugins.push(
        new webpack.DefinePlugin({__DEV__: env !== 'production'})
    );
    return config
};

下面是在我的App.js文件中使用react-native-vector-icons

import Icon from 'react-native-vector-icons/dist/FontAwesome';

<Icon name="glass" size={24}/>

我不知道為什么圖標沒有加載或我錯過了配置。 請幫我。 先感謝您。

最后經過大量研究,我找到了如下所述的解決方案:

add the TTF files and your custom font file into the public directory 

我剛剛從react-native-paper遇到了這個但不同的矢量圖標問題。

在這種情況下,要正確顯示圖標,必須在指向 ttf 文件的全局 ccs 中定義自定義字體系列。

https://github.com/oblador/react-native-vector-icons#web-with-webpack

好的。 所以讓我們假設你使用 nextjs 和 react-native-web。 我將逐步為您詳細介紹如何使其在 Web 上運行。

安裝所有這些包yarn add react-native-vector-icons react-native-svg next-compose-plugins next-transpile-modules

更新你的 next.config.js

const withPlugins = require('next-compose-plugins');
const withTM = require('next-transpile-modules')(['react-native-vector-icons', 'react-native-svg']);

module.exports = withPlugins([withTM], {
  images: {
    disableStaticImages: true,
  },
  typescript: {
    ignoreBuildErrors: true,
  },
  webpack: (config) => {
    config.resolve.alias = {
      ...(config.resolve.alias || {}),
      // Transform all direct `react-native` imports to `react-native-web`
      'react-native$': 'react-native-web',
    }
    config.resolve.extensions = [
      '.web.js',
      '.web.ts',
      '.web.tsx',
      ...config.resolve.extensions,
    ]
    config.module.rules.push(
      {
        test: /\.(jpe?g|png|gif|svg)$/i,
        use: [ 'url-loader?limit=10000', 'img-loader' ]
      },
    );
    return config
  },
});

轉到 node_modules/react-native-vector-icons/Fonts 並復制您需要的所有字體並將它們粘貼到您的公共文件夾中,就像您向項目添加自定義字體一樣,您需要將所有這些字體包括在內你的 style.css

@font-face {
    src: url(Ionicons.ttf);
    font-family: "Ionicons";
}

@font-face {
    src: url(FontAwesome.ttf);
    font-family: "FontAwesome";
}

@font-face {
    src: url(MaterialIcons.ttf);
    font-family: "MaterialIcons";
}

@font-face {
    src: url(Feather.ttf);
    font-family: "Feather";
}

@font-face {
    src: url(MaterialCommunityIcons.ttf);
    font-family: "MaterialCommunityIcons";
}

在您的 _document.js 文件中,確保像這樣包含 style.css 文件

        <Head>
          <link href="/fonts/style.css" rel="stylesheet"/>
        </Head>

最后導入它import Icon from 'react-native-vector-icons/MaterialCommunityIcons';

並設置它<Icon name="google-play" size={30} color="#900" />

瞧!!!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM