简体   繁体   English

如何使用 React Native 矢量图标?

[英]How to use React Native vector icons?

I have just installed React Native vector icons with this comand:我刚刚用这个命令安装了React Native 矢量图标

npm install react-native-vector-icons

But if I use it in the index.android.js但是如果我在 index.android.js 中使用它

<Icon name="ios-add" size={30} color="#4F8EF7" />

I get a square with a X in the center instead of the icon.我得到一个正方形,中间有一个 X 而不是图标。

Why ?为什么 ?

EDIT:编辑:

If I try to install it with "npm install react-native-vector-icons --save", then I get this error:如果我尝试使用“npm install react-native-vector-icons --save”安装它,则会收到此错误:

C:\Users\xrobot\Desktop\React Native\AwesomeProject>npm install react-native-vector-icons --save

    npm ERR! Windows_NT 10.0.14393
    npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "install" "react-native-vector-icons" "--save"
    npm ERR! node v6.9.5
    npm ERR! npm  v3.10.10
    npm ERR! path C:\Users\xrobot\Desktop\React Native\AwesomeProject\node_modules\.staging\react-native-vector-icons-73f301e7
    npm ERR! code EPERM
    npm ERR! errno -4048
    npm ERR! syscall rename

    npm ERR! Error: EPERM: operation not permitted, rename 'C:\Users\xrobot\Desktop\React Native\AwesomeProject\node_modules\.staging\react-native-vector-icons-73f301e7' -> 'C:\Users\xrobot\Desktop\React Native\AwesomeProject\node_modules\react-native-vector-icons'
    npm ERR!     at destStatted (C:\Program Files\nodejs\node_modules\npm\lib\install\action\finalize.js:25:7)
    npm ERR!     at C:\Program Files\nodejs\node_modules\npm\node_modules\graceful-fs\polyfills.js:264:29
    npm ERR!     at FSReqWrap.oncomplete (fs.js:123:15)
    npm ERR!
    npm ERR! Error: EPERM: operation not permitted, rename 'C:\Users\xrobot\Desktop\React Native\AwesomeProject\node_modules\.staging\react-native-vector-icons-73f301e7' -> 'C:\Users\xrobot\Desktop\React Native\AwesomeProject\node_modules\react-native-vector-icons'
    npm ERR!     at Error (native)
    npm ERR!  { Error: EPERM: operation not permitted, rename 'C:\Users\xrobot\Desktop\React Native\AwesomeProject\node_modules\.staging\react-native-vector-icons-73f301e7' -> 'C:\Users\emiliano\Desktop\React Native\AwesomeProject\node_modules\react-native-vector-icons'
    npm ERR!     at destStatted (C:\Program Files\nodejs\node_modules\npm\lib\install\action\finalize.js:25:7)
    npm ERR!     at C:\Program Files\nodejs\node_modules\npm\node_modules\graceful-fs\polyfills.js:264:29
    npm ERR!     at FSReqWrap.oncomplete (fs.js:123:15)
    npm ERR!
    npm ERR! Error: EPERM: operation not permitted, rename 'C:\Users\emiliano\Desktop\React Native\AwesomeProject\node_modules\.staging\react-native-vector-icons-73f301e7' -> 'C:\Users\xrobot\Desktop\React Native\AwesomeProject\node_modules\react-native-vector-icons'
    npm ERR!     at Error (native) parent: 'AwesomeProject' }
    npm ERR!
    npm ERR! Please try running this command again as root/Administrator.

    npm ERR! Please include the following file with any support request:
    npm ERR!     C:\Users\xrobot\Desktop\React Native\AwesomeProject\npm-debug.log

    C:\Users\xrobot\Desktop\React Native\AwesomeProject>npm install react-native-vector-icons
    AwesomeProject@0.0.1 C:\Users\xrobot\Desktop\React Native\AwesomeProject
    `-- react-native-vector-icons@4.0.0

EDIT 2:编辑2:

C:\Users\xrobot\Desktop\React Native\AwesomeProject>react-native link react-native-vector-icons
rnpm-install info Linking react-native-vector-icons android dependency
rnpm-install info Android module react-native-vector-icons has been successfully linked
rnpm-install info Linking react-native-vector-icons ios dependency
rnpm-install info iOS module react-native-vector-icons has been successfully linked
rnpm-install info Linking assets to ios project
rnpm-install WARN ERRGROUP Group 'Resources' does not exist in your XCode project. We have created it automatically for you.
rnpm-install info Linking assets to android project
rnpm-install info Assets have been successfully linked to your project

EDIT 3:编辑 3:

I have removed the app from the physical device and then I re-build it ( as QMFNP said ).我已经从物理设备中删除了该应用程序,然后我重新构建了它(如 QMFNP 所说)。 Now it works现在它起作用了

First, make sure you're saving the dependency in your project by doing:首先,确保您通过执行以下操作来保存项目中的依赖项:

npm install react-native-vector-icons --save . npm install react-native-vector-icons --save Including the --save is necessary, otherwise react-native link won't be able to locate the module.包括--save是必要的,否则react-native link将无法定位模块。

见附件截图

Before you can use them in your iOS or Android project, you also have to link the native modules.在您的 iOS 或 Android 项目中使用它们之前,您还必须链接原生模块。 The quick way to do this is by using the following command:执行此操作的快速方法是使用以下命令:

react-native link react-native-vector-icons

If for any reason you have problems using react-native link to link the native modules, the react-native-vector-icons README also provides detailed instructions for linking them manually on Android and iOS, and integrating the library on the web as well.如果由于任何原因您在使用react-native link链接本机模块时遇到问题, react-native-vector-icons README还提供了在 Android 和 iOS 上手动链接它们的详细说明,以及在网络上集成库。

Here is full UPDATED answer, just follow these steps :-这是完整的更新答案,只需按照以下步骤操作:-

1. npm install react-native-vector-icons --save 2. react-native link 3. react-native link react-native-vector-icons 4. import Icon using one of these ( as per your requirement ) 1. npm install react-native-vector-icons --save 2. react-native link 3. react-native link react-native-vector-icons 4.使用其中之一导入Icon (根据您的要求)

   **MaterialCommunityIcons**

    import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
   **Ionicons**

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

   **FontAwesome5**

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

5. Uses (JSX) 5.用途(JSX)

     <Icon size={24} color="white" name="movie" />

Step 1: Install react-native-element:第 1 步:安装 react-native-element:

npm install react-native-elements --save

Steps 2: Install react-native-vector-icons Install from npm步骤 2:安装 react-native-vector-icons 从 npm 安装

npm install react-native-vector-icons --save

Link it链接它

react-native link react-native-vector-icons

After that you can use it in your page by: Step 1:之后,您可以通过以下方式在您的页面中使用它: 第 1 步:

import { Icon } from 'react-native-elements';

Step 2:第2步:

<Icon name="md-beer" type="ionicon" color="#887700" />

Maybe you are not importing correctly the library, you must specify a family of icons you wanna use.也许您没有正确导入库,您必须指定要使用的图标系列。

I think the name of the icon you are using not correspond with the icon you are searching.我认为您使用的图标名称与您正在搜索的图标不符。

For example, if you will use material icons, the import will be: import Icon from 'react-native-vector-icons/MaterialIcons';例如,如果您将使用材质图标,则导入将是: import Icon from 'react-native-vector-icons/MaterialIcons';

And change the name to name={add}并将名称更改为name={add}

Try look for other icons family if you don want use material icons.如果您不想使用材质图标,请尝试寻找其他图标系列。

You can use :您可以使用 :

icon = {"<"Icon name='lock'/>} 

Or :或者 :

icon = {{ type:'font-awesome',name:'lock'}}

in any React native componant property.在任何 React 原生组件属性中。

I think this may help, at least this is what I have to do every time.我认为这可能会有所帮助,至少这是我每次都必须做的事情。 In the Project/android/app/build.gradle , add this line:Project/android/app/build.gradle ,添加以下行:

apply from: "../../node_modules/react-native-vector-icons/fonts.gradle"

follow this step manual https://github.com/oblador/react-native-vector-icons#option-manually-1按照此步骤手册https://github.com/oblador/react-native-vector-icons#option-manually-1

this may help some one install react native vector icon with cmd and follow every manual step but leave the mainApplication.java which is at android>app>src>main>java>com>youApplication>mainApplication.java comment it out if you run the auto linkng command which is react-native link react-native-vector-icons //import com.oblador.vectoricons.VectorIconsPackage;这可能有助于某些人使用 cmd 安装 react native 矢量图标并遵循每个手动步骤,但保留位于 android>app>src>main>java>com>youApplication>mainApplication.java 的 mainApplication.java 如果您运行auto linkng 命令是 react-native link react-native-vector-icons //import com.oblador.vectoricons.VectorIconsPackage; // Packages that cannot be autolinked yet can be added manually here, for example: // packages.add(new MyReactNativePackage()); // 不能自动链接的包可以在这里手动添加,例如: // packages.add(new MyReactNativePackage()); // packages.add(new VectorIconsPackage()); //packages.add(new VectorIconsPackage()); and rebuild your app this work for me I'm able to find it out in total more than 10hours并为我重建您的应用程序这项工作我能够找到它总共超过 10 小时

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

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