导入Moment.js时JavaScript语法错误

时间:2018-11-15 21:52:16

标签: javascript

我需要导入Moment.js,所以我先添加脚本然后再导入:

 <script src="~/Content/js/moment.js" type="moment"></script>
<script>
    import moment from 'moment';
</script>

我总是会出错:导入声明只能出现在模块的顶层

感谢帮助。

3 个答案:

答案 0 :(得分:0)

Check the docs

这是在浏览器中使用时刻的方法

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
<script>
  const today = moment()
  console.log(`Today's  date is ${today.format('DD MM YYYY')}`)
  const tomorrow = today.add(1, 'days')
  console.log(`Tomorrow is ${tomorrow.format('dddd')}`)
</script>

答案 1 :(得分:0)

根据Momentjs文档:

<script src="moment.js"></script>
<script>
    moment().format();
</script>

答案 2 :(得分:0)

您不需要在浏览器环境中导入lib。 您引用了lib源,并且在加载DOM之后,当前对象将位于全局范围内。 这是一个example

var now = moment().format('MMM DD h:mm A');