[英]Moment js undefined Laravel
我試圖在我的laravel項目中使用moment.js,而不是使用vue,但仍通過npm導入軟件包。
我的app.js文件:
require('moment/moment.js');
require('./bootstrap');
require('bootstrap-select');
在我的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})
然后在我的custom.js文件中,我得到了這個:
$(function() {
"use strict";
let date = moment().format();
console.log(date);
});
我收到以下錯誤:
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)
我已經確認該時刻在我編譯的app.js文件中。 那么為什么我的custom.js文件看不到它,我需要怎么做才能解決它?
在這一刻,您沒有為任何事情分配moment
。 根據文檔,您應該將其分配給要使用的變量:
var moment = require('moment');
moment()。format();
但是,如果您想在app.js
文件范圍之外使用它,則將無法使用。 一種方法是將其添加到window
對象:
window.moment = require('moment');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.