在我的main.js文件中,我有:
'use strict';
requirejs.config({
baseUrl: './',
paths: {
'jquery': 'bower_components/jQuery/jquery.min',
'async': 'bower_components/requirejs-plugins/src/async'
}
});
require(['jquery', 'async!http://maps.google.com/maps/api/js?key=siteKey'], function($, gMaps) {
console.log("$=" + $); //defined and working
console.log("gMaps=" + gMaps); //undefined
});
它运行没有错误,似乎加载gMaps但对象未定义。
答案 0 :(得分:-1)
我使用此资源http://blog.millermedeiros.com/requirejs-2-0-delayed-module-evaluation-and-google-maps/
解决了这个问题在main.js中,代码现在是
requirejs.config({
baseUrl: './',
paths: {
'jquery': 'bower_components/jQuery/jquery.min',
'load': 'resources/js/index/load',
'user': 'resources/js/index/user',
'async': 'bower_components/requirejs-plugins/src/async'
}
});
require(['jquery', 'gMaps'], function($, gMaps) {
console.log("$=" + $); //defined
console.log('googleMaps=' + gMaps); // defined
});
define('gMaps', ['async!http://maps.google.com/maps/api/js?v=3&key=siteKey'],
function(){
return window.google.maps;
});
define(['gMaps'], function(gMaps){
console.log('googleMaps=' + gMaps); // defined
});