简体   繁体   English

JavaScript / React本机代码中的导入语句错误

[英]Error on import statement in JavaScript/React Native code

I'm trying to incorporate a React component for radio buttons in my iOS app that's written in React Native, however I get an error when trying to import the component using the method that the author specified. 我试图在用React Native编写的iOS应用程序中合并用于单选按钮的React组件,但是在尝试使用作者指定的方法导入组件时出现错误。

I first installed the component in the root directory of the app's XCode project/source code using the following statement: 我首先使用以下语句将组件安装在应用程序的XCode项目/源代码的根目录中:

npm i -S react-native-radio-buttons

Everything looked like it went through fine, so I incorporated the code for the component into the JS file for the screen that would use it, but I get an error on the very first line (which contains the import statement). 一切看起来都很好,所以我将组件的代码合并到了要使用它的屏幕的JS文件中,但是在第一行(包含import语句)出现错误。

The import statement goes like this: import语句如下所示:

import { RadioButtons } from 'react-native-radio-buttons'

And the error is: 错误是:

Uncaught SyntaxError: Unexpected reserved word

As far as I can tell, that should be an acceptable way of doing things in ES6. 据我所知,这应该是ES6中可接受的处理方式。 If anyone could tell me why this would happen, I'd be grateful. 如果有人能告诉我为什么会这样,我将不胜感激。 Thanks in advance. 提前致谢。

react-native-radio-buttons author here, react-native-radio-buttons作者,

I assumed everybody was using Babel with ES6 features enabled. 我以为每个人都在使用启用了ES6功能的Babel。 I should add that to the README. 我应该将其添加到自述文件中。

Edit: instruction and example .babelrc added to 0.4.2 编辑:指令和示例.babelrc添加到0.4.2

Please try add this .babelrc file to your project root, as the provided example does: 请尝试将此.babelrc文件添加到您的项目根目录,如提供的示例所示:

{
  "whitelist": [
    "es6.modules"
  ]
}

Are you using a tool to translate from ES6? 您是否正在使用从ES6进行翻译的工具? "import" is not going to work. “导入”将无法正常工作。 Have you tried: 你有没有尝试过:

var RadioButtons = require('react-native-radio-buttons'); 

instead? 代替?

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

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