如何在反应原生中要求下划线 [英] How to require underscore in react-native

查看:37
本文介绍了如何在反应原生中要求下划线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

react-native 提到的变更日志https://facebook.github.io/react/blog/2015/04/17/react-native-v0.4.html

The change log of react-native mentions https://facebook.github.io/react/blog/2015/04/17/react-native-v0.4.html

NPM 模块兼容性:NPM 上有很多不依赖于节点/浏览器内部结构的库,这些库在 React Native 中非常有用,例如 superagent、下划线……

NPM modules compatibility: There are a lot of libraries on NPM that do not depend on node/browser internals that would be really useful in React Native, such as superagent, underscore, ...

但这对我不起作用.我是这样安装的,通过 package.json

But It doesn't work for me. It is how I install, through package.json

# package.json

 "dependencies": {
   "react-native": "*",
   "underscore": "^1.8.3"
   ...

我确实在 npm 依赖中看到了它

And I indeed see it in the npm dependecy

# npm ls
├─┬ react-native@0.8.0
|  ...
├── react-native-navbar@0.7.3
└── underscore@1.8.3

它确实适用于其他一些 React 组件

And it does work for some other react components

这是我的要求

var _ = require('underscore');

但是不行,_未定义

推荐答案

如果你正在使用 ES6 模块(就像在 ReactNative 中一样)正确的方法是使用 import 语句:

If you are using ES6 module (like in ReactNative) the correct way is to use the import statement:

import _ from 'lodash'

let text = _.isUndefined(route.rightButtonText) ? 'Default value' : route.rightButtonText;

这篇关于如何在反应原生中要求下划线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆