简体   繁体   English

Moment js undefined Laravel

[英]Moment js undefined Laravel

I'm trying to use moment.js in my laravel project, not using vue, but still importing the package via npm. 我试图在我的laravel项目中使用moment.js,而不是使用vue,但仍通过npm导入软件包。

My app.js file: 我的app.js文件:

require('moment/moment.js');
require('./bootstrap');
require('bootstrap-select');

in my webpack.pix.js file looks like this: 在我的webpack.pix.js文件中看起来像这样:

const mix = require('laravel-mix');    

mix.setResourceRoot('laravel/public');

mix.js('resources/js/app.js', 'public/js')
     .combine([
          'resources/theme/dist/js/app.js',
          'resources/theme/dist/js/app.init.js',
          'resources/theme/dist/js/sidebarmenu.js',
          'resources/theme/dist/js/waves.js',
          'resources/theme/assets/libs/perfect-scrollbar/dist/perfect-scrollbar.jquery.min.js',    
          'resources/theme/assets/extra-libs/sparkline/sparkline.js',  
          'resources/theme/dist/js/custom.js',
          'resources/js/custom.js'
     ], 'public/js/nice-admin.js')
     .copyDirectory('resources/theme/assets/images/', 'public/assets/images')
     .copyDirectory('resources/theme/dist/css/icons/material-design-iconic-font/fonts', 'public/fonts')
     .copy('node_modules/bootstrap-daterangepicker/daterangepicker.css', 'public/css/daterangepicker.css')
     .copy('node_modules/bootstrap-select/dist/css/bootstrap-select.min.css', 'public/css/bootstrap-select.min.css')
     .sass('resources/sass/app.scss', 'public/css', {
           implementation: require('node-sass')
}).options({processCssUrls: false})

And then in my custom.js file I've just got this: 然后在我的custom.js文件中,我得到了这个:

$(function() {
    "use strict";

    let date = moment().format();
    console.log(date);
});

I'm getting the following error: 我收到以下错误:

app.js:13202 Uncaught ReferenceError: moment is not defined
    at HTMLDocument.<anonymous> (nice-admin.js:2144)
    at mightThrow (app.js:12909)
    at process (app.js:12977)

I've verified that moment is in my compiled app.js file. 我已经确认该时刻在我编译的app.js文件中。 So why can't my custom.js file see it, and what do I need to do to fix it? 那么为什么我的custom.js文件看不到它,我需要怎么做才能解决它?

At the minute you not assigning moment to anything. 在这一刻,您没有为任何事情分配moment As per the docs you should assign it to a variable to be used: 根据文档,您应该将其分配给要使用的变量:

var moment = require('moment'); var moment = require('moment');
moment().format(); moment()。format();

However, this will not work if you want to use it outside of the scope of your app.js file. 但是,如果您想在app.js文件范围之外使用它,则将无法使用。 One way to do this would be to add it to the window object: 一种方法是将其添加到window对象:

window.moment = require('moment');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM