我正在使用Moment.js和React Native以如下方式显示manipulate日期和时间,但似乎无法正确使用它。下面的代码演示了我对该库的使用。
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. 我做错了什么?如何解决这个问题?
你导入moment的方式不对。
你必须像这样导入 moment 。
import moment from 'moment';
很明显,不要忘记在安装之前安装.NET包。
npm install moment --save