1

我正在尝试create-react-native-app在 monorepo 设置中与 expo 一起使用。当我从子文件夹启动应用程序app/并导入时@expo/vector-icons,我收到一个字体系列丢失的错误。

"fontFamily" 'material' is not a system font and has not been loaded 
through Expo.Font.loadAsync.

如果我在主src/文件夹中启动应用程序,则图标加载正常。

我已经配置了我rn-cli.config.js的应用程序,以便应用程序编译并运行良好的其他依赖项。我的项目设置类似于 monorepo,因此我可以在 repo 中有多个本机应用程序。

src/
  MainApp.js
  package.json
  app/App.js 
  app/app.json
  app/package.json
  app/rn-cli.config.js
  ...

我尝试了几件事无济于事:

  • 安装@expo/vector-icons在子文件夹 package.json
  • "assetExts": ["ttf"]app.json文件中设置。

我的代码(主要来自新的creat-react-native-app):

应用程序/App.js

export { default } from "../MainApp";

应用程序/应用程序.json

{
  "expo": {
    "sdkVersion": "22.0.0",
    "react": "16.0.0-beta.5"
  }
}

应用程序/package.json

{
  "private": true,
  "main": "./node_modules/react-native-scripts/build/bin/crna-entry.js",
  "scripts": {
    "native": "react-native-scripts start"
  },
  "dependencies": {
    "@expo/vector-icons": "^6.2.1",
    "expo": "^22.0.0",
    "react-native": "^0.49.0",
    "react-native-scripts": "1.7.0"
  }
}

应用程序/rn-cli.config.js

const blacklist = require("metro-bundler/src/blacklist");
const path = require("path");

const roots = [process.cwd(), path.resolve(__dirname, "..")];

const config = {
  getProjectRoots: () => roots,

  /**
   * Specify where to look for assets that are referenced using
   * `image!<image_name>`. Asset directories for images referenced using
   * `./<image.extension>` don't require any entry in here.
   */
  getAssetRoots: () => roots,

  /**
   * Returns a regular expression for modules that should be ignored by the
   * packager on a given platform.
   */
  getBlacklistRE: () =>
    blacklist([
      // Ignore the local react-native so that we avoid duplicate modules
      /\/app\/node_modules\/react-native\/.*/
    ])
};

module.exports = config;

包.json

{
  "name": "react-native-app",
  "version": "0.1.0",
  "private": true,
  "devDependencies": {
    "react-native-scripts": "1.7.0"
  },
  "scripts": {
    "native": "cd app && yarn native"
  },
  "dependencies": {
    "@expo/vector-icons": "^6.2.1",
    "expo": "^22.0.0",
    "react": "16.0.0-beta.5",
    "react-native": "^0.49.0"
  }
}

MainApp.js

import React from "react";
import { StyleSheet, Text, View } from "react-native";
import MaterialIcons from "react-native-vector-icons/MaterialIcons";

export default class App extends React.Component {
  render() {
    return (
      <View style={styles.container}>
        <Text>Open up App.js to start working on your app!</Text>
        <Text>Changes you make will automatically reload.</Text>
        <Text>Shake your phone to open the developer menu.</Text>
        <MaterialIcons name="search" color="black" size={32} />
        <MaterialIcons name="location-searching" color="black" size={32} />
      </View>
    );
  }
}
const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: "#fff",
    alignItems: "center",
    justifyContent: "center"
  }
});
4

3 回答 3

2

我对 react-native 很陌生,但我遇到了类似的问题,我通过根本不安装 expo Icons 并正常使用它们来解决。所以在我的 package.json 我有

 "react-native-vector-icons": "^4.4.2",

我导入如下字体:

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

我敢打赌这不是完美的解决方案,但由于 expo 只是一个让事情变得更容易的工具,我不会因为做一些有效的事情而感到难过,而不是使用另一个工具,这会使事情变得复杂;)

于 2017-12-04T22:09:19.177 回答
2

似乎问题在于我有两个不同的博览会模块。摆脱子文件夹中的那个使它工作。

于 2017-12-06T06:52:11.203 回答
0

我的世博会版本是“世博”:“^32.0.0”。您需要做的就是添加支持 react-native-vector-icons 组的字体系列,例如 AntDesign、MaterialIcons、FontAwesome 等。

import FontAwesomeIcon from 'react-native-vector-icons/FontAwesome';

<FontAwesomeIcon name="circle" size={10} color="#FF6859" style={{ marginLeft: 5 }} />

您还可以从https://expo.github.io/vector-icons/找到图标

于 2019-02-25T11:37:59.247 回答