我正在使用Root的主题 - Sage
我试图将谷歌地图排队,已经数小时没有成功。
我一直在Chrome控制台中收到此错误:
this.route.parent.parent.parent.params.subscribe(...)
从我可以解决的问题来看,它意味着它找不到像经典jQuery未定义的脚本排序问题这样的函数。
setup.php
Uncaught InvalidValueError: initMap is not a function ... js?key=MY_API_KEY&callback=initMap:95
我的Main.js摘录
/**
* Theme assets
*/
function assets() {
wp_enqueue_style('sage/css', Assets\asset_path('styles/main.css'), false, null);
wp_enqueue_style('font-awesome', '//maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css', false, null);
if (is_single() && comments_open() && get_option('thread_comments')) {
wp_enqueue_script('comment-reply');
}
wp_enqueue_script('sage/js', Assets\asset_path('scripts/main.js'), ['jquery'], null, true);
wp_enqueue_script('jquery-validate', '//cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.15.1/jquery.validate.min.js', [], null, true);
if (is_page_template('template-about.php')) {
wp_enqueue_script('google-maps', '//maps.googleapis.com/maps/api/js?key=MY_API_KEY_HERE&callback=initMap', [], null, true);
}
}
add_action('wp_enqueue_scripts', __NAMESPACE__ . '\\assets', 100);
第106行是googlemaps网址行。特别是(function($) {
// Use this variable to set up the common and page specific functions. If you
// rename this variable, you will also need to rename the namespace below.
var Sage = {
// All pages
'common': {
init: function() {
if (!$('body').hasClass('home')) {
$(this).find('.banner').removeClass('navbar-fixed-top');
}
var map;
console.log('reached');
function initMap() {
var location = {lat: 51.47672559, lng: -3.17107379};
var markerloc = {lat: 51.476852, lng: -3.167869};
map = new google.maps.Map(document.getElementById('map'), {
center: location,
scrollwheel: false,
zoom: 17
});
var marker = new google.maps.Marker({
position: markerloc,
map: map,
title: 'Hello World!'
});
}
}
},
部分。
脚本在DOM中显示如下。
&callback=initMap
有人可以帮助我吗?
我被告知这一点,但我不确定如何做到这一点?
最有可能的是,Google地图最终排队等候 在sage / js之后。您的条件块应考虑到并添加 google-maps作为sage / js的依赖。
答案 0 :(得分:1)
正如charlietfl所说的那样依赖
wp_enqueue_script('google-maps', '//maps.googleapis.com/maps/api/js?key=MY_API_KEY_HERE&callback=initMap', ['sage-js'], null, true);
我需要添加' sage-js'作为对地图的依赖。