2017-03-20 41 views
-2

在我main.js文件我有:問題加載谷歌地圖API與requirejs

'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但對象是不確定的。

回答

-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 

    });