You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

安装vue-google-autocomplete后出现Google引用错误求助

Fixing "ReferenceError: google is not defined" with vue-google-autocomplete in Laravel + Vue

Ah, that classic google is not defined error—let me break down what's missing here and how to fix it quickly for your Laravel 5.6 + Vue setup:

The core issue is that the vue-google-autocomplete component depends entirely on the Google Maps JavaScript API (specifically the Places library) to function, but you haven't loaded that API into your page yet. Here's the step-by-step fix:

Step 1: Add the Google Maps API Script to Your Layout

First, you need to include the Google Maps JavaScript API (with the Places library enabled) in your main Blade template (usually resources/views/layouts/app.blade.php).

You’ll need a valid Google Maps API key (grab one from the Google Cloud Console, and make sure to enable both the Maps JavaScript API and Places API for your project).

Add this script tag either in the <head> section or right before your closing </body> tag (placing it before your Vue app script is better to ensure the google object exists when Vue initializes):

<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_GOOGLE_MAPS_API_KEY&libraries=places"></script>

Replace YOUR_GOOGLE_MAPS_API_KEY with your actual key. The libraries=places parameter is critical—it loads the Places library that powers the autocomplete functionality.

Step 2: Ensure the API Loads Before Your Vue App

In your Blade template, make sure the Google Maps script loads before your compiled app.js file. A typical setup at the end of your body would look like this:

<!-- Load Google Maps API first -->
    <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places"></script>
    <!-- Then load your Vue app -->
    <script src="{{ mix('js/app.js') }}"></script>
</body>

Step 3: Optional: Guard Against Late API Loading

If you ever run into edge cases where the API takes longer to load than your Vue app, add a guard to your component to only render it once the google object exists:

<vue-google-autocomplete 
    v-if="typeof google !== 'undefined'"
    id="map" 
    classname="form-control" 
    placeholder="Start typing"
></vue-google-autocomplete>

This prevents the component from trying to initialize before the API is ready.

Quick Security Note

Don’t forget to set up API key restrictions in the Google Cloud Console (e.g., restrict it to your domain's HTTP referrers) to prevent unauthorized use of your key.

内容的提问来源于stack exchange,提问作者Pankaj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 09:04:55