【发布时间】:2017-06-28 00:02:21
【问题描述】:
当我尝试在构造函数、componentWillMount 或 componentDidMount 中使用 Moment.js 中的日期时,我收到错误消息:
Uncaught TypeError: _moment2.default.date is not a function
我没有使用 Webpack 或 npm 之外的任何特定构建工具。这是我的相关代码:
import React from 'react';
import Moment from 'moment';
export default class Date extends React.Component {
constructor() {
super();
this.state = { day: '',
month: '',
year: '',
weekday: ''
};
}
componentDidMount() {
this.setDate();
}
setDate() {
this.state.day = Moment.date();
this.state.month = Moment.format('MMM');
this.state.year = Moment.year();
this.state.weekday = Moment.format('dddd');
}
这是因为渲染组件时 Moment 尚未完全加载,还是其他原因?我怎样才能解决这个问题?
当我在 render() 函数中调用 Moment 时,我没有收到此错误。但是,我需要状态中的日期信息,以便我可以根据日期更新其他应用程序组件。
【问题讨论】:
标签: javascript reactjs momentjs