【问题标题】:Different type of import in React Native?React Native 中不同类型的导入?
【发布时间】:2017-04-17 08:35:13
【问题描述】:

在 React Native 中我们必须通过以下方法导入包。

  1. 从 'react-native-router-flux' 导入 { Actions };
  2. 从“react-native-push-notification”导入 PushNotification;

这些导入语句有什么不同?

【问题讨论】:

标签: react-native react-native-android react-native-ios


【解决方案1】:

当您像这样导出组件/事物时

export class Foo extends Component {…

你必须像这样导入它

import {Foo} from './foo.js'

当你默认导出时,像这里

export default class Bar extends Component {…

你可以这样导入

import Bar from './bar.js'

当然,您可能每个文件只有一个默认导出。

【讨论】:

  • 感谢@Viktor Sec
  • 所以这意味着在这个例子中import Foo, { getBar } from 'react-native-xxx';文件有2个导出类?
  • 是的,Foo 是默认导出,而getBar 只是常规导出。
【解决方案2】:

默认导出

默认导出如下:

const student={
  name: 'Sam'
}
export default student;

它被导入为

import student from './student.js'

import std from './student.js'

接收文件中的名称由您决定。只能有一个默认导出。

命名导出

命名导出如下:

export const anyFunc = () => {...}

export const anyValue = 10;

它被导入为:

import { anyFunction } from './anyfile.js'
import { anyValue } from './anyfile.js'

每个文件可以有多个命名导出。 导入您想要的特定导出并将它们括在大括号中。 导入模块的名称必须与导出模块的名称相同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-12
    • 1970-01-01
    • 2019-06-25
    • 2020-09-17
    • 2019-06-25
    • 2021-10-01
    • 1970-01-01
    • 2018-10-24
    相关资源
    最近更新 更多