【问题标题】:Using Moment.js with React Native Typescript将 Moment.js 与 React Native Typescript 一起使用
【发布时间】:2020-05-08 18:13:45
【问题描述】:

我正在使用 Moment.js 和 React Native 以下列方式显示/操作日期和时间,但似乎不能正确使用它。下面的代码演示了我对该库的使用。

import * as React from 'react';
import { Text, View, StyleSheet } from 'react-native';
import Constants from 'expo-constants';
import * as moment from 'moment';
import { Card } from 'react-native-paper';

interface event {
  title: string,
  created: any,
  expiration: any
}

const party : event = {
  title: '21st Bday Party',
  created: moment('2019 03 14'),
  expiration: moment('2019 03 15')
}

export default function App() {
  return (
    <View style={styles.container}>
      <Card>
        <Text>{party.created.format()}</Text>
      </Card>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    paddingTop: Constants.statusBarHeight,
    backgroundColor: '#ecf0f1',
    padding: 8,
  },
});

我收到以下错误:TypeError: moment is not a function.我做错了什么?如何解决这个问题?

【问题讨论】:

    标签: typescript react-native momentjs


    【解决方案1】:

    您以错误的方式导入时刻。

    你必须像这样导入时刻:

    import moment from 'moment';
    

    显然,不要忘记安装包之前:

    npm install moment --save
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-27
      • 1970-01-01
      • 2017-08-19
      • 2018-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多